Building a Containerised Backend with Docker Compose

Overview

In this exercise, I built a fully containerised backend system using Docker Compose.

The system consists of a Python API and a MySQL database, running in separate containers but working together as a single application.

The goal was to understand how services are connected, started, and verified in a containerised environment.


Step 1: Build a Simple Python API

I first created a minimal Python API that supports:

  • Adding a product (POST)

  • Fetching all products (GET)

Example API routes:

复制代码
@app.route("/products", methods=["POST"])
def add_product():
    ...

@app.route("/products", methods=["GET"])
def get_products():
    ...

This API is responsible only for handling HTTP requests and database operations.


Step 2: Containerise the API with Dockerfile

Next, I containerised the API using a Dockerfile.

复制代码
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app.py .
CMD ["python", "app.py"]

This defines:

  • The runtime environment

  • The dependencies

  • How the API starts inside a container


Step 3: Prepare Database Initialisation

I created an SQL script that automatically runs when the database container starts for the first time.

复制代码
CREATE TABLE products (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100),
  quantity INT
);

This ensures the database schema is ready without manual setup.


Step 4: Orchestrate Services with Docker Compose

Using Docker Compose, I defined and connected the API and database services.

Docker Compose automatically:

  • Creates a shared network

  • Allows services to communicate using service names

  • Manages startup order


Step 5: Handle Startup Timing Issues

During startup, the API initially failed because the database was not ready.

I solved this by adding a retry mechanism in the API so it waits until the database becomes available.

复制代码
def get_db_connection():
    while True:
        try:
            return mysql.connector.connect(...)
        except:
            time.sleep(3)

This made the system stable and resilient during startup.


Step 6: Verify the System

Finally, I verified the system by sending HTTP requests directly from the command line.

复制代码
curl -X POST http://localhost:5000/products \
  -H "Content-Type: application/json" \
  -d '{"name":"Apple","quantity":10}'

curl http://localhost:5000/products

Both data insertion and retrieval worked as expected.


Final Outcome

I successfully built and ran a fully containerised backend system ,

where a Python API and MySQL database communicate through Docker Compose,

and verified that the system works end-to-end.

相关推荐
echome88813 分钟前
Linux 定时任务:crontab 写法与常见失效原因
linux·运维·网络·定时任务·crontab
老约家的可汗14 分钟前
Linux开发三件套:make、git、gdb 入门指南
linux·运维·服务器
分布式存储与RustFS1 小时前
可观测性数据落进对象存储:Proscia 案例拆解与 ClickHouse 自托管配置
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
方便面不加香菜1 小时前
Docker Image-- 镜像命令详解与使用示例
运维·docker·容器
Star(up up up)1 小时前
Docker系列---Dockerfile镜像文件
人工智能·经验分享·学习·程序人生·docker·容器·github
-星空下无敌2 小时前
Windows Docker K8s 安装部署Naocs ,SpringBoot 3.x 4.x 整合Naocs 版本兼容 配置中心、服务注册与发现
windows·docker·nacos·kubernetes·springboot·alibaba
芥子沫2 小时前
从零开始用 Docker 部署 Hoppscotch:自建 API 调试平台的完整教程
docker·接口测试·api开发·api管理
yujunlong39192 小时前
[特殊字符] Serverpod 4.1.0 全栈项目开发与 Docker 部署规范文档
flutter·docker·serverpod
吴声子夜歌2 小时前
Kubernetes权威指南——深入掌握Pod(使用StatefulSet搭建MongoDB集群)
容器·kubernetes
天远API2 小时前
零信任架构实战:基于天远劳动仲裁信息查询构建自动化灵活用工劳务派遣人员准入背调网关
运维·人工智能·架构·自动化