Build a flask App using Docker Compose

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.

Installing 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 "https://github.com/docker/compose/releases/download/1.24.0/docker-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 its official github repository and then using the chmod command, it gives the user the executable permission.

Setting Up Directory Structure

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.

mkdir application
cd application
mkdir web

Creating the Flask Application

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

# Import the flask module
from flask 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
@app.route('/')
def tutorialspoint():
    return "Welcome to TutorialsPoint"

# Create the main driver function
if __name__ == '__main__':
    # call the run method
    app.run(debug=True, host='0.0.0.0')

The above flask code creates a simple flask application and prints "Welcome to TutorialsPoint".

Creating the Dockerfile

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 ["app.py"]

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 app.py file.

Creating Requirements File

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

Flask

Creating Docker Compose Configuration

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

web:
  build: ./web
  ports:
    - "5000:5000"
  volumes:
    - .:/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.

Building and Running the Application

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

sudo docker-compose up

This command will build the Docker image and start the Flask application. You can access the application by opening your web browser and navigating to http://localhost:5000.

Project Structure Overview

Your final project structure should look like this −

application/
??? docker-compose.yml
??? web/
    ??? Dockerfile
    ??? app.py
    ??? requirements.txt

Conclusion

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 the application as a service using Docker Compose. This approach provides a containerized environment for your Flask applications, making them easily portable and scalable.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements