Compose是一个用于定义和运行多容器Docker应用程序的工具。使用Compose,您可以使用YAML文件来配置应用程序的服务。然后,只需一个命令,就可以从配置中创建并启动所有服务。
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
$ sudo rm /usr/local/bin/docker-compose
用Python/ target=_blank class=infotextkey>Python构建一个简易网页统计网页点击量,docker-compose进行发布
Step1:创建项目
flask
redis
Step2:创建Dockerfile文件
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Step3:在docker-compose.yml中定义services
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Step4:用Docker compose构建和运行app
Step5:绑定一个数据卷
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
environment:
FLASK_ENV: development
redis:
image: "redis:alpine"
将当前目录与容器的/code目录绑定,这样可以动态修改代码
Step6:重新构建和运行app
先docker-compose down停止服务,在构建
$ docker-compose down
$ docker-compose up
用YAML文件定义服务,默认文件是docker-compose.yml,包含4个顶级key,version、services.NETworks、volumes
参考compose-spec/spec.md at master · compose-spec/compose-spec · GitHub
指定本 yml 依从的 compose版本
定义多个应用服务,包含环境配置、镜像构建等
指定构建镜像的路径
version: "3.9"
services:
webapp:
build: ./app
定义服务的block IO配置,参考compose-spec/spec.md at master · compose-spec/compose-spec · GitHub
指定自定义容器名称
定义服务间启动或关闭的依赖关系
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
覆盖容器启动的默认命令
command: [ "bundle", "exec", "thin", "-p", "3000" ]
domainname declares a custom domain name to use for the service container.
覆盖容器默认的entrypoint
从文件中添加环境变量到容器,可以是一个或多个文件
env_file: .env
env_file:
- ./a.env
- ./b.env
文件格式:
# Set Rails/Rack environment
RACK_ENV=development
VAR="quoted"
添加环境变量
environment:
RACK_ENV: development
SHOW: "true"
USER_INPUT:
暴露端口,但不映射到宿主机,只被连接的服务访问,仅可以指定内部端口
expose:
- "3000"
- "8000"
用于检测 docker 服务是否健康运行。
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"] # 设置检测程序
interval: 1m30s # 设置检测间隔
timeout: 10s # 设置检测超时时间
retries: 3 # 设置重试次数
start_period: 40s # 启动后,多少秒开始启动检测程序
指定容器运行的镜像
image: redis:5
设置容器标签
labels:
com.example.description: "Accounting webapp"
com.example.department: "Finance"
labels:
- "com.example.description=Accounting webapp"
- "com.example.department=Finance"
连接到另一个容器的网络,简单将就是让容器相互连通
web:
links:
- db
- db:database
- redis
服务的日志记录配置,driver:指定服务容器的日志记录驱动程序,默认值为json-file。有以下三个选项
driver: "json-file"
driver: "syslog"
driver: "none"
仅在 json-file 驱动程序下,可以使用以下参数,限制日志得数量和大小。
logging:
driver: json-file
options:
max-size: "200k" # 单个文件大小为200k
max-file: "10" # 最多10个文件
syslog 驱动程序下,可以使用 syslog-address 指定日志接收地址。
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
设置网络模式,格式如下:
network_mode: "bridge" #桥接模式
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
配置容器连接的网络
services:
some-service:
networks:
- some-network
- other-network
networks:
some-network:
# Use a custom driver
driver: custom-driver-1
other-network:
# Use a custom driver which takes special options
driver: custom-driver-2
services:
frontend:
image: awesome/webapp
networks:
- front-tier
- back-tier
monitoring:
image: awesome/monitoring
networks:
- admin
backend:
image: awesome/backend
networks:
back-tier:
aliases:
- database
admin:
aliases:
- MySQL
networks:
front-tier:
back-tier:
admin:
指定ip地址
services:
frontend:
image: awesome/webapp
networks:
front-tier:
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
networks:
front-tier:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
- subnet: "2001:3984:3989::/64"
端口映射,映射主机与容器端口,格式:Host:ontainer
ports:
- "5000:5000"
容器重启策略
restart: "no"
restart: always
restart: on-failure
restart: unless-stopped
存储敏感数据,比如密码
services:
frontend:
image: awesome/webapp
secrets:
- server-certificate
secrets:
server-certificate:
file: ./server.cert
将主机数据卷挂载到容器
services:
db:
image: postgres:latest
volumes:
- "/localhost/postgres.sock:/var/run/postgres/postgres.sock"
- "/localhost/data:/var/lib/postgresql/data"
覆盖容器工作目录
services:
backend:
image: awesome/database
volumes:
- db-data:/etc/data
backup:
image: backup-service
volumes:
- db-data:/var/lib/backup/data
volumes:
db-data:
services:
frontend:
image: awesome/webapp
networks:
- front-tier
- back-tier
networks:
front-tier:
back-tier:
driver: bridge
$ docker-compose --help
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f <arg>...] [--profile <name>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--profile NAME Specify a profile to enable
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information