Docker Config 教程
在集群环境中配置文件的分发,可以通过将配置文件放入镜像中、设置环境变量、挂载volume、挂载目录的方式,当然也可以通过 docker config 来管理集群中的配置文件,这样的方式也更加通用。
# docker config
基础命令
$ docker config --help Usage: docker config COMMAND Manage Swarm configs # 管理 集群 配置 Commands: create Create a config from a file or STDIN # 从文件或标准输入创建config inspect Display detailed information on one or more configs # 查看config详细信息 ls List configs # 查看config列表 rm Remove one or more configs #删除config
1
2
3
4
5
6
7
8
9
10
# 创建 config
从文件创建
$ vi default.conf server { listen 88; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } $ docker config create conf-1 default.conf
1
2
3
4
5
6
7
8
9
10
11
12从标准输入创建
$ echo "listen 80" | docker config create conf-2 -
1
# 查看 config
简单查看
$ docker config ls ID NAME CREATED UPDATED je21ykql9tzebr0j2v7ep0kat conf-1 7 minutes ago 7 minutes ago nvzeahpik5itq7mrvad08pap6 conf-2 7 seconds ago 7 seconds ago
1
2
3
4查看详细信息
$ docker config inspect conf-1 [ { "ID": "je21ykql9tzebr0j2v7ep0kat", "Version": { "Index": 170 }, "CreatedAt": "2021-12-08T22:12:31.543232369Z", "UpdatedAt": "2021-12-08T22:12:31.543232369Z", "Spec": { "Name": "conf-1", "Labels": {}, "Data": "c2VydmVyIHsKICAgIGxpc3RlbiAgICAgICA4ODsKICAgIHNlcnZlcl9uYW1lICBsb2NhbGhvc3Q7CgogICAgbG9jYXRpb24gLyB7CiAgICAgICAgcm9vdCAgIC91c3Ivc2hhcmUvbmdpbngvaHRtbDsKICAgICAgICBpbmRleCAgaW5kZXguaHRtbCBpbmRleC5odG07CiAgICB9Cn0K" } } ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16查看详细信息并使用base64解码
$ docker config inspect -f '{{json .Spec.Data}}' conf-1 | cut -d '"' -f2 | base64 -d server { listen 88; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } }
1
2
3
4
5
6
7
8
9
10
# 删除 config
删除
$ docker config rm conf-2 conf2
1
2
# demo
使用nginx镜像创建容器,并使用docker config
$ docker service create --name nginx-01 --config source=conf,target=/etc/nginx/conf.d/default.conf -p 90:88 nginx:latest pocy3ph88gy7ng9g2lbq9jvnw overall progress: 1 out of 1 tasks 1/1: running [==================================================>] verify: Service converged
1
2
3
4
5
6
7
上次更新: 2023/12/06, 22:30:32