DEV Community

Edith Ogechi Odo
Edith Ogechi Odo

Posted on Edited on

Containerizing a Flask Task Management API with Docker and Docker Compose

As part of my DevOps Engineering capstone at Abuja Data School, I worked on containerizing a Task Management API.

The objective was to understand how to package an application and its dependencies into containers, making it easier to build, run, test, and deploy consistently across environments.

The application was built with Flask and PostgreSQL.

Here's how I approached the project.

  1. Understanding the Application Architecture

Before containerizing the application, I examined its structure and understood how the API communicated with the database.

The application used Flask for its API layer and PostgreSQL for persistent data storage.

Architecture components

  • Flask: Handles API requests and application logic.
  • Gunicorn: Serves the Flask application.
  • PostgreSQL: Stores task records.
  • Docker: Packages the application into a container.
  • Docker Compose: Coordinates the application and database services.
  1. Cloning the Repository

I started by cloning the task management project:

git clone https://github.com/...../task-mgt-capstone.git
cd task-mgt-capstone

I inspected the repository and reviewed the README to understand the application requirements and how it was expected to run.

  1. Understanding the Environment Configuration

The application required environment variables for database connectivity and other settings.

I reviewed the example environment configuration and prepared the required local environment values.

This helped separate configuration from application code.

  1. Creating the Dockerfile

I created a Dockerfile to define how the application image would be built.

The image needed to include:

  • A suitable Python runtime.
  • Application dependencies.
  • The application source code.
  • The required startup configuration.
  1. Configuring Docker Compose

Rather than starting the API and database independently, I used Docker Compose to manage the services.

The Compose configuration defined the application and PostgreSQL services, their environment variables, networking, and persistent database storage.

This allowed the services to communicate through the Compose network.

  1. Building and Starting the Containers

I used:

docker compose up -d --build

This built the application image and started the services in the background.

I checked the running containers with:

docker compose ps

To inspect application logs, I used:

docker compose logs

  1. Testing the API

I tested the application's API endpoints to confirm that it was responding correctly.

The application included health and readiness endpoints:

  • /health – checks application health.
  • /ready – checks application readiness.

I also tested the task API using HTTP requests.

For example, the application supported creating and retrieving task records through its API.

  1. Running Automated Tests

I ran the project's tests using:

python -m pytest -q

The tests passed.

The project used SQLite for its automated tests, while PostgreSQL was used as the application database in the containerized environment.

  1. Publishing the Docker Image

After building and testing the application, I prepared the Docker image for publishing to a container registry.

This made the image available for use in subsequent CI/CD and Kubernetes deployment stages.

Top comments (0)