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.

相关推荐
翼龙云_cloud5 分钟前
云代理商:Hermes Agent在量化交易中的实战应用
运维·服务器·人工智能·ai智能体·hermes agent
木雷坞13 分钟前
Home Assistant Docker Compose 升级失败排查:镜像、备份和设备映射
服务器·docker·home assisant
无限进步_19 分钟前
【Linux】Makefile:让编译自动化
linux·运维·自动化
Jinkxs22 分钟前
LoadBalancer- 简单限流策略:Nginx 基于连接 / 请求的限流实现
java·运维·nginx
流浪00130 分钟前
告别静态打印:Linux C 实现实时刷新进度条
linux·运维·c语言
qq_1969761732 分钟前
硬核教程:用Gemini境像站构建端到端自动化办公工作流,告别重复操作(国内免费镜像实测)
运维·自动化
小此方32 分钟前
Re:Linux系统篇(二十)进程篇·五:深入理解 Linux 进程优先级:从底层逻辑到实战修改
linux·运维·服务器
流浪00137 分钟前
Linux篇(八) Make 与 Makefile 超详细入门教程|从零基础到手写自动化编译
linux·运维·自动化
j_xxx404_42 分钟前
Linux线程:从内存分页机制(Page Table/TLB/Page Fault)彻底读懂 Linux 线程本质
linux·运维·服务器·开发语言·c++·人工智能·ai
老码观察1 小时前
K8s 容器化部署的宿主机资源规划的踩坑实录
docker·容器·kubernetes