{{announcement.body}}
{{announcement.title}}

How to Dockerize a Python Flask Application

DZone 's Guide to

How to Dockerize a Python Flask Application

In this article, we'll walk you through how to dockerize a Flask application with Python step-by-step.

· Cloud Zone ·
Free Resource

In this article, we will set up a python Flask application and Dockerize it efficiently in a few minutes. 

Steps to Follow

Let's Create a simple Python Flask Application. Refer to the code here: Docker Flask Repo.

Now, its time to package our application into a docker image. Create a Dockerfile with the following steps:

1. This installs Python3.7 on the image

Dockerfile


2. Specify DEBIAN_FRONTED=noninteractive.

Dockerfile


3. Install all the system requirements that are required for the docker image

Dockerfile


4. Let's jump into the working directory:

Dockerfile


5. Create a new directory for keeping the Flask application code:

Dockerfile


6. Now change the working directory as shown below:

Dockerfile


7. Copy all your workspace files into the image:

Dockerfile


8. Now, install all your code dependencies from requirements.txt:

Dockerfile


9. Don't forget to upgrade your pip:

Dockerfile


10. Change your working directory where your packaging is done:

Dockerfile


11. Let's shine the docker instructions from here:

Dockerfile


We are done with creating the docker file that is required for a docker image. Now, run the docker file to create an image:

Plain Text


Refer to the following article to learn more about Docker Image vs Docker Container. Now, let's create the docker-compose.yml file to run the docker image in a container.

Let's provide the information required for the container to start. We have only one service here, and the image name docker_flask. Run the app.py file with the make command.  

Dockerfile


Your make file has does starts your application:

Plain Text


Now, start your container which starts your Python Flask web app.

Plain Text


Now your simple Python Flask application is up and running in the Docker container. Now check the container id, with:

Plain Text


This lists all your docker containers. Check your application logs with:

Plain Text


Hurray, we are done running the flask web app on a docker container!

Topics:
containers, docker, flask, python 3.7.0, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}