Dockerize a Python application, work with containers.

In this scenario we have a simple flask application that we want to dockerize, our application consists of a single file named server.py.

Directory Structure

Our application directory structure is the following

server.py

src/server.py: this directory holds the server.py file, which is our application, its content are:

Its a simple application, it only returns “hello world” but its great for our demo purposes.

requirements.txt

requirements.txt: the libraries that our application needs, will be added to the image, its content are

Dockerfile

Dockerfile: is the instructions needed for docker to create the image. its content are:

Lets examine what each line does:

It asks docker to use image python:3.8, if this image could not be found localy it will be downloaded from dockerhub.

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile instruction.

Copies requirements.txt to WORKDIR

It will install to the image we create the libraries defined in requirements.txt

It will copy all contents of src/ to WORKDIR

This is the command that will be executed when docker runs a container using this image, this is a shortcut to

Building the image

Now we know what each line of the Dockerfile does lets proceed to the actuall building of the image

This command will create a local docker image with the name python-flask

Viewing the image

This command will output all the local images allong with some details like their id, when created, size, name, tag, etc

This command will output a long JSON output with details regarding this image.

Starting the container

  • -d: starts the container in the background
  • -p 5000:5000 maps port 5000 of the container to the local port 5000 of the docker host, we know that we have to map port 5000 to docker host because 5000 is the default flask port.

Now start a browser and enter http://localhost:5000 you should see something like the following

This means that we have build the image successfully and created a container using this image.

Lets see some details regarding the running container

This command outputs the status of the running containers

We can see the container id and which image is used, the command that the container runs, when the container created and when the container started, any port forwards that we might created, also docker adds a random name to each container, in this case is inteligent-cohen

Launch multiple containers using a single image

Docker can create multiple containers using the same image, the only restriction is that the local forwarded port cannot be 5000 because is already taken by “inteligen-cohen” container.

Note that this this example we used local port 5001

Start a browser and enter http://localhost:5001

Success! we are getting the hello world text!

At the prompt enter:

We can see that the running containers are now two

One container is the “inteligent_cohen” and the second one is “dreamy_taussig” which started 47 seconds back, and the local forwarded port is 5001

Stopping Containers

This command will stop the container by sending to its running process a SIGTERM signal, if the application has not been terminated in 10 seconds then docker will automatically issue a SIGKILL command to the PID of this container which is equal to

Killing a container is not a good idea because it does not give time to the container to finnish any tasks that might had not end, this can led to data corruption in some cases.

To verify that the container has terminated we can issue

We see that only one container is currently running, the “inteligent_cohen”. There is a “trick” we can see what containers run in the past by using the -a switch

We can see that “dreamy_taussig” exited 5 minutes ago.

Restarting containers

Stopping a container does not mean that the container is deleted, we can restart the container with the start command

We can see that the container is up and running.

Deleting a container

To delete a container and not just to stop the container we need to stop the container using the stop command and then the container rm command

As you can see the docker ps -a does not show any stopped container, the container has been deleted.

Note that deleting a container does not deletes the image.

I hope you found the article interesting :)

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store