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.

相关推荐
碳基沙盒2 小时前
OpenClaw 多 Agent 配置实战指南
运维
小p19 小时前
docker学习7:docker 容器的通信方式
docker
小p20 小时前
docker学习5:提升Dockerfile水平的5个技巧
docker
小p21 小时前
docker学习3:docker是怎么实现的?
docker
小p2 天前
docker学习: 2. 构建镜像Dockerfile
docker
小p3 天前
docker学习: 1. docker基本使用
docker
蝎子莱莱爱打怪3 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀3 天前
Docker部署Nacos
docker·容器
缓解AI焦虑3 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
1candobetter4 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka