docker-compose.yml 配置参考
Docker Compose 文件配置的完整参考。
基本结构
version: '3.8'
services: # 服务定义 web: # ...
networks: # 网络定义
volumes: # 卷定义服务配置
镜像相关
services: web: image: nginx:alpine # 使用镜像 build: . # 构建镜像 build: context: ./dir dockerfile: Dockerfile args: VERSION: "1.0"端口和网络
services: web: ports: - "8080:80" - "443:443" networks: - frontend - backend dns: - 8.8.8.8环境变量
services: web: environment: NODE_ENV: production PORT: 3000 env_file: - .env - .env.production数据卷
services: web: volumes: - ./src:/app/src # 绑定挂载 - data:/app/data # 命名卷 - /app/node_modules # 匿名卷依赖和启动顺序
services: web: depends_on: - db depends_on: db: condition: service_healthy重启策略
services: web: restart: always # always, on-failure, unless-stopped, no资源限制
services: web: deploy: resources: limits: cpus: '1' memory: 1G reservations: cpus: '0.5' memory: 512M健康检查
services: web: healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 30s timeout: 10s retries: 3 start_period: 40s日志配置
services: web: logging: driver: json-file options: max-size: "10m" max-file: "3"网络配置
networks: frontend: driver: bridge backend: internal: true卷配置
volumes: db-data: driver: local cache: external: true完整示例
version: '3.8'
services: web: build: context: . args: NODE_VERSION: 18 image: myapp:${VERSION:-latest} container_name: web-app restart: unless-stopped ports: - "3000:3000" environment: NODE_ENV: production DATABASE_URL: postgresql://user:pass@db:5432/mydb env_file: - .env volumes: - ./src:/app/src - node-modules:/app/node_modules networks: - frontend - backend depends_on: db: condition: service_healthy healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000/health"] interval: 30s timeout: 10s retries: 3 deploy: resources: limits: cpus: '1' memory: 1G logging: driver: json-file options: max-size: "10m" max-file: "3"
db: image: postgres:15-alpine environment: POSTGRES_PASSWORD: ${DB_PASSWORD} POSTGRES_DB: mydb volumes: - postgres-data:/var/lib/postgresql/data networks: - backend healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5
networks: frontend: driver: bridge backend: internal: true
volumes: node-modules: postgres-data: