简介
引用 菜鸟教程
引用 csdn
Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。
Compose 使用的三个步骤:
- 使用 Dockerfile 定义应用程序的环境。
- 使用 docker-compose.yml 定义构成应用程序的服务,这样它们可以在隔离环境中一起运行。
- 执行 docker-compose up 命令来启动并运行整个应用程序。
安装
docker-compose与docker关系
引用docker官网
Compose file format |
Docker Engine release |
Compose specification |
19.03.0+ |
3.8 |
19.03.0+ |
3.7 |
18.06.0+ |
3.6 |
18.02.0+ |
3.5 |
17.12.0+ |
3.4 |
17.09.0+ |
3.3 |
17.06.0+ |
3.2 |
17.04.0+ |
3.1 |
1.13.1+ |
3.0 |
1.13.0+ |
2.4 |
17.12.0+ |
2.3 |
17.06.0+ |
2.2 |
1.13.0+ |
2.1 |
1.12.0+ |
2.0 |
1.10.0+ |
群晖的7.2使用的对应版本为
|Compose file format |Docker Engine release|
|v2.9.0|20.10.23|
yaml 格式
yaml格式一般缩进两个字符,且不支持Tab符号
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| version: '3' services: web: container_name: my-web-container labels: description1: description1 description2: description2 image: nginx:latest build: .
volumes: - "/docker/a:/config" - "logvolume01:/var/log" volumes: - type: volume source: ngx-cfg-data target: /etc/nginx volume: nocopy: true - type: bind source: /home/static target: /usr/share/nginx/html environment: DEV_ENV: test SESSION_SECRET: environment: - DEV_ENV=test - SESSION_SECRET network_mode: host ports: - "5000:5000" expose: - "80" - "443" networks: - network-1 extra_hosts: - "registry.yun-ti.com:192.168.0.50" - "code.yun-ti.com:192.168.0.50" depends_on: - db - redis links: - redis cap_add: - ALL cap_drop: - SYS_PTRACE cgroup_parent: m-executor-abcd command: ["bundle", "exec", "thin", "-p", "3000"] ulimits: nproc: 65535 nofile: soft: 65535 hard: 65535 sysctls: net.core.somaxconn: 10240 net.ipv4.tcp_syncookies: 0 sysctls: - net.core.somaxconn=10240 - net.ipv4.tcp_syncookies=0
redis: image: redis volumes: ngx-cfg-data: mysql-data networks: network-1: driver: bridge network-2: driver: host
|
运行
docker-compose up -d
以依赖性顺序启动服务。
docker-compose up <SERVICE>
自动包含 的依赖项。
docker-compose stop
停止容器服务