1.compose模板文件组成
顶级层次,由6部分组成
1.1 version
填写版本号,一般不用写
1.2 services
定义服务名字
1.3 networks
定义网络
1.4 volumes
定义存储
1.5 configs
1.6 secrets
2. compose语法
用空格来控制格式,空出3个空格
2.1 services
image
yaml
services:
web:
image: hello-world
services:
web:
image: hello-world
在 services 标签下的第二级标签是 web,这个名字是用户自己自定义,它就是服务名称。 image 则是指定服务的镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像
container_name
yaml
services:
web:
image: hello-world
container_name: vector
services:
web:
image: hello-world
container_name: vector
hostname
yaml
services:
web:
image: hello-world
container_name: vector
hostname: vector
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart
描述:启动策略
restart
defines the policy that the platform applies on container termination.
no
: The default restart policy. It does not restart the container under any circumstances.always
: The policy always restarts the container until its removal.on-failure[:max-retries]
: The policy restarts the container if the exit code indicates an error. Optionally, limit the number of restart retries the Docker daemon attempts.unless-stopped
: The policy restarts the container irrespective of the exit code but stops restarting when the service is stopped or removed.
yaml
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
ports
描述:启动映射外部端口
yaml
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
ports:
- 8686:8686
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
ports:
- 8686:8686
ports 其它写法
yaml
ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "8000-9000:80"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"
- "6060:6060/udp"
ports:
- "3000"
- "3000-3005"
- "8000:8000"
- "9090-9091:8080-8081"
- "49100:22"
- "8000-9000:80"
- "127.0.0.1:8001:8001"
- "127.0.0.1:5000-5010:5000-5010"
- "6060:6060/udp"
volumes
yaml
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
ports:
- 8686:8686
volumes:
- /var/log/nginx:/nginx_logs # 这是需要采集的日志的路径需要挂载到容器内
services:
web:
image: hello-world
container_name: vector
hostname: vector
restart: always
ports:
- 8686:8686
volumes:
- /var/log/nginx:/nginx_logs # 这是需要采集的日志的路径需要挂载到容器内