docker学习(十三)docker config 的使用

目录

  • 前言
  • 一、docker config 命令
  • 二、docker config 使用


前言

在集群环境中配置文件的分发,可以通过将配置文件放入镜像中、设置环境变量、挂载volume、挂载目录的方式,当然也可以通过 docker config 来管理集群中的配置文件,这样的方式也更加通用。


一、docker config 命令

1、docker config

[root@swarm-master test]# docker config --htlp
unknown flag: --htlp
See 'docker config --help'.

Usage:	docker config COMMAND

Manage Docker 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

Run 'docker config COMMAND --help' for more information on a command.

2、 创建config

(1)从文件创建

创建文件

vi default.conf
[root@swarm-master test]# cat default.conf 
server {
    listen       88;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

创建config

[root@swarm-master test]# docker config create conf default.conf 
je21ykql9tzebr0j2v7ep0kat

查看config

[root@swarm-master test]# docker config ls
ID                          NAME                CREATED             UPDATED
je21ykql9tzebr0j2v7ep0kat   conf                5 minutes ago       5 minutes ago

(2)从标准输入创建

创建config

[root@swarm-master test]# echo "listen 80" | docker config create conf2  -
nvzeahpik5itq7mrvad08pap6

查看config

[root@swarm-master test]# docker config ls
ID                          NAME                CREATED             UPDATED
je21ykql9tzebr0j2v7ep0kat   conf                7 minutes ago       7 minutes ago
nvzeahpik5itq7mrvad08pap6   conf2               7 seconds ago       7 seconds ago

3、查看config详细信息

[root@swarm-master test]# docker config inspect conf
[
    {
        "ID": "je21ykql9tzebr0j2v7ep0kat",
        "Version": {
            "Index": 170
        },
        "CreatedAt": "2021-12-08T22:12:31.543232369Z",
        "UpdatedAt": "2021-12-08T22:12:31.543232369Z",
        "Spec": {
            "Name": "conf",
            "Labels": {},
            "Data": "c2VydmVyIHsKICAgIGxpc3RlbiAgICAgICA4ODsKICAgIHNlcnZlcl9uYW1lICBsb2NhbGhvc3Q7CgogICAgbG9jYXRpb24gLyB7CiAgICAgICAgcm9vdCAgIC91c3Ivc2hhcmUvbmdpbngvaHRtbDsKICAgICAgICBpbmRleCAgaW5kZXguaHRtbCBpbmRleC5odG07CiAgICB9Cn0K"
        }
    }
]

对conf进行base64解码

[root@swarm-master test]# docker config inspect -f '{{json .Spec.Data}}' conf | cut -d '"' -f2 | base64 -d
server {
    listen       88;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

4、删除secret

[root@swarm-master test]# docker config rm conf2
conf2

[root@swarm-master test]# docker config ls
ID                          NAME                CREATED             UPDATED
je21ykql9tzebr0j2v7ep0kat   conf                10 minutes ago      10 minutes ago

二、docker config 使用

1、使用nginx镜像创建容器

在conf配置中,将nginx的监听端口改成了88,替换掉nginx中的默认80端口的配置文件,创建service时,将容器内部端口88端口映射成主机上90端口

[root@swarm-master test]# 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 

2、测试

访问90端口,可以看到访问是成功的。

[root@swarm-master test]# curl http://127.0.0.1:90
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

你可能感兴趣的:(docker,docker)