Simple Docker-Compose Deployment for Python-based Todo Applications

Neeraj GuptaNeeraj Gupta
3 min read

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:

  1. Microservices Architecture
    This project adopts a modular architecture where different components (services) are isolated:

    1. 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.

    2. Application Service: Contains CRUD operations for managing tasks. Isolated from the database to ensure that changes to one service do not impact the other.

  2. 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 the init.sql script to set up the schema.

  • The todoApp service starts after the postgres service and connects to it using the environment variables for database credentials and connection details.

  • Users interact with the todoApp service through APIs exposed on http://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:

  1. We are using python:3.9-slim as a parent image for our application image.

  2. 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

  1. Here, we have used the .env file for the environment variables.
    Advantages:
    - Decouples sensitive data from code.
    - Allows easy reconfiguration without modifying the codebase.

  2. Created the volumes for data persistence.

  3. 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.

  4. The application includes a script (init.sql) to initialize the database schema (e.g., creating the goals table).

  5. Configured the restart policies ensuring that the services automatically recover after unexpected crashes or host reboots.

Benefits of this setup

  1. Containerized and Portable: Both the application and database are encapsulated in containers, making it easy to deploy anywhere.

  2. Isolation: The services are isolated on a dedicated network, enhancing security and reducing conflicts with other services.

  3. Data Persistence: Database data is stored in a persistent volume to prevent data loss.

  4. 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

1
Subscribe to my newsletter

Read articles from Neeraj Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Neeraj Gupta
Neeraj Gupta