Docker containers wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in.

– from What Is Docker?

If you are new to Docker, this guide will show you how to create a basic Docker container.

I have a small Python Flask web application that I want to put inside a Docker container. I also want to run the web application using a server called uwsgi. Dockerizing a Python Flask application will give you a straightforward intro into Docker containers.

Build a Docker Container

Contents of ./requirements.txt
This file specifies software dependencies needed for our web application.

Contents of ./

Contents of ./Dockerfile

In this configuration:

  • First, we update the Ubuntu OS that is shipped with the container:
  • Next, we install Python, Python development files and a package manager for Python. Since we are using a Debian-based OS inside the container, apt-get is used to install packages. This may be different, depending on the flavor of Linux you ship:
  • Next, we install all dependencies needed for our app. This is done before adding our application files to the container because Docker will skip this step if requirements.txt has not changed upon subsequent builds. Every Docker command builds a layer that is cached and if a single layer is changed, Docker will invalidate this cache. This ordering will prevent our dependencies from being reinstalled upon changes to our application code.
  • Next, we add all application files to the /webapp directory inside the container:
  • Next, we set the working directory to /webapp:
  • Next, port 8000 is exposed to allow communication to the container. By default, no ports are exposed:
  • Finally, we specify the command to run when the container is started:
  • Alternatively, we can set the ENTRYPOINT to a command without flags. This will allow flags to be specified when the container is initialized:

Build the Docker container:

Run the Container

In this configuration:

  • -d runs the container in detached mode. To run the container in the foreground, remove this flag.
  • –cpuset-cpus 1 restricts the container to use only 1 CPU. To remove this restriction, remove this flag.
  • –name flask allows custom container names, where we assign the container name “flask”. If a name is not specified, Docker will automatically assign one.
  • –restart=always will automatically restart the container if the application exits.
  • travcunn/flask specifies the image for the container, which was built in a previous step.

How valuable is your time as an IT admin? Spend time focusing on the big tasks  by taking FTP servers and file management off your plate. Click here to give  SmartFile a try and see how much time you save on non-business critical tasks.

Test Your Container

Once the container is running, we can check if the web server is running.

When a Docker container is started, it is automatically assigned an IP address that is accessible only to your local machine. To check our HTTP server, we need to find the IP address of the container. First, find the container ID:

Next, inspect the container to find the IP:

Now visit: or the IP address of the container in your web browser. You should see a response from the server running inside your container.

Stay Tuned

I hope you enjoyed this article on dockerizing a python flask application. Stay tuned for future Docker posts about application service discovery, connecting Docker containers, and using databases with Docker applications.


Get our free DevOps course delivered straight to your inbox! You’ll learn these tactics:

  • Docker Tips and Tricks
  • Agile Methodologies
  • Documentation and Tools Hacks
  • Containerization vs Virtualization

These DevOps lessons will help your team collaborate and become more agile, so sign up now!

5 thoughts on “Dockerizing a Python Flask Application”

  1. Great article! It would be interesting to follow up with how you’d connect a Dockerized Flask app to Nginx. One way would be to Dockerize Nginx and use something like compose to link the two containers.

Leave a Reply

Your email address will not be published. Required fields are marked *