
Deploy basic Python web application with Docker Compose
Learn to deploy a basic Python web app with Docker Compose: Define services, manage containers, and simplify your deployment process.
Published Jan 28, 2025
Using the Flask framework, the application features a hit counter in Redis, providing a practical example of how Docker Compose can be applied in web development scenarios.
- Ensure Docker is installed on your machine. Docker allows you to package your application and its dependencies into a container.
- Installed the latest version of Docker Compose
- A basic understanding of Docker concepts and how Docker works
- You should have Python installed to run the Flask application locally and manage Python dependencies.
- Use a text editor or an Integrated Development Environment (IDE) to write your Python code. Popular choices include Visual Studio Code, PyCharm or Sublime Text.
First, create a directory for your project and navigate into it.
ย Create a file called
app.py
in your project directory.In this example,
redis
is the hostname of the redis
container on the application's network and the default port, 6379
is used.Create another file called
requirements.txt
in your project directory and add the dependencies for Flask
and Redis
.Create a
Dockerfile
to define the environment for running your Flask application.Understand the Dockerfile:
- Build an image starting with the Python 3.10 image.
- Set the working directory to
/code
. - Set environment variables used by the
flask
command. - Install gcc and other dependencies
- Copy
requirements.txt
and install the Python dependencies. - Install requirements.txt
- Add metadata to the image to describe that the container is listening on port 5000
- Copy the current directory
.
in the project to the workdir.
in the image. - Set the default command for the container to
flask run --debug
.
Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file.
Create a file called
compose.yaml
in your project directory to configure your Docker services.This Compose file defines two services:
web
and redis
.The
web
service uses an image that's built from the Dockerfile
in the current directory. It then binds the container and the host machine to the exposed port, 8000
. This example service uses the default port for the Flask web server, 5000
.The
redis
service uses a public Redis image pulled from the Docker Hub registry.With a single command, you create and start all the services from your configuration file.
From your project directory, start up your application by running
docker compose up
.Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.
Open your web browser and Enter
http://localhost:8000/
in a browser to see the application running.If this doesnโt resolve, you can also try
http://127.0.0.1:8000
.You should see a message in your browser saying:
Hello World! I have been seen 1 times.

Refresh the page.
Hello World! I have been seen 2 times.

Switch to another terminal window, and type
docker image ls
to list local images.Listing images at this point should return
redis
and web
.You can inspect images with
docker inspect <tag or id>
.Stop the application, either by running
docker compose down
from within your project directory in the second terminal, or by hitting CTRL+C
in the original terminal where you started the app.Edit the
compose.yaml
file in your project directory to use watch
so you can preview your running Compose services which are automatically updated as you edit and save your code:Whenever a file is changed, Compose syncs the file to the corresponding location under
/code
inside the container. Once copied, the bundler updates the running application without a restart.Check the
Hello World
message in a web browser again, and refresh to see the count increment.To see Compose Watch in action:
Change the greeting in
app.py
and save it. For example, change the Hello World!
message to Hello from Docker!
:return 'Hello from Docker! I have been seen {} times.\n'.format(count)

Once youโre done, run
docker compose down
.Using multiple Compose files lets you customize a Compose application for different environments or workflows.
This is useful for large applications that may use dozens of containers, with ownership distributed across multiple teams.
In your project folder, create a new Compose file called
infra.yaml
.Cut the Redis service from your
compose.yaml
file and paste it into your new infra.yaml
file. Make sure you add the services
top-level attribute at the top of your file. Your infra.yaml
file should now look like this:In your
compose.yaml
file, add the include
top-level attribute along with the path to the infra.yaml
file.Run
docker compose up
to build the app with the updated Compose files, and run it. You should see the Hello world
message in your browser.If you want to run your services in the background, you can pass the
-d
flag (for "detached" mode) to docker compose up
and use docker compose ps
to see what is currently running:Run
docker compose --help
to see other available commands.If you started Compose with
docker compose up -d
, stop your services once you've finished with them.You can bring everything down, removing the containers entirely, with the
ย
docker compose down
command.ย