Flask Docker

We’re going to create a Python Flask application in docker. You learn how to use Docker on your local computer and how to run and create docker images.

Make a flask app at app/main.py. For the Docker image we will use, you need to do two important things: Make sure the app is really called main.py Make sure the Flask app variable is really called app. Running Flask on Docker Swarm Let's look at how to spin up a Docker Swarm cluster on DigitalOcean and then configure a microservice, powered by Flask and Postgres, to run on it. This is an intermediate-level tutorial. It assumes that you have basic working knowledge of Flask, Docker, and container orchestration.


What is docker?

Flask Docker

Flask Docker Azure

Docker is a software container system that allows build containers from the logic of your application to automate their deployment.

Containers help developers break the program into tiny pieces including collections, dependencies, and so on and build a package from it

Related course:Python Flask: Create Web Apps with Flask

Why docker?

  • Docker provides security for apps running in a shared environment.
  • Rapid development of the process.
  • Simple to scale and also to monitor.
  • Ensure a consistent environment for new constructions.
  • Open the source, and it’s free to be used.


You need to install python3, python3-pip and flask


Create a new directory “myapp” for your project. To create an image we need a program, as I described earlier, you can use a python flask app.

Start it with python app.py, and open the url localhost:5000. You should see the output in your web browser.

Docker file

Docker gives us the ability to create custom images with the assistance of Dockerfile. Dockerfile is a plain file containing steps on how to create the image.

Flask Docker Hub

Create a Dockerfile and insert the code into it:

So what does this mean?

FROM informs docker from which base image you would like to base the image. In this instance, we are going to create an image that keeps Ubuntu as the base image.

The RUN command executes commands inside the image. In this easy example, you’re installing python3-pip and flask packages.

The command ADD attaches code to the image.
It supports 2 arguments:

  • a source file
  • a target inside the image to which the data has to be copied.

In this case, we transfer app.py to the/(root) directory of the file.

WORKDIR configures the current working directory of the program to execute each of the commands in.In this case, it is configured as / (root).

EXPOSE: Exposes the port in which to run our app. In this configuration, the Flask app runs on port 5000 by default.

Flask docker image

The most important step, the command to start your application.

Build docker image

Open a terminal and type

The command docker build tells the image from the docker file.

To see all images type the command:

Create and run Docker container

To run your Flask app from the image, you can use the command docker run.

If everything went right, you’ll see the same output on localhost:5000

You’ve made your first docker container with Python Flask!

Docker commands

Docker has some commands you may find useful:

Related course:Python Flask: Create Web Apps with Flask