Docker Compose快速指南

最小示例

1
2
3
4
5
6
version: '3.8'
services:
web:
image: nginx:alpine
ports:
- "80:80"

核心配置

镜像与构建

1
2
3
4
5
6
7
8
9
services:
web:
image: nginx:alpine
api:
build:
context: ./app
dockerfile: Dockerfile.prod
args:
- VERSION=1.0

端口与环境变量

1
2
3
4
5
6
7
8
9
10
11
services:
web:
ports:
- "80:80"
- "443:443"
- "8080:8000"
api:
environment:
- NODE_ENV=production
- DB_HOST=db
- DB_PASSWORD=${DB_PASSWORD}

卷挂载

1
2
3
4
5
6
7
8
services:
db:
volumes:
- db_data:/var/lib/postgresql/data
- ./config:/app/config
- ./static:/app/static:ro
volumes:
db_data:

服务依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
services:
web:
depends_on:
- db
- redis
db:
depends_on:
db:
condition: service_healthy
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5

重启策略

1
2
3
services:
web:
restart: unless-stopped

可选值:always / on-failure:3 / no

命令速查

注:新版Docker(v2+)使用 docker compose,旧版使用 docker-compose

后台启动服务

1
docker-compose up -d

重新构建并启动

1
docker-compose up --build

停止并删除服务

1
docker-compose down

同时删除卷

1
docker-compose down -v

重启服务

1
docker-compose restart

查看服务状态

1
docker-compose ps

实时查看日志

1
docker-compose logs -f

查看指定服务日志

1
docker-compose logs -f web

查看最近日志

1
docker-compose logs --tail 100

进入容器

1
docker-compose exec web sh

非交互执行命令

1
docker-compose exec -T web cat config.js

运行多个实例

1
docker-compose up -d --scale web=3

实战模板

Node.js + MongoDB

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
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- MONGODB_URI=mongodb://db:27017/myapp
volumes:
- .:/app
- /app/node_modules
depends_on:
db:
condition: service_healthy
command: npm run dev
db:
image: mongo:7
volumes:
- mongo_data:/data/db
healthcheck:
test: echo 'db.runCommand("ping").ok' | mongosh localhost:27017/test --quiet
interval: 10s
timeout: 5s
retries: 5
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- app
volumes:
mongo_data:

微服务基础架构

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
version: '3.8'
services:
gateway:
build: ./gateway
ports:
- "8080:8080"
user-service:
build: ./services/user
depends_on:
postgres:
condition: service_healthy
order-service:
build: ./services/order
depends_on:
postgres:
condition: service_healthy
postgres:
image: postgres:15-alpine
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
volumes:
postgres_data:

多环境配置

使用override(自动应用)

基础配置

1
2
3
4
5
6
# docker-compose.yml
services:
web:
image: myapp:v1
ports:
- "80:80"

开发覆写(自动应用)

1
2
3
4
5
6
# docker-compose.override.yml
services:
web:
volumes:
- ./src:/app/src
command: npm run dev

生产环境

1
2
3
4
5
# docker-compose.prod.yml
services:
web:
image: myapp:prod
restart: always

环境变量

.env文件

1
2
3
# .env
COMPOSE_PROJECT_NAME=myapp
DB_PASSWORD=secret123

启动

1
docker-compose up -d

常用技巧

命令别名

1
2
3
4
alias dc='docker-compose'
alias dcu='docker-compose up -d'
alias dcd='docker-compose down'
alias dcl='docker-compose logs -f'

重建服务

1
2
3
docker-compose down -v
docker-compose build --no-cache
docker-compose up -d

清理资源

1
2
docker-compose down -v --remove-orphans
docker system prune -f