摘要
docker compose 是什么?
-
Docker Compose 用一份 YAML 描述多个服务、网络和卷。docker compose up 按这份文件创建并启动它们。同一网络里的服务用服务名互相访问。
-
换环境时改文件里的变量或换一份 env_file,服务定义可以保持不动。
-
把同一份文件交给 Swarm 时,build、depends_on 由 docker compose up 使用,deploy 由 docker stack deploy 使用。对照表见 Docker Swarm 之 栈(Stack)。
docker compose 安装
1 2
| sudo dnf install docker-compose-plugin -y docker compose version
|
docker compose 命令
| 命令 |
中文说明 |
示例 |
| attach |
连接到服务的运行中容器的标准输入、输出和错误流 |
docker compose attach web |
| build |
构建或重新构建服务 |
docker compose build |
| config |
解析并标准化 Compose 文件 |
docker compose config |
| cp |
在服务容器与本地文件系统之间复制文件/文件夹 |
docker compose cp web:/app/file.txt ./file.txt |
| create |
为服务创建容器,但不启动 |
docker compose create |
| down |
停止并移除容器、网络等资源 |
docker compose down |
| events |
实时接收容器事件 |
docker compose events |
| exec |
在运行中的容器中执行命令 |
docker compose exec web ls /app |
| images |
列出已创建容器所使用的镜像 |
docker compose images |
| kill |
强制停止服务容器 |
docker compose kill |
| logs |
查看服务容器的日志输出 |
docker compose logs |
| ls |
列出当前运行的 Compose 项目 |
docker compose ls |
| pause |
暂停服务容器 |
docker compose pause |
| port |
显示某端口映射的公网地址 |
docker compose port web 80 |
| ps |
列出服务的容器 |
docker compose ps |
| pull |
拉取服务使用的镜像 |
docker compose pull |
| push |
推送服务镜像到仓库 |
docker compose push |
| restart |
重启服务容器 |
docker compose restart |
| rm |
移除已停止的服务容器 |
docker compose rm |
| run |
在服务上运行一次性命令 |
docker compose run web echo Hello |
| scale |
扩展服务实例数量 |
docker compose up --scale web=3 |
| start |
启动已存在但已停止的服务容器 |
docker compose start |
| stats |
实时显示容器资源使用情况 |
docker compose stats |
| stop |
停止运行中的服务容器 |
docker compose stop |
| top |
显示容器内的运行进程 |
docker compose top |
| unpause |
取消暂停服务容器 |
docker compose unpause |
| up |
创建并启动服务容器 |
docker compose up |
| version |
显示 Docker Compose 版本信息 |
docker compose version |
| wait |
阻塞直到第一个服务容器停止 |
docker compose wait |
| watch |
监听服务构建上下文变更并重新构建/刷新容器 |
docker compose watch |
docker compose 常用命令
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
| docker compose up
docker compose up -d
docker compose -f docker-compose.yml up -d
docker-compose -f compose1.yml -f compose2.yml up -d
docker compose up service_name
docker compose up service_name1 service_name2
docker compose up --build
docker compose stop
docker compose kill
docker compose restart
docker compose rm -s
docker compose down
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| docker compose ls
docker compose ps -a
docker compose config
docker compose images
docker compose top
docker compose stats
docker compose logs -f
docker compose logs -f service_name
docker compose exec service_name bash
|
1 2 3 4 5 6
| docker compose down
docker compose pull
docker compose up -d --build
|
docker compose 默认文件名
1 2 3 4
| docker-compose.yml docker-compose.yaml compose.yml compose.yaml
|
docker-compose.yml 的语法
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
| name: project_name services: servicename: image: build: pull_policy: container_name: command: entrypoint: user: working_dir: environment: env_file: ports: expose: volumes: networks: network_mode: extra_hosts: dns: depends_on: restart: healthcheck: cpus: mem_limit: read_only: tmpfs: cap_drop: security_opt: logging: profiles: develop: volumes: networks:
|
volumes
1 2 3 4 5 6 7 8 9 10 11 12 13
| services: backend: image: example/database volumes: - db-data:/etc/data
backup: image: backup-service volumes: - db-data:/var/lib/backup/data
volumes: db-data:
|
driver
1 2 3 4
| volumes: db-data: driver: local db-data2:
|
driver_opts
1 2 3 4 5 6
| volumes: db-data: driver_opts: type: "nfs" o: "addr=10.40.0.199,nolock,soft,rw" device: ":/docker/example"
|
external
1 2 3
| volumes: db-data: external: true
|
name
1 2 3 4
| volumes: db-data: name: db-data external: true
|
labels
1 2 3 4 5 6
| volumes: db-data: labels: com.example.description: "Database volume" com.example.department: "IT/Ops" com.example.label-with-empty-value: ""
|
networks
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| services: frontend: image: example/webapp networks: - front-tier - back-tier db: image: postgres networks: - backend
networks: front-tier: back-tier:
|
1 2 3 4 5 6 7 8 9 10 11 12
| services: some-service: image: foo
services: some-service: image: foo networks: default: {} networks: default: {}
|
driver
1 2 3
| networks: gitea: driver: bridge
|
driver_opts
1 2 3 4 5
| networks: frontend: driver: bridge driver_opts: com.docker.network.bridge.host_binding_ipv4: "127.0.0.1"
|
driver_opts 常见配置项(针对 bridge 网络驱动)
选项键名(driver_opts) |
说明 |
com.docker.network.bridge.name |
指定桥接网络的名称(默认是随机生成,如 br-xxxxx) |
com.docker.network.bridge.enable_icc |
是否允许容器之间的通信(true 或 false) |
com.docker.network.bridge.enable_ip_masquerade |
是否启用 IP 假冒(NAT,通常用于外网访问) |
com.docker.network.bridge.host_binding_ipv4 |
设置容器端口绑定到主机的哪个 IP(如 "127.0.0.1",绑定到本地) |
com.docker.network.bridge.default_bridge |
是否将该网络设置为默认 bridge 网络(true 或 false) |
com.docker.network.driver.mtu |
设置网络的最大传输单元(MTU,例如 "1500") |
com.docker.network.bridge.allow_non_default_bridge |
是否允许容器加入非默认的 bridge 网络(较少使用) |
attachable
1 2 3
| networks: gitea: attachable: true
|
external
1 2 3
| networks: gitea: external: true
|
name
1 2 3 4
| networks: gitea: name: gitea external: true
|
labels
1 2 3 4 5 6
| networks: mynet1: labels: com.example.description: "Financial transaction network" com.example.department: "Finance" com.example.label-with-empty-value: ""
|
internal
1 2 3
| networks: gitea: internal: true
|
services
-
服务是应用程序中计算资源的抽象定义,可以独立于其他组件进行扩展或替换。服务由一组容器支持,由平台根据复制要求和布局约束运行。由于服务由容器支持,因此它们由 Docker 镜像和一组运行时参数定义。服务中的所有容器都使用这些参数以相同的方式创建。
-
service 包含的属性非常多,具体请参考services顶级元素,这里只介绍比较常用的属性,
-
这里以Gitea的docker-compose.yml为例
Gitea 是一种轻松、自托管的一体化软件开发服务。它包括 Git 托管、代码审查、团队协作、包注册表和 CI/CD。它类似于 GitHub、Bitbucket 和 GitLab。
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
|
networks: gitea: external: false driver: bridge
volumes: gitea: driver: local
services: server: image: gitea/gitea:latest environment: - USER_UID=1000 - USER_GID=1000 - DB_TYPE=mysql - DB_HOST=db:3306 - DB_NAME=gitea - DB_USER=gitea - DB_PASSWD=gitea restart: always networks: - gitea volumes: - gitea:/data - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro ports: - "3000:3000" depends_on: db: condition: service_healthy
db: image: mysql:8 environment: - MYSQL_ROOT_PASSWORD=gitea - MYSQL_USER=gitea - MYSQL_PASSWORD=gitea - MYSQL_DATABASE=gitea restart: always networks: - gitea volumes: - ./mysql:/var/lib/mysql healthcheck: test: ["CMD-SHELL", "mysql -u root -pgitea", "-e 'SELECT 1;'"] interval: 10s timeout: 5s retries: 3
|
build
1 2 3 4
| services: server: build: . image: example/webapp
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| services: server: build: context: ./webapp dockerfile: Dockerfile platforms: - linux/amd64 args: - FOO=bar labels: com.example.description: "Accounting webapp" com.example.department: "Finance" com.example.label-with-empty-value: ""
|
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
| services: server: image: example/webapp build: context: ./webapp dockerfile: Dockerfile target: prod pull: true no_cache: true network: host ssh: - default cache_from: - type=registry,ref=example/webapp:cache cache_to: - type=local,dest=./cache additional_contexts: resources: ./resources tags: - example/webapp:prod secrets: - server-certificate secrets: server-certificate: file: ./server.cert
|
image
1 2 3 4 5 6
| services: server: image: redis
|
container_name
1 2 3 4
| services: server: image: nginx container_name: gitea
|
command
1 2 3 4
| services: server: command: ["python", "app.py"] image: example/webapp
|
entrypoint
1 2 3 4 5 6 7 8 9 10 11 12
| services: server: image: example/webapp entrypoint: - php - -d - zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so - -d - memory_limit=-1 - vendor/bin/phpunit
|
depends_on
1 2 3 4 5 6 7 8 9 10
| services: web: build: . depends_on: - db - redis redis: image: redis db: image: postgres
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| services: web: build: . depends_on: db: condition: service_healthy restart: true redis: condition: service_started required: false redis: image: redis db: image: postgres
|
environment
1 2 3 4 5 6 7 8 9 10 11 12 13
| services: server: image: example/webapp environment: - FOO=bar - BAZ=qux
services: server: image: example/webapp environment: FOO: bar BAZ: qux
|
env_file
1 2 3 4 5 6
| services: server: image: example/webapp env_file: - ./a.env - ./b.env
|
1 2 3 4 5 6 7 8 9 10
| services: server: image: example/webapp env_file: - path: ./default.env required: true - path: ./override.env required: false - path: ./raw.env format: raw
|
restart
1 2 3 4 5 6 7 8
| services: server: image: example/webapp restart: "no"
|
healthcheck
1 2 3 4 5 6 7 8 9 10
| services: server: image: example/webapp healthcheck: test: ["CMD", "curl", "-f", "http://localhost"] interval: 1m30s timeout: 10s retries: 3 start_period: 40s start_interval: 5s
|
test定义Compose运行的命令来检查容器运行状况。它可以是字符串,也可以是列表。如果是列表,第一个项目必须是NONE、CMD或CMD-SHELL。如果它是一个字符串,它等同于指定CMD-SHELL后跟该字符串。
1 2 3 4
| test: ["CMD", "curl", "-f", "http://localhost"]
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"] test: curl -f https://localhost || exit 1
|
test 中的 CMD 和 CMD-SHELL 是两种不同的执行方式,它们的主要区别在于:
| 项目 |
CMD |
CMD-SHELL |
| ✅ 用途 |
直接执行命令(不通过 shell) |
通过 shell(如 /bin/sh -c)执行命令 |
| 🧾 写法格式 |
["CMD", "executable", "arg1", "arg2"] |
["CMD-SHELL", "command string"] |
| 🔧 是否使用 shell |
否 |
是 |
| 🧠 是否支持 shell 语法 |
❌ 否 (不能使用 &&、 ||、\$VAR 等) |
✅ 是 (支持管道、重定向、变量、命令组合) |
| 🛡️ 安全性/可移植性 |
✅ 更安全,执行更明确 |
⚠ 依赖容器中存在 shell(如 /bin/sh) |
| ⚙️ 执行效率 |
✅ 稍快,因无需 shell 解析 |
⚠ 稍慢,需通过 shell 启动 |
| 📦 推荐使用场景 |
- 简单健康检查命令 - 安全环境 - 精简镜像 |
- 需要使用逻辑控制(||, &&) - 复杂检查逻辑 |
| 📌 示例 |
["CMD", "curl", "-f", "http://localhost"] |
["CMD-SHELL", "curl -f http\://localhost || exit 1"] |
1 2 3 4 5 6
| services: server: image: example/webapp healthcheck: disable: true
|
volumes
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
| services: backend: image: example/backend volumes: - type: volume source: db-data target: /data volume: nocopy: true subpath: sub - type: bind source: /var/run/postgres/postgres.sock target: /var/run/postgres/postgres.sock
volumes: db-data:
services: backend: image: example/backend volumes: - db-data:/data - /var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock
volumes: db-data:
|
| 访问模式 |
作用 |
rw |
读写,省略时就是这个 |
ro |
只读。Gitea 示例里的 /etc/timezone:/etc/timezone:ro 就是这种写法 |
z |
SELinux 标记为多个容器共享。没有 SELinux 的系统会忽略 |
Z |
SELinux 标记为仅当前容器使用 |
type |
source |
volume |
顶层 volumes 里的名字 |
bind |
宿主机路径 |
tmpfs |
不写 source |
image |
镜像引用。子目录用 image.subpath,Compose 2.35.0 起 |
npipe |
Windows 命名管道的路径 |
cluster |
Swarm 集群卷的名字 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| services: backend: image: example/backend volumes: - type: bind source: ./config target: /config read_only: true bind: create_host_path: false propagation: rprivate selinux: Z - type: tmpfs target: /run tmpfs: size: 64m mode: 0o755 - type: image source: example/assets:1.0 target: /assets image: subpath: static
|
networks
1 2 3 4 5 6 7 8 9
| services: some-service: networks: - some-network - other-network
networks: some-network: other-network:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| services: backend: image: example/backend networks: back-tier: aliases: - database ipv4_address: 172.16.238.10 ipv6_address: 2001:3984:3989::10 networks: back-tier: ipam: config: - subnet: 172.16.238.0/24 - subnet: 2001:3984:3989::/64
|
| 字段 |
作用 |
aliases |
该网络上的额外主机名。同一个别名可以同时给多个容器,解析到哪一个并不固定 |
ipv4_address、ipv6_address |
加入该网络时使用的静态地址 |
mac_address |
这张网卡的 MAC 地址 |
link_local_ips |
链路本地地址列表 |
interface_name |
容器里这张网卡的接口名,例如 eth0 |
driver_opts |
传给该网络驱动的键值对 |
priority |
连接优先级,默认 0。服务级 mac_address 落在数值最大的那张网卡上 |
gw_priority |
默认网关选数值最大的那张网卡,默认 0 |
ports
1 2 3 4 5 6 7 8 9
| services: web: image: nginx ports: - "8080:80" - "127.0.0.1:8081:80" - "9090-9091:8080-8081" - "6060:6060/udp" - "3000"
|
| 片段 |
作用 |
| 宿主机地址 |
可省略。省略时绑定 0.0.0.0。IPv6 地址放在方括号里,如 [::1]:6001:6001 |
| 宿主机端口 |
可省略,或写成范围。省略时由引擎分配空闲端口 |
| 容器端口 |
必填,可以是单个端口或范围 |
| 协议 |
tcp 或 udp,默认 tcp |
1 2 3 4 5 6 7 8 9 10 11
| services: web: image: nginx ports: - name: web target: 80 published: "8080" host_ip: 127.0.0.1 protocol: tcp app_protocol: http mode: host
|
network_mode: host 的容器直接使用宿主机网络栈。这种服务再写 ports,运行时会报错。
expose
1 2 3 4 5 6
| services: web: image: nginx expose: - "80" - "8080-8085/tcp"
|
cpus、mem_limit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| services: backend: image: myapp:latest mem_limit: 1g mem_reservation: 512m cpus: 2.0 cpu_shares: 1024 cpu_quota: 100000 cpu_period: 100000
|
| 字段名 |
类型 |
说明 |
cpus |
float |
限制容器最多使用多少 CPU(逻辑核数);例如 0.5 = 一半 CPU。0.000 表示不限制 |
mem_limit |
string |
容器最大内存限制,支持单位:b、k、m、g(如 256m、1g) |
mem_reservation |
string |
容器启动时预留的内存,超过这个值不被硬性截断(只提示调度器) |
cpu_shares |
int |
CPU 权重(默认 1024),多个容器抢 CPU 时才比较这个值 |
cpu_quota |
int |
每个周期内允许使用的 CPU 时间(单位:微秒)。和 cpus 不要同时写 |
cpu_period |
int |
CPU 调度周期(单位:微秒,默认 100000)。和 cpus 不要同时写 |
cpuset |
string |
允许运行的逻辑 CPU,如 0-3 或 0,1 |
cpu_count |
int |
可用 CPU 个数 |
cpu_percent |
int |
可用 CPU 占本机的百分比 |
cpu_rt_runtime |
时长或微秒 |
实时调度下,一个周期内允许运行的时间 |
cpu_rt_period |
时长或微秒 |
实时调度周期 |
mem_swappiness |
int |
0 到 100,容器匿名页换出到交换分区的倾向 |
memswap_limit |
string |
内存和交换分区加在一起的上限。mem_limit: 300m 且 memswap_limit: 1g 时,内存最多 300m,交换分区最多 700m。两者相等时容器不能使用交换分区。设为 -1 时交换分区只受宿主机剩余空间限制 |
查看容器的 cpu 和 内存配置情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| docker inspect --format='{{json .HostConfig.CpuQuota}} {{json .HostConfig.CpuPeriod}}' <container_id>
docker inspect --format='{{json .HostConfig.Memory}}' <container_id>
docker inspect --format='{{json .HostConfig.CpuShares}}' <container_id>
docker inspect --format='{{json .HostConfig.NanoCpus}}' <container_id>
docker inspect <container_id> | jq '.[0].HostConfig | { cpus: (.NanoCpus / 1000000000), cpu_shares: .CpuShares, mem_limit: (.Memory / 1024 / 1024 | tostring + " MB"), mem_reservation: (.MemoryReservation / 1024 / 1024 | tostring + " MB"), cpu_quota: .CpuQuota, cpu_period: .CpuPeriod }'
|
user、working_dir、hostname、init
1 2 3 4 5 6 7
| services: web: image: example/webapp user: "1000:1000" working_dir: /app hostname: web init: true
|
stdin_open、tty
1 2 3 4 5
| services: web: image: example/webapp stdin_open: true tty: true
|
stop_signal、stop_grace_period
1 2 3 4 5
| services: web: image: example/webapp stop_signal: SIGTERM stop_grace_period: 30s
|
network_mode
| 值 |
作用 |
bridge |
接到 Docker 默认网桥。这条网络上不能用服务名互相解析 |
host |
直接使用宿主机网络栈,不要再写 ports |
none |
关闭容器网络 |
service:服务名 |
使用另一个 Compose 服务的网络栈 |
container:容器名 |
使用指定容器的网络栈 |
1 2 3 4
| services: web: image: nginx network_mode: host
|
1 2 3 4 5 6
| services: web: image: nginx extra_hosts: - "db.local=10.0.0.8" - "myhostv6=[::1]"
|
dns、dns_search、dns_opt
1 2 3 4 5 6 7 8 9 10
| services: web: image: nginx dns: - 8.8.8.8 - 9.9.9.9 dns_search: - example.com dns_opt: - no-tld-query
|
mac_address
1 2 3 4
| services: web: image: nginx mac_address: "02:42:ac:11:00:02"
|
privileged
1 2 3 4
| services: web: image: example/webapp privileged: true
|
cap_add、cap_drop
1 2 3 4 5 6 7
| services: web: image: example/webapp cap_drop: - ALL cap_add: - NET_BIND_SERVICE
|
security_opt
1 2 3 4 5 6
| services: web: image: example/webapp security_opt: - no-new-privileges:true - label=user:USER
|
devices
1 2 3 4 5 6 7
| services: web: image: example/webapp devices: - "/dev/ttyUSB0:/dev/ttyUSB0" - "/dev/sda:/dev/xvda:rwm"
|
group_add
1 2 3 4 5 6
| services: web: image: alpine group_add: - mail - "100"
|
gpus
1 2 3 4 5 6 7 8 9 10
| services: model: image: example/model gpus: all
|
read_only、tmpfs
1 2 3 4 5 6 7
| services: web: image: example/webapp read_only: true tmpfs: - /tmp:rw,noexec,nosuid,size=16m - /run:mode=755,uid=1000,gid=1000
|
| 选项 |
作用 |
rw |
这块内存文件系统可读写 |
noexec |
不能在这个目录里执行程序 |
nosuid |
忽略这里文件上的 setuid、setgid |
size |
容量上限,写成 size=16m。m 表示 MiB,还可以用 k、g |
mode |
目录权限,如 mode=755 |
uid、gid |
属主的用户号和组号 |
| 值 |
作用 |
missing |
本机没有这张镜像才拉取。未使用 Build Specification 时的默认值。if_not_present 是它的旧名。标签为 latest 时仍然每次都拉 |
always |
每次都从仓库拉取 |
never |
只使用本机镜像。本机没有就失败 |
build |
构建镜像。本机已经有也重新构建 |
daily |
距离上次拉取超过 24 小时才检查仓库 |
weekly |
距离上次拉取超过 7 天才检查仓库 |
every_<时长> |
距离上次拉取超过这个时长才检查。单位可以是 w、d、h、m、s,例如 every_12h |
1 2 3 4 5
| services: web: image: nginx pull_policy: every_12h platform: linux/amd64
|
profiles
1 2 3 4 5
| services: phpmyadmin: image: phpmyadmin profiles: - debug
|
1 2
| docker compose --profile debug up -d
|
scale
1 2 3 4
| services: web: image: nginx scale: 3
|
extends
1 2 3 4 5 6 7
| services: web: extends: file: common.yml service: webapp environment: PORT: "8080"
|
| 合并方式 |
适用情况 |
| 按键覆盖 |
environment、labels、sysctls、healthcheck 这类映射。当前文件里出现的键覆盖被引用服务的同名键 |
| 列表拼接 |
ports、expose、cap_add、secrets 等。被引用的项在前,当前文件的项在后,重复项去掉 |
| 标量取当前文件 |
image、user 这类单值。当前文件写了就用当前文件的 |
shm_size
1 2 3 4
| services: web: image: example/webapp shm_size: 1gb
|
ulimits
1 2 3 4 5 6 7 8
| services: web: image: example/webapp ulimits: nproc: 65535 nofile: soft: 20000 hard: 40000
|
pids_limit
1 2 3 4
| services: web: image: example/webapp pids_limit: 100
|
sysctls
1 2 3 4 5 6
| services: web: image: nginx sysctls: net.core.somaxconn: 1024 net.ipv4.tcp_syncookies: 0
|
pid
1 2 3 4 5
| services: web: image: nginx pid: "host"
|
labels
1 2 3 4 5 6
| services: web: image: nginx labels: com.example.description: "web" com.example.department: "ops"
|
logging
1 2 3 4 5 6 7 8
| services: web: image: nginx logging: driver: json-file options: max-size: "10m" max-file: "3"
|
secrets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| services: web: image: example/webapp secrets: - server-certificate - source: my-token target: token uid: "103" gid: "103" mode: 0o440 secrets: server-certificate: file: ./server.cert my-token: environment: MY_TOKEN
|
configs
1 2 3 4 5 6 7 8 9 10 11 12
| services: web: image: nginx configs: - source: nginx_conf target: /etc/nginx/nginx.conf uid: "103" gid: "103" mode: 0o440 configs: nginx_conf: file: ./nginx.conf
|
pre_start
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| services: app: image: example/webapp depends_on: db: condition: service_healthy volumes: - data:/data pre_start: - command: ["./manage.py", "migrate"] - image: busybox command: sh -c 'chown -R 1000:1000 /data' user: root db: image: postgres:16 volumes: data:
|
post_start
pre_stop
1 2 3 4 5 6 7 8 9 10 11 12
| services: web: image: example/webapp post_start: - command: ./on_start.sh user: root privileged: true working_dir: /app environment: - FOO=BAR pre_stop: - command: ./on_stop.sh
|
develop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| services: web: image: example/webapp build: . develop: watch: - action: sync path: ./web target: /app/web ignore: - node_modules/ - action: rebuild path: package.json - action: sync+restart path: ./assets target: /app/assets
|
action |
作用 |
sync |
把 path 同步到容器内的 target |
rebuild |
重新构建镜像并重建容器 |
restart |
重启容器 |
sync+restart |
先同步到 target,再重启容器 |
docker run 转 docker compose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| npm install composerize -g
composerize docker run -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro --restart always --log-opt max-size=1g nginx
name: <your project name> services: nginx: ports: - 80:80 volumes: - /var/run/docker.sock:/tmp/docker.sock:ro restart: always logging: options: max-size: 1g image: nginx
|
docker compose 编排管理工具 Dockge
| 功能模块 |
主要描述 |
| Compose 堆栈全生命周期管理 |
可以创建、编辑、启动、停止、重启、删除基于 compose.yaml 文件的 Docker 堆栈 |
| 交互式 Compose 编辑器 + Web 终端 |
实时编辑 YAML 并查看输出,还可以直接在浏览器中操作终端 |
将 docker run … 命令转换为 Compose 文件 |
快速生成 compose.yaml,便于版本控制和结构管理 |
| 实时操作反馈 |
镜像拉取、堆栈启动/停止等操作均可实时查看进度 |
| 多主机代理支持(≥1.4.0) |
能在单一 UI 中管理多个 Docker 主机 |
| 高兼容性 & 安全设计 |
Compose 文件保存在本地,不会被系统托管,便于使用 CLI 或其他工具管理 |
1 2 3 4 5 6 7 8 9 10 11
| mkdir -p /opt/stacks /opt/dockge cd /opt/dockge
curl "https://dockge.kuma.pet/compose.yaml?port=5001&stacksPath=%2Fopt%2Fstacks" --output compose.yaml
docker compose up -d
|