Build a Real Python Application

2026-12-02来源 dev.toEnglish
Build a Real Python Application

Introduction

Over the past four days, I progressed from running single pre-built containers to orchestrating static websites with Docker Compose. Today, I built a real full-stack multi-container application: a custom Python (Flask) web app running in its own container, communicating over Docker's private network with a MySQL 8 database backed by a persistent named volume.

1. Flask, Dependencies & Building Custom Images

  • What is Flask? Flask is a lightweight, beginner-friendly Python web framework used to build web applications and APIs quickly without heavy boilerplate.
  • What is requirements.txt? It is a plain-text manifest listing all external Python libraries (flask==3.0.0 and mysql-connector-python==8.3.0) that an application depends on so Docker or another developer can install them automatically with pip.
  • What is a Dockerfile? A Dockerfile is a step-by-step recipe of instructions telling Docker how to assemble a custom container image from scratch.
  • Difference between image: httpd:2.4 and build: .: Using image: httpd:2.4 tells Docker Compose to pull a ready-made image directly from Docker Hub. Using build: . tells Docker Compose to look for a Dockerfile in the current folder (.) and build a brand-new custom image for our own application code.

2. Creating the Workspace & Dependencies

I created a fresh Day5 folder and added requirements.txt containing flask==3.0.0 and mysql-connector-python==8.3.0 (the official driver allowing Python to communicate with MySQL):

mkdir Day5 && cd Day5 nano requirements.txt 

3. Building the Python Application (app.py)

Next, I created app.py to serve a web route, connect to MySQL using environment variables, run a SQL query, and display the live response in the browser:

  • @app.route("/"): Maps the root URL (/) of the web server to the home() function directly below it so visiting http://localhost:5000 triggers that function.
  • os.environ.get(...): Reads environment variables (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) passed into the container at runtime so credentials aren't hardcoded in source code.
  • mysql.connector.connect(...): Opens a live network connection from the Python container to the MySQL database container using those credentials.
  • cursor.execute(...): Sends a SQL query (SELECT 'Connected to MySQL successfully!' AS message) over the connection to be executed by MySQL.
  • app.run(...): Starts the Flask web server listening on 0.0.0.0 (all network interfaces inside the container) on port 5000.

4. Building the Custom Dockerfile

I created a Dockerfile to package my Python app and its dependencies into a portable image:

FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 5000 CMD ["python", "app.py"] 
  • FROM: Sets the base image (python:3.11-slim), giving the container a pre-installed Python environment.
  • WORKDIR: Sets /app as the active working directory inside the container for all following commands.
  • COPY: Copies files (requirements.txt first, then the rest of the project files .) from my local machine into /app inside the image.
  • RUN: Executes a shell command during the image build process (pip install -r requirements.txt) to bake the dependencies into the image.
  • EXPOSE: Documents that the container listens on network port 5000 at runtime.
  • CMD: Defines the default command (python app.py) that runs automatically when the container starts.

5. Orchestrating with docker-compose.yml

I brought both the custom app service and the official database service together in docker-compose.yml:

services: app: build: . ports: - "5000:5000" environment: DB_HOST: database DB_USER: root DB_PASSWORD: mypassword DB_NAME: mydb depends_on: - database database: image: mysql:8.0 environment: MYSQL_ROOT_PASSWORD: mypassword MYSQL_DATABASE: mydb volumes: - db-data:/var/lib/mysql volumes: db-data: 

Image

6. Understanding Environment Variables

  • Where does the Python application get these values? From the container's runtime environment via os.environ.get(...).
  • Where are they defined in Compose? Under the environment: section of the app service in docker-compose.yml.
  • Why is database used as the database hostname? Because in docker-compose.yml, the MySQL service is named database, and Docker's internal DNS automatically resolves service names to their container IP addresses.
  • Why don't you need to find the database container's IP address? Container IPs are assigned dynamically and change whenever containers recreate; Docker Compose's built-in DNS lets containers reach each other reliably by service name.

7. Giving the Database a Permanent Home (Named Volumes)

  • What is a named Volume? A persistent storage volume created and managed directly by Docker (db-data) that lives independently of any single container's lifecycle.
  • Where does MySQL store its data? Inside the container at /var/lib/mysql.
  • Why is a named Volume useful for databases? Databases store critical state; mounting a named volume ensures data stays safe, fast, and managed by Docker rather than trapped inside a temporary container layer.
  • What happens to your database when the container is removed? The database files remain completely intact inside the db-data volume and automatically reattach when a new MySQL container starts.
  • Comparing .:/app vs. db-data:/var/lib/mysql: .:/app is a bind mount linking a specific host directory (.) into the container (great for live code editing), whereas db-data:/var/lib/mysql is a named volume managed in Docker's internal storage area (ideal for database persistence).

8. Building & Starting Everything (docker compose up -d --build)

Predictions Before Running

  • How many containers will be created? Two (day5-app-1 and day5-database-1).
  • Which image will Docker build? The custom Python web application image (day5-app) from my Dockerfile.
  • Which image will Docker download? python:3.11-slim (for the base build stage) and mysql:8.0 (if not already cached locally).
  • What network will be created? day5_default.
  • What volume will be created? day5_db-data.

Running & Verifying the Application

docker compose up -d --build docker compose ps docker network ls docker volume ls 

Image

Image

Opening http://localhost:5000 in my browser confirmed that Flask connected to MySQL and executed the live SQL query:

Image

9. Discovering Container-to-Container Communication

  • What network are they using? day5_default (verified via docker network ls and docker inspect).
  • What is the MySQL service name? database.
  • How does Python find MySQL? Python reads DB_HOST=database and queries Docker's embedded DNS server (127.0.0.11) inside the day5_default bridge network.
  • Why can it use database instead of an IP address? Docker Compose automatically registers every service name as a DNS alias on the shared bridge network, routing traffic directly to the active day5-database-1 container.

10. Clean Up (docker compose stop vs. docker compose down)

  • Difference between docker compose stop and docker compose down: docker compose stop halts running containers without deleting them or the network. docker compose down stops and removes the containers and the day5_default network, while still preserving our named volume (day5_db-data) unless --volumes (-v) is explicitly added.
docker compose down docker compose ps -a docker volume ls 

Image

原文:https://www.a8q9.cn/en/article/build-a-real-python-application-d3cce4.html

相关文章