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.
- Docker
- Git
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.
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/
An empty Dockerfile has been provided has been provided, to complete this:
- Choose a base image
- Add your project files
- Install project with pip
- Configure the
ENTRYPOINT
to run the project
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
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>
Go to https://localhost:5000
List the running docker processes, and take note of your container's ID:
$ docker ps
View the logs of the container:
$ docker logs <CONTAINER-ID>
Connect to the container:
$ docker exec -it <CONTAINER-ID> sh
From within the container you can show the environment variables:
$ echo $NAME
Stop the container:
$ docker stop <CONTAINER-ID>
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 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.
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>
From within the root directory of the repository, run:
$ docker-compose up -d
To stop:
$ docker-compose down
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:
- Multi-stage Docker builds
- Pushing to and pulling from Dockerhub
- Conference talk: Container Operator's Manual by Alice Goldfuss