Docker Compose Specification快速开始
笔记
最新版的 Docker Compose 不叫 V2 也不叫 V3,成为 Compose Specification
。
# 安装
安装了 docker 就有
# 查询版本
docker compose version
# 配置
推荐的文件名: compose.yaml
services:
project-front:
image: ccr.ccs.tencentyun.com/xxx/project-front:latest
ports:
- "8081:8081"
environment:
SERVER_PORT: 8081
project-admin:
image: ccr.ccs.tencentyun.com/duomu/xxx/project-admin:latest
ports:
- "8080:8080"
environment:
SERVER_PORT: 8080
- version:[可选],新版不再需要通过 version 判断版本。参考值
"3"
- services:必填,容器组,可以有多个子元素
- 自定义的容器名称:如
my-project-front
- image:镜像名
- 自定义的容器名称:如
# 挂载
Volumes | Docker Docs (opens new window)
services:
db:
image: mysql:latest
volumes:
- {宿主机绝对路径}:/var/lib/mysql
# 硬件限制
services:
project-front:
image: ccr.ccs.tencentyun.com/xxx/project-front:latest
ports:
- "8081:8081"
environment:
SERVER_PORT: 8081
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
# 健康检查
services:
my_service:
image: my_image
restart: always
ports:
- "8080:8080"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
# 组网
networks:
default:
external: true
name: safeline-ce
- name:为要加入的组网
# 常用命令
# 拉去镜像 再启动(常用于重新运行latest)
docker compose pull && docker compose up -d # 方法1
docker compose up -d --build --pull always # 方法2
# 强制重新创建
docker compose up --force-recreate -d
# 扩展:零停机部署
wowu/docker-rollout: 🚀 Zero Downtime Deployment for Docker Compose (opens new window)
# 安装
# Create directory for Docker cli plugins
mkdir -p ~/.docker/cli-plugins
# Download docker-rollout script to Docker cli plugins directory
curl https://raw.githubusercontent.com/wowu/docker-rollout/main/docker-rollout -o ~/.docker/cli-plugins/docker-rollout
# Make the script executable
chmod +x ~/.docker/cli-plugins/docker-rollout
# 配置
services:
nms-api:
image: nms-api:latest
# container_name: nms-api # 不能指定容器名
environment:
SERVER_PORT: 8080
VIRTUAL_HOST: nms-api.nipx.cn # 指定域名
VIRTUAL_PORT: 8080 # 指定端口
restart: always
healthcheck:
test: ["CMD-SHELL", "curl -f http://nms-api:8080 || exit 1"] # 这里要用nms-api:8080 而不是localhost:8080
interval: 30s
timeout: 10s
retries: 3
start_period: 30s
nginx-proxy:
image: nginxproxy/nginx-proxy
ports:
- "8090:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
# 参考
上次更新: 2025/03/01, 15:52:13