Dockerize your Flask Application

Abhinav Kumar
3 min readJul 8, 2021

In this post we are going to create a simple flask application, build docker image for the application and run the image as container.

Photo by Ian Taylor on Unsplash

Before you follow along, you need to make sure that you have Python and Docker installed on your system.

Setup Sample Flask Application

Create a directory called flask-docker and follow below steps:

cd /path/to/flask-docker
python3 -m venv env
source env/bin/activate
pip3 install flask
pip3 freeze > requirements.txt

In the above steps we are doing the following:

  • Change to the project root directory.
  • Create a virtual environment to hold all the dependencies of this project.
  • Activate the environment.
  • Install flask as the dependency using pip package manager.
  • Freeze the requirements — specify the dependencies in a file that pip will read and install to the docker containers.

Now that we have set up the environment and installed dependencies, let’s create a simple flask application. Create file called app.py in project root and paste in below code:

from flask import Flaskapp = Flask(__name__)@app.route('/')
def index():
return 'Hello Flask Docker'
if __name__ == '__main__':
app.run(debug=True)

To run the application follow below steps:

source env/bin/activate
flask run

In the above steps we are doing the following:

  • Activate the virtual environment in which we have set up our dependencies.
  • Run flask server.

Open a browser and navigate to http://localhost:5000/. You should see “Hello Flask Docker”

Setting up Docker

We need to create a Dockerfile which contains instructions required to create an image. Create a file called Dockerfile in the project root and paste in below code:

FROM python:3.8-slim-buster WORKDIR /appCOPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .CMD [ "flask", "run", "--host=0.0.0.0"]

In the above code we are doing the following:

  • Specifying a base image to build image from.
  • Create a working directory to be used as default location for all subsequent commands.
  • Copy requirements.txt from project root to the working directory (app).
  • Install requirements into the container from requirements.txt
  • Copy source code from project root to the working directory (app).
  • CMD specify what command we want to execute when our image is executed inside a container.

For creating an image we need to run below command

docker build --tag flask-docker:v0.0.1 .

Here, we are using docker build . to build an actual image by reading the instructions in the Dockerfile. The optional tag flag is used to set the name of the image and tag in the format <name>:<tag>. To list all the images:

docker images

At this point we have an image with which we can create a container and run it as below:

docker run -d -p 5000:5000 flask-docker

Here, we are using below flags:

  • -d (detach) to run the container in detached mode.
  • -p (publish flag) to map host’s port to the container’s port.

Open a browser and navigate to http://localhost:5000/. You should see “Hello Flask Docker” again but this time our application is running inside a container.

Conclusion

In this post, we created a simple flask application, created a Dockerfile and build a docker image. We then used the image to create a docker container.

Hope this helps to get started with dockerizing your flask applications.

Happy learning !!!

--

--