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.

相关推荐
源远流长jerry8 小时前
TCP 性能管理核心:滑动窗口、流量控制与拥塞控制机制解析
运维·服务器·网络
北方的流星9 小时前
华为交换机MSTP和VRRP综合应用配置
运维·网络·华为
宠..9 小时前
奇怪的语法错误
运维·服务器·数据库·qt·microsoft
EndingCoder9 小时前
数组和元组:处理集合数据
linux·运维·前端·ubuntu·typescript
数据雕塑家9 小时前
Linux运维实战:巧用文件操作实现SSH免密登录配置
linux·运维·ssh
天码-行空9 小时前
【大数据环境安装指南】HBase单机环境搭建教程
大数据·linux·运维·数据库·hbase
南棱笑笑生10 小时前
20260105给荣品PRO-RK3566开发板适配Rockchip原厂的Buildroot【linux-5.10】系统时解决eth0不能开机就打开的问题
linux·运维·服务器·rockchip
弓弧名家_玄真君10 小时前
虚拟机里的ubuntu 系统 设置宿主机关机ip不变化
linux·运维·ubuntu
今晚务必早点睡10 小时前
Linux 压力测试实战操作手册:从环境准备到瓶颈定位的完整流程
linux·运维·压力测试
qq_3168377510 小时前
安卓Termux的ssh服务常开
运维·ssh