1. Docker Compose vs Docker Swarm
1.1 Basic Concepts and Positioning
Docker Compose is a tool used to define and run multi-container Docker applications.
It manages application service orchestration through a YAML configuration file and is mainly designed for single-host environments. It is especially suitable for local development and small-scale deployment scenarios.
Docker Swarm is Docker’s native container orchestration platform.
It allows multiple Docker hosts to form a cluster and provides cross-node container scheduling, load balancing, and high availability. Its goal is to simplify container orchestration complexity in production environments.
1.2 Architectural Differences
| Feature | Docker Compose | Docker Swarm |
|---|---|---|
| Deployment Scope | Single-host deployment | Multi-host cluster |
| Configuration Method | Based on a single YAML configuration file | Cluster configuration and service definitions |
| Startup Command | docker-compose up |
docker service create/update |
| Network Communication | Relies on host-level port mapping and container links | Built-in overlay network and service discovery |
| Scaling Capability | Manual scaling | Automated scaling and load balancing |
| Node Management | No node concept | Manager nodes handle task scheduling and cluster management |
| Workload Execution | Runs containers directly | Worker nodes execute container workloads |
| High Availability | No built-in high availability | Built-in HA and failure recovery |
| Consensus Algorithm | None | Uses the Raft consensus algorithm to maintain cluster state |
| Update Mechanism | Restart containers | Supports rolling updates and service scaling |
| Use Cases | Rapid prototyping and local testing | Production environments and large-scale deployment |
2. Personal Project Deployment Scenario
Recommended: Docker Compose
For personal projects, Docker Compose is recommended because it is more friendly in terms of resource consumption, maintenance cost, and learning curve.
With a single YAML file, you can manage the entire application stack and complete deployment within minutes without requiring professional DevOps skills. It consumes fewer system resources and can fully meet the small-scale load requirements of personal projects, making it the most cost-effective container orchestration solution.
3. Docker Deployment Architecture Design for Personal Projects
3.1 Configuration File Explanation — Key docker-compose.yml Configuration
version: '3.8'
services:
backend:
image: registry.example.com/my-app/backend:latest
container_name: my-app-backend
ports:
- "8000:8000"
environment:
# Centralized environment configuration
- PYTHONUNBUFFERED=1
- PYTHONWARNINGS=ignore::DeprecationWarning
- LOG_LEVEL=INFO
- APP_ENV=prod
# Database configuration
- DATABASE_URL=${DATABASE_URL}
- REDIS_URL=${REDIS_URL}
# JWT configuration
- SECRET_KEY=${SECRET_KEY}
# Third-party service configuration
- OPENAI_API_KEY=${OPENAI_API_KEY}
- OPENAI_BASE_URL=${OPENAI_BASE_URL:-https://api.openai.com/v1}
# Enable scheduled jobs
- ENABLE_SCHEDULED_JOBS=true
volumes:
# Data persistence
- ./backend/uploads:/app/uploads
- ./backend/logs:/app/logs
command: python run_server.py
restart: always
deploy:
restart_policy:
condition: on-failure
delay: 60s
networks:
- app-network
frontend:
image: registry.example.com/my-app/frontend:latest
container_name: my-app-frontend
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- REACT_APP_API_URL=http://backend:8000 # Service-to-service communication
- REACT_APP_TITLE=My Personal Project
depends_on:
- backend # Service dependency management
restart: always
deploy:
restart_policy:
condition: on-failure
delay: 60s
networks:
- app-network
networks:
app-network:
driver: bridge # Internal network isolation
3.2 Detailed Script Overview
docker-compose.yml – The core file for defining service orchestration, including:
- Builder stage: Installs dependencies (using the
uvpackage manager) and builds the virtual environment. - Runner stage: Copies code and the virtual environment, builds the final runtime image, sets environment variables, and starts the main program.
Dockerfile.base – Configuration file for building the base image:
- Builds the backend application image based on a configurable base image (image URL, name, tag).
- Copies project code into the container.
- Sets the container to run a specified script on startup.
Dockerfile – Builds the final image and defines two main services:
- Backend service: Includes the API service and scheduled jobs (jobs are integrated into the main application).
- Frontend service: React frontend application.
3.3 Execution Order
When using for the first time during development or deployment:
- Build the base image using
Dockerfile.base. - Build the application image using
Dockerfile. - Start the services using
docker-compose.yml.
When images already exist:
- Simply run
docker-compose.yml; it will automatically pull the images (if needed) and run the containers.