docker-compose.yml
配置文件:docker-compose.yml
是 Docker Compose 使用的配置文件,用于定义和管理多个容器化应用的部署。
通过这个文件,用户可以描述容器服务、网络、数据卷等,
可以批量构建镜像,
可简化容器的启动、停止和管理流程。
使用例子
假设当前有以下目录结构
- src
- apps
- api
- deployments
- docker-compose.yml
- scripts
- build.sh
- Dockerfile
比如我现在有这样一个文件:docker-compose.yml
version: "3.9"
services:
api:
image: ${IMAGE_PREFIX}/api:${TAG}
build:
context: ../
dockerfile: ./scripts/Dockerfile
args:
...
此时在scripts目录下,
使用 docker-compose 指令即可构建一系列镜像
docker-compose -f ../deployments/docker-compose.yml build
在正确的配置 image 键后,也可将构建完成的镜像推送至镜像仓库中
docker-compose -f ../deployments/docker-compose.yml push
将这两个指令写在 build.sh 中,运行该脚本即可构建、上传镜像。
顶层 Services 配置键
这是 services 下的顶层配置项,每个服务可以使用这些键来定义其行为:
如果仅用于构建镜像,那么使用 image、build 即可。
若需运行,则需使用更多 key 配置,如:
portsvolumesenvironmentnetworkscontainer_namedepends_on
services:
<service_name>:
image: # 使用的镜像
build: # 构建镜像相关
container_name: # 指定容器名称
command: # 覆盖默认命令
environment: # 设置环境变量
ports: # 端口映射
volumes: # 挂载卷
env_file: # 从文件加载环境变量
networks: # 服务连接到哪些网络
depends_on: # 定义服务之间的依赖
restart: # 容器的重启策略
deploy: # 定义服务的部署配置(仅用于 swarm)
links: # 服务之间的链接(过时)
logging: # 日志配置
healthcheck: # 健康检查
stdin_open: # 为容器打开 stdin
tty: # 为容器分配伪 TTY
extra_hosts: # 定义额外的主机名解析
security_opt: # 设置安全选项
cap_add: # 向容器添加 Linux 内核功能
cap_drop: # 从容器中移除 Linux 内核功能
user: # 设置容器用户
working_dir: # 容器内工作目录
volumes_from: # 从其他容器挂载卷
labels: # 定义标签
stop_grace_period: # 容器停止前的延迟时间
stop_signal: # 停止容器时发送的信号
Service 分层的配置
image
镜像
- 例如:官方镜像nginx
image: nginx:alpine - 例如:自有镜像仓库的镜像
前缀:register_url/其他 image: ${IMAGE_PREFIX}/<service>:${TAG}
build
用于定义构建镜像相关的配置:
build:
context: # Dockerfile 所在目录
dockerfile: # 指定 Dockerfile 文件名
args: # 构建时传递的构建参数
例子:
build:
context: ../
dockerfile: ./Dockerfile
args:
- svc:<service>
- version:${VERSION}
ports
宿主机和容器内端口的映射
ports:
- "8080:80"
- "3306:3306"
volumes
数据卷,用于持久化存储或挂载目录
volumes:
- ./data:/data
environment
容器内环境变量
environment:
- MYSQL_ROOT_PASSWORD=123
env_file
从文件内加载环境变量
env_file:
- .env
networks
定义容器连接的网络,是docker管理的network
networks:
- frontend
depends_on
定义服务依赖,确保某些服务先启动
depends_on:
- db
restart
定义容器重启策略:
- no:不重启(默认)。
- always:总是重启。
- on-failure:失败时重启。
- unless-stopped:除非手动停止,否则一直重启。
restart:always
logging
日志驱动和日志相关配置
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
healthcheck
健康监测
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 5
deploy
服务的部署选项(用于 docker swarm模式)
deploy:
replicas: 3 # 节点数量
update_config:
parallelism: 2
restart_policy:
condition: on-failure
labels
为容器和服务添加自定义标签
labels:
- "com.example.description=This is my service"
extra_hosts
将额外的主机名添加到容器的 /etc/hosts。
extra_hosts:
- "myhost:192.168.1.100"
security_opt
设置安全选项
- no-new-privileges:true
cap_add
向容器添加 Linux 内核功能
- NET_ADMIN
cap_drop
从容器中移除 Linux 内核功能
- MKNOD
user
设置容器用户
working_dir
容器内工作目录
- /app
volumes_from
从其他容器挂载卷
- data-container
stop_grace_period
容器停止前的延迟时间
- 30s
stop_signal
停止容器时发送的信号
SIGTERM: 由操作系统或其他进程发送的退出信号,表示需要优雅地关闭。SIGINT: 由用户发送的退出信号,表示需要立即关闭。Ctrl+C就是一个例子。SIGKILL: 由操作系统发送的强制终止信号,表示需要立即关闭。