Simple Docker-Compose Deployment for Python-based Todo Applications
This blog will show how to deploy the todo application with a docker-compose file.
About application
This application setup consists of two main components orchestrated using Docker Compose: a PostgreSQL database and a Todo application. Here's a brief overview:
Microservices Architecture
This project adopts a modular architecture where different components (services) are isolated:Database Service: Powered by PostgreSQL, a relational database that stores structured data like tasks and their metadata and decouples from the application to allow flexibility in scaling and managing storage.
Application Service: Contains CRUD operations for managing tasks. Isolated from the database to ensure that changes to one service do not impact the other.
Orchestration Using Docker Compose
Role of Docker Compose: Docker Compose is used to define, configure, and run multi-container applications.It simplifies the management of the Postgres and todoApp services, ensuring they work together seamlessly.
It handles dependencies (e.g., ensuring the database is up before the application starts).
Application Workflow
The
postgres
service initializes the database, including running theinit.sql
script to set up the schema.The
todoApp
service starts after thepostgres
service and connects to it using the environment variables for database credentials and connection details.Users interact with the
todoApp
service through APIs exposed onhttp://localhost:8081
.Data from the application (e.g., tasks or goals) is stored in the PostgreSQL database.
Code-base
Here, we are using a Python flask-based application with the following structure code.
├── app.py
├── Dockerfile
├── .dockerignore
├── requirements.txt
└── templates
└── index.html
In Dockerfile:
We are using python:3.9-slim as a parent image for our application image.
Using Gunicorn web server to run our application.
Build context
The path to the location where the Dockerfile & other supporting files are stored, this path is known as the build context.
Whatever path is specified as the build context is where the docker daemon looks for the Dockerfile as well as supporting files.
Whenever the build is initiated by the “docker build“ command, the files under the build context are transferred to the docker daemon, at the temp directory of the docker’s filesystem (docker home directory).
In the project, we also used the “.dockerignore“ file, adding all the files or folders to be ignored by the build context.
Docker-compose file
Here, we have used the .env file for the environment variables.
Advantages:
- Decouples sensitive data from code.
- Allows easy reconfiguration without modifying the codebase.Created the volumes for data persistence.
Created a custom Docker network (todo-network) to ensure secure communication between the todoApp and Postgres services, isolating them from other containers or services on the host.
The application includes a script (init.sql) to initialize the database schema (e.g., creating the goals table).
Configured the restart policies ensuring that the services automatically recover after unexpected crashes or host reboots.
Benefits of this setup
Containerized and Portable: Both the application and database are encapsulated in containers, making it easy to deploy anywhere.
Isolation: The services are isolated on a dedicated network, enhancing security and reducing conflicts with other services.
Data Persistence: Database data is stored in a persistent volume to prevent data loss.
Scalable: Additional instances of the
todoApp
can be added easily if needed.
├── app
│ ├── app.py
│ ├── Dockerfile
│ ├── .dockerignore
│ ├── requirements.txt
│ └── templates
│ └── index.html
├── docker-compose.yml
├── docs
│ ├── docker-compose-steps.sh
│ └── docker-steps.sh
├── .env.sample
├── init.sql
└── README.md
We are using the following Todo application repo.
Github: https://github.com/minex970/python-based-todo-application
Subscribe to my newsletter
Read articles from Neeraj Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by