一、简介
Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。
Compose 中有两个重要的概念:
服务 (service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
项目 (project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。
二、安装
Docker Desktop for Mac/Windows 自带 docker-compose 二进制文件,安装 Docker 之后可以直接使用。
$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01
1
2
3
$docker-compose--version
docker-composeversion1.23.2,build1110ad01
三、composer模板文件
编写一个最简单的模板
# docker-compose.yml
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
# docker-compose.yml
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
version 表示模板使用的规则版本号
services表示所有服务
webapp表示服务名称
image表示镜像,nginx:v3是我们之前构建的镜像
ports表示端口映射
启动服务
$ docker-compose up -d
1
2
$docker-composeup-d
-d 表示后台常驻进程方式运行
下面我们挑选几个进行演练。
build
指定 Dockerfile 所在文件夹的路径(可以是绝对路径,或者相对 docker-compose.yml 文件的路径)。 Compose 将会利用它自动构建这个镜像,然后使用这个镜像。
version: '3'
services:
webapp:
build: ./dir
1
2
3
4
5
6
version:'3'
services:
webapp:
build:./dir
也可以使用 context 指令指定 Dockerfile 所在文件夹的路径。
使用 dockerfile 指令指定 Dockerfile 文件名。
使用 arg 指令指定构建镜像时的变量。
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
1
2
3
4
5
6
7
8
9
10
version:'3'
services:
webapp:
build:
context:./dir
dockerfile:Dockerfile-alternate
args:
buildno:1
container_name
指定容器名称。默认将会使用 项目名称_服务名称_序号
version: "3"
services:
webapp:
container_name: my-ngxin
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
version:"3"
services:
webapp:
container_name:my-ngxin
image:nginx:v3
ports:
-"80:80"
查看服务
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19f8fdb1f76d nginx:v3 "nginx -g 'daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp my-ngxin
1
2
3
4
5
$dockerps
CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
19f8fdb1f76dnginx:v3"nginx -g 'daemon of…"4secondsagoUp3seconds0.0.0.0:80->80/tcpmy-ngxin
depends_on
解决容器的依赖、启动先后的问题。下面为例,会先启动redis,之后再启动webapp。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
depends_on:
- redis
redis:
image: redis:latest
1
2
3
4
5
6
7
8
9
10
11
12
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
depends_on:
-redis
redis:
image:redis:latest
env_file
从文件中获取环境变量,可以为单独的文件路径或列表。
如果通过 docker-compose -f FILE 方式来指定 Compose 模板文件,则 env_file 中变量的路径会基于模板文件路径。默认为.env模板。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
env_file: .env
redis:
image: redis:latest
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
env_file:.env
redis:
image:redis:latest
env_file:
-./common.env
-./apps/web.env
-/opt/secrets.env
environment
设置环境变量。你可以使用数组或字典两种格式。
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
environment:
RACK_ENV: development
SESSION_SECRET:
1
2
3
4
5
6
7
8
9
10
11
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
environment:
RACK_ENV:development
SESSION_SECRET:
如果变量名称或者值中用到 true|false,yes|no 等表达 布尔 含义的词汇,最好放到引号里,避免 YAML 自动解析某些内容为对应的布尔语义。这些特定词汇,包括
y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
1
2
y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
expose
暴露端口,但不映射到宿主机,只被连接的服务访问。
仅可以指定内部端口为参数
version: "3"
services:
webapp:
image: nginx:v3
expose:
- "3000"
ports:
- "80:80"
1
2
3
4
5
6
7
8
9
10
version:"3"
services:
webapp:
image:nginx:v3
expose:
-"3000"
ports:
-"80:80"
network_mode
设置网络模式。使用和 docker run 的 --network 参数一样的值。
network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"
1
2
3
4
5
6
network_mode:"bridge"
network_mode:"host"
network_mode:"none"
network_mode:"service:[service name]"
network_mode:"container:[container name/id]"
networks
配置容器连接的网络。
假设,在目录app下创建docker-compose.yml,内容如下:
version: "3"
services:
webapp:
image: nginx:v3
ports:
- "80:80"
1
2
3
4
5
6
7
8
version:"3"
services:
webapp:
image:nginx:v3
ports:
-"80:80"
使用docker-compose up启动容器后,这些容器都会被加入app_default网络中。使用docker network ls可以查看网络列表,docker network inspect可以查看对应网络的配置。
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
0a9fa5ffdf97 app_default bridge local
...
1
2
3
4
5
$dockernetworkls
NETWORKIDNAMEDRIVERSCOPE
0a9fa5ffdf97app_defaultbridgelocal
...
假设我们lnmp环境需要网络隔离,如下配置,nginx、mysql、redis都有独立的网络,相互隔离,php需要链接他们,则都可以所有网络都可以使用。
version: '3'
services:
nginx:
image: nginx:alpine
# ...
networks:
- net-php
php:
# ...
networks:
- net-php
- net-mysql
- net-redis
mysql:
image: mysql:5.7
# ...
networks:
- net-mysql
redis:
image: redis:latest
# ..
networks:
- net-redis
networks:
net-php:
net-mysql:
net-redis:
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
version:'3'
services:
nginx:
image:nginx:alpine
# ...
networks:
-net-php
php:
# ...
networks:
-net-php
-net-mysql
-net-redis
mysql:
image:mysql:5.7
# ...
networks:
-net-mysql
redis:
image:redis:latest
# ..
networks:
-net-redis
networks:
net-php:
net-mysql:
net-redis:
ports
暴露端口信息。
使用宿主端口:容器端口 (HOST:CONTAINER) 格式,或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
1
2
3
4
5
6
7
ports:
-"3000"
-"8000:8000"
-"49100:22"
-"127.0.0.1:8001:8001"
注意:当使用 HOST:CONTAINER 格式来映射端口时,如果你使用的容器端口小于 60 并且没放到引号里,可能会得到错误结果,因为 YAML 会自动解析 xx:yy 这种数字格式为 60 进制。为避免出现这种问题,建议数字串都采用引号包括起来的字符串格式。
secrets
存储敏感数据,例如 mysql 服务密码。
version: "3.1"
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
secrets:
- db_root_password
- my_other_secret
secrets:
my_secret:
file: ./my_secret.txt
my_other_secret:
external: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version:"3.1"
services:
mysql:
image:mysql
environment:
MYSQL_ROOT_PASSWORD_FILE:/run/secrets/db_root_password
secrets:
-db_root_password
-my_other_secret
secrets:
my_secret:
file:./my_secret.txt
my_other_secret:
external:true
volumes
数据卷所挂载路径设置。可以设置为宿主机路径(HOST:CONTAINER)或者数据卷名称(VOLUME:CONTAINER),并且可以设置访问模式 (HOST:CONTAINER:ro)。
该指令中路径支持相对路径。
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
1
2
3
4
5
volumes:
-/var/lib/mysql
-cache/:/tmp/cache
-~/configs:/etc/configs/:ro
如果路径为数据卷名称,必须在文件中配置数据卷。
version: "3"
services:
my_src:
image: mysql:8.0
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
1
2
3
4
5
6
7
8
9
10
11
version:"3"
services:
my_src:
image:mysql:8.0
volumes:
-mysql_data:/var/lib/mysql
volumes:
mysql_data:
本文参考地址:https://yeasy.gitbooks.io/docker_practice/compose/compose_file.html