配置文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| services: app: build: context: . dockerfile: Dockerfile restart: always volumes: - ./config:/config:ro - app-data:/data-test:rw ports: - "8080:80" - "3000" - "5000-6000" - "9090-9091:8080-8081" - "3031:3031/udp" - "127.0.0.1:8070:80" env_file: ./default.env environment: - ENV1=1 post_start: - command: chown -R /data 1001:1001 user: root pre_stop: - command: ./data_flush.sh annotations: com.example.foo: bar attach: true blkio_config: cpu_count: 4 cpu_precenet: 90 cpu_shares: 512 cpu_period: 100000 cpu_quota: 20 cpu_rt_runtime: 100ms cpu_rt_period: 1000000 cpus: 1.5 cpuset: "0,1,2-6" cap_add: - ALL cap_drop: cgroup: private cgroup_parent: m-executor-abcd command: ["start", "--heldess"] container_name: webapp delpoy: endpoint_mode: vip labels: com.example.description: "This is example." resources: limits: cpus: "1.5" memory: 2G develop: dns: - 1.1.1.1 - 8.8.8.8 dns_search: domainname: example.com entrypoint: expose: - "3000" external_links: - redis-2 extra_hosts: - "host.docker.internal:host-gateway" group_add: - mail hostname: "my-app" init: true links: - redis-3 logging: driver: "json-file" options: max-size: "10m" max-file: 3 mem_limit: "2G" network_mode: none networks: some-network: ipv4_address: 172.31.0.3 interface_name: eth3 aliases: - alias1 - alias3 other-network: aliases: - alias2 oom_kill_disable: false read_only: true tmpfs: - /tmp runtime: runc secrets: - server-certificate stdin_open: true tty: true ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000 user: www working_dir: /app
phpmyadmin: image: phpmyadmin:latest pull_policy: missing profiles: ["dev"] env_file: - path: ./default.env required: true - path: ./override.env required: false environment: PMA_HOST: db MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_ROOT_PASSWORD2: ports: - 8081:80 depends_on: db: condition: service_healthy restart: true required: true redis: condition: service_started db: image: mysql:lts volumes: - mysql-data:/var/lib/mysql:rw - ./init-scripts:/docker-entrypoint-initdb.d environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: example healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-pexample"] interval: 10s timeout: 5s retries: 5 start_period: 20s disable: false
redis: image: redis:latest volumes: - redis-data:/data:rw
redis-3: image: redis:latest networks: - redis-3-coust
volumes: app-data: driver: local redis-data: driver: local mysql-data: driver: local
networds: default: {} some-network: name: some-network ipam: driver: default config: subnet: 172.31.0.0/16 ip_range: 172.31.5.0/24 gateway: "172.31.5.1" internal: true other-network: driver: bridge driver_opts: com.docker.network.bridge.host_binding_ipv4: "127.0.0.1" enable_ipv4: true enable_ipv6: false external: true secrets: server-certificate: file: ./server.cert
|
注意事项
优先级 |
来源 |
🥇 1. CLI 中使用 -e 参数 |
docker compose run -e VAR=value |
🥈 2. Compose 文件中 environment 字段,并使用了变量插值 |
environment: - VAR=${VAR_FROM_SHELL_OR_ENV_FILE} |
🥉 3. Compose 文件中 environment 字段,直接赋值 |
environment: - VAR=value |
🏅 4. Compose 文件中的 env_file 字段 |
env_file: .env |
🏅 5. Dockerfile 中的 ENV 指令 |
ENV VAR=value |
compose 会自动加载同目录下的 .env
中的环境变量,但是仅用于在 compose.yml
文件内进行插值
depends_on 搭配组合
配置 |
依赖服务未定义/未启动 |
依赖服务存在但未 healthy |
依赖服务 healthy |
condition: service_healthy , required: true |
❌ 报错并阻止启动 |
⏳ 等待健康(阻塞) |
✅ 启动 |
condition: service_healthy , required: false |
⚠️ 仅警告,继续启动 |
⏳ 等待健康(阻塞) |
✅ 启动 |
logging 配置
日志驱动 |
必备配置 |
推荐配置 / 说明 |
示例 |
json-file |
无(默认) |
控制本地日志大小和轮转 |
logging: driver: "json-file" options: max-size: "10m" max-file: "3" compress: "true" |
syslog |
syslog-address |
可选:syslog-facility 、tag ;发送到远程 syslog |
logging: driver: "syslog" options: syslog-address: "tcp://192.168.0.10:514" syslog-facility: "daemon" tag: "myapp" |
journald |
无(依赖 systemd) |
可选:tag |
logging: driver: "journald" options: tag: "{{.Name}}" |
fluentd |
fluentd-address |
可选:fluentd-async-connect 、tag ;发送到 Fluentd 聚合 |
logging: driver: "fluentd" options: fluentd-address: "localhost:24224" fluentd-async-connect: "true" tag: "myapp.{{.Name}}" |
gelf |
gelf-address |
可选:gelf-compression-type 、tag ;发送到 Graylog |
logging: driver: "gelf" options: gelf-address: "udp://127.0.0.1:12201" gelf-compression-type: "gzip" tag: "myapp" |
awslogs |
awslogs-region , awslogs-group , awslogs-stream |
可选:awslogs-create-group |
logging: driver: "awslogs" options: awslogs-region: "us-east-1" awslogs-group: "/my/log/group" awslogs-stream: "myapp-stream" awslogs-create-group: "true" |
none |
无 |
不保存日志 |
logging: driver: "none" |