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.

相关推荐
r-t-H18 小时前
Docker进阶与容器编排实践-第三章
运维·docker·容器
willhuo18 小时前
Docker 存储目录迁移:解决 No space left on device
docker·容器·eureka
啦啦啦~~~33018 小时前
【装机工具】电脑重装系统!office安装管理软件!一键自动化下载、安装、部署Office的办公增强工具
运维·c语言·windows·自动化·电脑
Legend NO2418 小时前
从数据中台到 Data Fabric:数据价值落地,终究要回归本质(二)
大数据·运维·fabric
vortex518 小时前
解决 Alpine Linux 虚拟机从 VirtualBox 迁移到 VMware 的内核崩溃问题
linux·运维
Dontla19 小时前
WSL卡死解决办法(wsl2卡死、WSL死机、WSL无响应、WSL无法启动、Docker Desktop卡死)(重启后解决了)
docker
qq_白羊座19 小时前
Linux 压缩 / 解压(tar)命令 + 参数详解
linux·运维·github
极客先躯19 小时前
高级java每日一道面试题-2026年02月07日-实战篇[Docker]-如何使用存储插件(如 NFS、Ceph)?
运维·分布式·容器·自动化·文件·插件·高可用
“码”力全开19 小时前
打通安防孤岛:基于 Docker 与 GB28181/RTSP 架构的 AI 视频管理平台,全源码交付解锁二次开发自主权
人工智能·docker·架构
IT探索19 小时前
服务器 BIOS 测试
运维·服务器·网络