Coder Social home page Coder Social logo

lythi45 / pyladies-docker-workshop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emilywoods/pyladies-docker-workshop

0.0 1.0 0.0 79 KB

a hands-on introduction to docker ๐Ÿณ

License: MIT License

Python 23.55% CSS 41.63% HTML 34.82%

pyladies-docker-workshop's Introduction

PyLadies Docker Workshop

Welcome everyone! ๐Ÿ ๐ŸŒˆ

The purpose of this workshop is to provide an interactive introduction to Docker.

This repository contains a configurable Hello World web server application. The goal of this exercise is to containerize the application and run it as an image.

Prerequisites

  • Docker
  • Git

Instructions

The application code is located within the /helloworld directory. This is a simple Flask application which takes in one environment variable, NAME and runs a web server on port 5000.

Install and run the application

Get started with the usual approach to installing and running the application. We can later compare this approach with how you would run the application with Docker.

$ cd helloworld/app
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -e .
$ python app.py

Navigate to http://localhost:5000/

Complete the Dockerfile

An empty Dockerfile has been provided has been provided, to complete this:

  1. Choose a base image
  2. Add your project files
  3. Install project with pip
  4. Configure the ENTRYPOINT to run the project

Run your container

  1. Build the container with a tag, replacing <IMAGE-TAG> with hello-world:latest:

    $ docker build . -t <IMAGE-TAG>

Once it's built, you can show your built images with:

$ docker images
  1. Run the container. --publish maps a port from within the container to a port on your local machine, and -env defines an environment variable that will be present in your container's environment:

    $ docker run --publish 5000:5000 --env NAME=<YOUR NAME> <IMAGE-TAG>
  2. Go to https://localhost:5000
  3. List the running docker processes, and take note of your container's ID:

    $ docker ps
  4. View the logs of the container:

    $ docker logs <CONTAINER-ID>
  5. Connect to the container:

    $ docker exec -it <CONTAINER-ID> sh

From within the container you can show the environment variables:

$ echo $NAME
  1. Stop the container:

    $ docker stop <CONTAINER-ID>

Image Optimisation

If you used the python:3.7 base image, you will see that the image size is ~900MB when you run:

$ docker images

This is quite a large image, for an application of this size! The official Python base images have slim variants with fewer system libraries installed, such as python:3.7-slim. Does using this base image have an impact on your image size?

Docker Compose

Docker Compose is a tool which can be used to define and run multiple docker containers. An empty docker-compose file has been provided for you to fill in.

You can install it from here.

Complete the docker-compose file

Note: if you use Linux, you will need to install this separately from Docker, you can find install instructions here. For MacOS and Windows, it will already be installed.

Provide the version of Compose to use:

version: "3"

Define the container, giving it a name hello-world, and providing an image tag with version latest to use:

services:
  hello-world:
    image: hello-world:latest

Map port 5000 on the container to port 5000 on the host machine:

ports:
  - "5000:5000"

Define your environment variable:

environment:
  NAME: <YOUR NAME>

Use docker-compose!

From within the root directory of the repository, run:

$ docker-compose up -d

To stop:

$ docker-compose down

Further learning

Well done! You've just:

  • Made a Dockerfile
  • Built, run and explored the docker container
  • Used Docker Compose

That's a lot to take in! What's next?

If you want to learn more about Docker and containers, we recommend:

pyladies-docker-workshop's People

Contributors

emilywoods avatar autophagy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.