In modern application deployments, container orchestration tools have become an indispensable part. Docker Compose, as Docker's official stand-alone orchestration tool, simplifies the management of multi-container applications through declarative YAML files.
1. Docker Compose core advantages
1.1 Comparison with traditional Docker run
Docker Compose has significant advantages over directly using the docker run command:
Declarative configuration: All service parameters are centrally defined in the file
Dependency management: Automatically handle service startup sequence and network connections
Environment Isolation: Supports the definition of independent configurations for different environments (development/test/production)
One-click operation: Simplify the start, stop and rebuild process of multi-container applications
1.2 Typical application scenarios
Quickly build the development environment
Local testing of microservice architecture
Integration testing in CI/CD pipeline
Small and medium-sized production deployment
2. Environment preparation and installation
2.1 Docker Compose Installation
Install the latest version on Linux system:
sudo curl -L "/docker/compose/releases/download/v2.20.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
Verify installation:
docker-compose --version # The output should be similar: Docker Compose version v2.20.3
2.2 Project directory structure
Recommended standard directory layout:
nginx-project/ ├── ├── config/ │ ├── │ └── / │ └── ├── html/ │ └── └── logs/ ├── └──
3. Basic deployment practice
3.1 Minimization
version: "3.8" services: web: image: nginx:1.23-alpine ports: - "80:80" volumes: - ./html:/usr/share/nginx/html restart: unless-stopped
Start the service:
docker-compose up -d
3.2 Complete feature configuration example
version: "3.8" services: nginx: image: nginx:1.23-alpine container_name: production_nginx ports: - "80:80" - "443:443" volumes: - ./config/:/etc/nginx/:ro - ./config/:/etc/nginx/ - ./html:/usr/share/nginx/html - ./logs:/var/log/nginx - ./certs:/etc/ssl/certs environment: - TZ=Asia/Shanghai - NGINX_ENVSUBST_OUTPUT_DIR=/etc/nginx/ networks: - frontend deploy: resources: limits: cpus: "1.5" memory: 512M healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 30s timeout: 3s retries: 3 networks: frontend: driver: bridge attachable: true
4. Advanced configuration skills
4.1 Multi-environment configuration management
Use extends and multiple compose files to achieve environment differentiation:
:
services: nginx: image: nginx:${NGINX_VERSION:-1.23-alpine} volumes: - ${CONFIG_PATH:-./config}:/etc/nginx
:
version: "3.8" services: nginx: extends: file: service: nginx environment: - NGINX_ENV=production deploy: replicas: 3
Start production environment:
docker-compose -f -f up -d
4.2 Load balancing configuration
Example of deploying Nginx+PHP-FPM cluster:
services: nginx: image: nginx:1.23-alpine depends_on: - php volumes: - ./:/etc/nginx/ php: image: php:8.2-fpm deploy: replicas: 3 volumes: - ./php:/var/www/html redis: image: redis:7-alpine
4.3 Dynamic configuration generation
Combined with envsubst to implement template configuration:
services: nginx: environment: - BACKEND_SERVER=app:8080 volumes: - ./templates/:/etc/nginx/templates/
Template file example:
upstream backend { server ${BACKEND_SERVER}; }
V. Safety reinforcement plan
5.1 Non-root user running
services: nginx: user: "1000:1000" cap_drop: - ALL cap_add: - NET_BIND_SERVICE
5.2 Network security isolation
networks: frontend: internal: false backend: internal: true services: nginx: networks: - frontend - backend database: networks: - backend
5.3 Mirror Signature Verification
services: nginx: image: nginx@sha256:abc123... platform: linux/amd64
6. Best practices in production environment
6.1 Log Management Plan
services: nginx: logging: driver: "json-file" options: max-size: "10m" max-file: "3" compress: "true"
6.2 Monitoring integration
Prometheus monitoring configuration example:
services: nginx: labels: - "=true" - "=9113" volumes: - ./:/etc/nginx// prometheus: image: prom/prometheus ports: - "9090:9090"
6.3 Blue and green deployment implementation
services: nginx: deploy: update_config: parallelism: 2 delay: 10s order: start-first healthcheck: test: ["CMD-SHELL", "curl -sf http://localhost/health || exit 1"]
7. Troubleshooting and maintenance
7.1 Common diagnostic commands
Check service status:
docker-compose ps docker-compose top
Real-time log monitoring:
docker-compose logs -f --tail=100
Enter container debugging:
docker-compose exec nginx sh
7.2 Configuration verification process
# Check the syntaxdocker-compose config #Try runningdocker-compose up --dry-run # Rebuild servicedocker-compose up -d --force-recreate
Version 7.3 Upgrade Strategy
Back up existing configuration and data
Modify the mirrored version in the compose file
Stage rolling updates:
docker-compose pull docker-compose up -d --no-deps nginx
8. Performance optimization suggestions
8.1 Resource restriction configuration
services: nginx: deploy: resources: limits: cpus: "2" memory: 1G reservations: memory: 512M
8.2 Static resource cache
Optimized Nginx configuration:
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires 1y; add_header Cache-Control "public"; access_log off; }
8.3 Connection parameter tuning
environment: - NGINX_WORKER_PROCESSES=auto - NGINX_WORKER_CONNECTIONS=4096 - NGINX_KEEPALIVE_TIMEOUT=65
This is the end of this article about the detailed tutorial on using Docker Compose to deploy Nginx. For more related content on Docker Compose to deploy Nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!