docker compose文件格式
docker compose使用的是yaml文件格式,后缀是 .yml
YAML基本规则
1、大小写敏感
2、使用缩进表示层级关系
3、禁止使用tab缩进,只能使用空格键
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
5、使用#表示注释
6、字符串可以不用引号标注
yaml中的三种数据结构
- map - 散列表
# 使用冒号(:)表示键值对,同一缩进的所有键值对属于一个map,示例:
age : 12
name : huang
# 也可以这样写
{age:12, name:huang}
# 使用json
{"age":12, "name":"huang"}
- list - 数组
# 使用连字符(-)表示:
# YAML表示
- a
- b
- 12
[a, b, 12]
# 使用json表示
["a", "b", 12]
- scalar - 纯量
字符串
- "wos字符串"
- 我是字符串
布尔值
- true
- false
整数
浮点数
NULL 使用 ~ 表示
如:
# 1
Websites:
YAML: yaml.org
Ruby: ruby-lang.org
Python: python.org
Perl: use.perl.org
# 使用json表示
{"Websites": {"YAML":"yaml.org ", "Ruby":"ruby-lang.org"}}
# 2
languages:
- Ruby
- Perl
- Python
- c
# 使用json表示
{"languages":["ruby", "perl", "python", "c"]}
# 3
-
- Ruby
- Perl
- Python
-
- c
- c++
- java
# 使用json表示
[["ruby", "perl", "python"], ["c", "c++", "java"]]
# 4
-
id: 1
name: huang
-
id: 2
name: liao
# 使用json表示
[{"id":1, "name":"huang"}, {"id":2, "name":"liao"}]
compose文件样例:
- compose版本号、服务标识符必须顶格写
- 属性名和属性值是以’: '(冒号+空格) 隔开
- 层级使用’ '(两个空格)表示
- 服务属性使用’ - '(空格空格-空格)来表示
version: '2' # compose 版本号
services: # 服务标识符
web1: # 子服务命名
image: nginx # 服务依赖镜像属性
ports: # 服务端口属性
- "9999:80" # 宿主机端口:容器端口
container_name: nginx-web1 # 容器命名
compose属性介绍
#镜像:
格式:
image: 镜像名称:版本号
举例:
image: nginx:latest
#容器命名:
格式:
container_name: 自定义容器命名
举例:
container_name: nginx-web1
#数据卷:
格式:
volumes:
- 宿主机文件:容器文件
举例:
volumes:
- ./linshi.conf:/nihao/haha.sh
#端口:
格式:
ports:
- "宿主机端口:容器端口"
举例:
ports:
- "9999:80"
#镜像构建:
格式:
build: Dockerfile 的路径
举例:
build: .
build: ./dockerfile_dir/
build: /root/dockerfile_dir/
#镜像依赖:
格式:
depends_on:
- 本镜像依赖于哪个服务
举例:
depends_on:
- web1
#networks:
加入指定网络,格式如下:
services:
some-service:
networks:
- some-network
- other-network