Build a flask App using Docker Compose

DockerOperating SystemOpen Source

Docker Compose allows you to build multi-container Docker applications. If you are working on a microservice project where you have different nodes working on different parts of the project, Docker Compose is exactly what you need. Using Docker Compose, you can work on different components of the project on different Docker Containers and combine them to create a single application.

In this article, we are going to discuss how to build a Flask application which uses Python modules and we will try to run it inside a Docker Container using Docker Compose.

First, you need to install Docker Compose in your local machine. To install Docker Compose, you can use either of the following methods −

  • Using package manager

sudo apt−get install docker−compose
  • From the official repository

You need curl to download files from the official repository.

sudo apt−get install curl

sudo curl −L "−compose−$(uname −s)−$(uname −m)" −o /usr/local/bin/docker−compose

sudo chmod +x /usr/local/bin/docker−compose

The above sequence of commands downloads and installs curl from apt package manager. After that using curl, it downloads the latest version of Docker Compose from it’s official github repository and then using the chmod command, it gives the user the executable permission.

After you have downloaded Docker Compose in your local machine, create a directory structure as follows.

Create a folder named “application” and inside that create another folder named “web” which will contain all our flask files.

Move to the “web”directory and create a python file called “”. Create a simple flask application inside the file or paste the code below.

#Import the flask module
from flask import import Flask

#Create a Flask constructor. It takes name of the current module as the argument
app = Flask(__name__)

#Create a route decorator to tell the application, which URL should be called for the #described function and define the function

def tutorialspoint():
return "Welcome to TutorialsPoint"

#Create the main driver function
if __name__ == '__main__':
#call the run method,host='')

The above flask code creates a simple flask application and prints “Welcome to TutorialsPoint”. After you have created the python file, create the dockerfile with the following code.

FROM python:3
RUN apt−get −y update
RUN apt−get install −y pip3 build−essential
COPY . .
RUN pip3 install −r requirements.txt
ENTRYPOINT ["python3"]
CMD [""]

The above dockerfile creates a python 3 image, runs updates and installs essential python packages. It then uses the requirements.txt file mentioned below to install the flask library and defines the entrypoint as the flask app which we just created in the file.

Create a file called requirements.txt inside the web folder and paste the following.


Now, go to the parent directory called application and create a docker-compose.yml file with the following contents.

build: ./web
− "5000:5000"
− .:/code

The above file builds the image from the dockerfile in the “web” directory and then exposes 5000 port to the container’s 5000 port. It then mounts the current directory to /code on docker container.

You can now build the docker image “application” from the directory called “web” using the following command.

sudo docker−compose up

To conclude, in this article we saw how to install docker compose, build a simple flask web application, create a dockerfile which builds the image and then deploy then deploy the application as a service using Docker Compose. The above mentioned steps is the most simplistic approach to create a web application using Docker Compose.

Published on 27-Oct-2020 12:29:03