directory search
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
characters

关于配置

Docker 17.06引入了swarm服务配置,它允许您在服务映像外部或运行容器中存储非敏感信息,例如配置文件。这允许您保持图像尽可能通用,而无需将配置文件绑定到容器或使用环境变量。

Config以类似于秘密的方式运行,不同之处在于它们没有在休息时加密,并且不使用RAM磁盘直接安装到容器的文件系统中。随时可以从服务添加或删除配置,并且服务可以共享配置。您甚至可以将配置与环境变量或标签结合使用,以获得最大的灵活性。

注意:Docker配置仅适用于群集服务,而不适用于独立容器。要使用此功能,请考虑调整您的容器作为1级服务运行。

Linux和Windows服务都支持配置。

Docker如何管理配置

当您将配置添加到swarm中时,Docker会通过相互TLS连接将配置发送到swarm管理器。配置存储在加密的Raft日志中。整个Raft日志会在其他管理器中复制,从而确保配置的高可用性保证与其他群集管理数据相同。

当您授予新创建或正在运行的服务对配置的访问权时,配置将作为文件装载到容器中。容器中安装点的位置默认为/<config-name>在Linux容器中。在Windows容器中,配置都被装入C:\ProgramData\Docker\configs,符号链接被创建到所需的位置,默认为C:\<config-name>

您可以随时更新服务以授予其访问其他配置或撤销对给定配置的访问权限。

如果节点是群管理器,或者它正在运行已被授权访问配置的服务任务,则节点只能访问配置。当容器任务停止运行时,共享给它的配置将从该容器的内存中文件系统卸载并从节点的内存刷新。

如果节点在运行可访问配置的任务容器时失去与群集的连接,则任务容器仍可访问其配置,但在节点重新连接到群集之前无法接收更新。

您可以随时添加或检查单个配置,或列出所有配置。您无法删除正在运行的服务正在使用的配置。请参阅旋转配置,以便在不中断正在运行的服务的情况下移除配置。

为了更容易地更新或回滚配置,请考虑在配置名称中添加版本号或日期。通过控制给定容器内配置的挂载点,这变得更容易。

阅读更多关于docker config命令

使用这些链接阅读有关特定命令的内容,或者继续执行有关使用配置与服务的示例。

  • docker config create

  • docker config inspect

  • docker config ls

  • docker config rm

示例

本节包含说明如何使用Docker配置的示例。

注意:为简单起见,这些示例使用单引擎群和非标度服务。这些示例使用Linux容器,但Windows容器也支持配置。

简单的例子:开始使用配置

这个简单的例子显示了配置如何在几个命令中工作。对于一个真实世界的例子,继续到中间例子:使用配置与一个Nginx服务。

  1. 添加一个配置到Docker。该docker config create命令读取标准输入,因为最后一个参数表示要读取配置文件的文件被设置为-。$ echo“这是一个配置”| 码头配置创建我的配置 -

  2. 创建一个redis服务并授予它对配置的访问权限。默认情况下,容器可以访问配置/my-config,但可以使用该target选项自定义容器上的文件名。$ docker service create --name redis --config my-config redis:alpine

  3. 验证任务是否正在运行,没有问题docker service ps。如果一切正常,输出如下所示:$ docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS bkna6bpn8r1a redis.1 redis:alpine ip-172-31-46-109 8秒前运行

  4. 获取redis服务任务容器的标识docker ps,以便您可以使用docker exec它连接到容器并读取config数据文件的内容,该数据文件默认为全部可读,并且与config的名称相同。下面的第一条命令说明了如何找到容器ID,第二个和第三个命令使用shell完成来自动执行此操作。                                                    $ docker ps --filter name=redis -q  5cb1c2348a59  $ docker exec $(docker ps --filter name=redis -q) ls -l /my-config  -r--r--r--    1 root     root            12 Jun  5 20:49 my-config                                                       $ docker exec $(docker ps --filter name=redis -q) cat /my-config 这是一个配置

  5. 尝试删除配置。删除失败,因为redis服务正在运行并可以访问配置。$ docker config ls  ID                          NAME                CREATED             UPDATED fzwcfuqjkvo5foqu7ts7ls578   hello               31 minutes ago      31 minutes ago   $ docker config rm my-config 从daemon的错误反馈: rpc error: code = 3 desc = config 'my-config'正在被以下服务使用: Redis

  6. redis通过更新服务,从正在运行的服务中删除对配置的访问。$ docker service update --config-rm my-config redis

  7. 重复步骤3和4,验证该服务不再有权访问配置。容器ID将不同,因为该service update命令重新部署服务。$ docker exec -it $(docker ps --filter name=redis -q) cat /my-config  cat: can't open '/my-config': No such file or directory

  8. 停止并删除服务,并从Docker中删除配置。$ docker service rm redis $ docker config rm my-config

简单的例子:在Windows服务中使用配置

这是一个非常简单的例子,它展示了如何在微软Windows Server 2016上使用Docker 17.06 EE上运行的Microsoft IIS服务配置,或者在Microsoft Windows 10上使用Docker for Mac 17.06上的配置。它将网页存储在配置中。

此示例假定您已安装PowerShell。

  1. 将以下内容保存到一个新文件中:index.html<html> <head> <title> Hello Docker </ title> </ head> <body> <p> Hello Docker!您已经部署了一个HTML页面。</ p> </ body> </ html>

  2. 如果您还没有这样做,请初始化或加入群集。PS> docker swarm init

  3. 将该index.html文件保存为名为的群集配置homepage。PS> docker config create homepage index.html

  4. 创建一个IIS服务并授予它对homepage配置的访问权限。PS> docker service create     --name my-iis     -p 8000:8000     --config src=homepage,target="\inetpub\wwwroot\index.html"     microsoft/iis:nanoserver

  5. 访问IIS服务http://localhost:8000/。它应该从第一步开始提供HTML内容。

  6. 删除服务和配置。PS> docker service rm my-iis  PS> docker config rm homepage

高级示例:使用配置与Nginx服务

这个例子分为两部分。第一部分是关于生成站点证书,并不直接涉及Docker配置,但是它建立了第二部分,您在其中存储和使用站点证书作为一系列秘密,并将Nginx配置作为配置使用。

生成站点证书

为您的站点生成根CA和TLS证书和密钥。对于生产站点,您可能希望使用服务Let’s Encrypt来生成TLS证书和密钥,但此示例使用命令行工具。这一步有点复杂,但仅仅是一个设置步骤,以便您可以将某些内容存储为Docker秘密。如果你想跳过这些子步骤,您可以使用我们的加密生成网站密钥和证书,命名文件site.keysite.crt,并跳过配置Nginx的容器。

  1. 生成一个根密钥。$ openssl genrsa -out“root-ca.key”4096

  2. 使用根密钥生成CSR。$ openssl req \           -new -key "root-ca.key" \           -out "root-ca.csr" -sha256 \           -subj '/C=US/ST=CA/L=San Francisco/O=Docker/CN=Swarm Secret Example CA'

  3. 配置根CA. 编辑一个名为的新文件root-ca.cnf并将以下内容粘贴到其中。这限制了根CA只能签署叶证书而不能签署中间CA. root_ca basicConstraints = critical,CA:TRUE,pathlen:1 keyUsage = critical,nonRepudiation,cRLSign,keyCertSign subjectKeyIdentifier = hash

  4. 签署证书。$ openssl x509 -req -days 3650 -in“root-ca.csr”\ -signkey“root-ca.key”-sha256 -out“root-ca.crt”\ -extfile“root-ca.cnf”-extensions \ root_ca

  5. 生成站点密钥。$ openssl genrsa -out“site.key”4096

  6. 生成站点证书并使用站点密钥对其进行签名。$ openssl req -new -key“site.key”-out“site.csr”-sha256 \ -subj'/ C = US / ST = CA / L = San Francisco / O = Docker / CN = localhost'

  7. 配置站点证书。编辑一个名为的新文件site.cnf并将以下内容粘贴到其中。这限制了站点证书,因此它只能用于对服务器进行身份验证,并且不能用于签名证书。server authorityKeyIdentifier = keyid,issuer basicConstraints = critical,CA:FALSE extendedKeyUsage = serverAuth keyUsage = critical,digitalSignature,keyEncipherment subjectAltName = DNS:localhost,IP:127.0.0.1 subjectKeyIdentifier = hash

  8. 签署网站证书。$ openssl x509 -req -days 750 -in“site.csr”-sha256 \ -CA“root-ca.crt”-CAkey“root-ca.key”-CAcreateserial \ -out“site.crt”-extfile“site .cnf“-extensions server

  9. site.csrsite.cnf文件不需要由Nginx的服务,但你需要他们,如果你想生成一个新的站点证书。保护root-ca.key文件。

配置Nginx容器

  1. 生成一个非常基本的Nginx配置,通过HTTPS提供静态文件。TLS证书和密钥将作为Docker机密存储,以便它们可以轻松旋转。在当前目录中,site.conf使用以下内容创建一个新文件:server {listen 443 ssl; server_name localhost; ssl_certificate /run/secrets/site.crt; ssl_certificate_key /run/secrets/site.key; 位置/ {root / usr / share / nginx / html; index index.html index.htm; }}

  2. 创建两个秘密,代表密钥和证书。只要小于500 KB,您就可以将任何文件存储为秘密文件。这使您可以将密钥和证书与将使用它们的服务分离。在这些示例中,秘密名称和文件名是相同的。$ docker secret创建site.key site.key $ docker secret创建site.crt site.crt

  3. site.conf文件保存在Docker配置中。第一个参数是配置的名称,第二个参数是要从中读取的文件。$ docker config create site.conf site.conf List the configs: $ docker config ls  ID                          NAME                CREATED             UPDATED 4ory233120ccg7biwvy11gl5z   site.conf  4秒前

  4. 创建一个运行Nginx并可以访问两个秘密和配置的服务。$ docker service create \ --name nginx \ --secret site.key \ --secret site.crt \ --config source = site.conf,target = / etc / nginx / conf.d / site.conf \  - 发布3000:443 \ nginx:latest \ sh -c“exec nginx -g'daemon off;'”在运行的容器中,现在存在以下三个文件:

- `/run/secrets/site.key`- `/run/secrets/site.crt`- `/etc/nginx/conf.d/site.conf`
  1. 验证Nginx服务正在运行。$ docker service ls ID NAME MODE REPLICAS IMAGE zeskcec62q24 nginx replicated 1/1 nginx:latest $ docker service ps nginx NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS Nginx.1.9ls3yo9ugcls nginx:latest moby Running Running 3分钟前

  2. 验证服务是否可操作:您可以访问Nginx服务器,并且正在使用正确的TLS证书。$ curl --cacert root-ca.crt https://0.0.0.0:3000  <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>     body {         width: 35em;         margin: 0 auto;         font-family: Tahoma, Verdana, Arial, sans-serif;     } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>如果您看到此页面,nginx web服务器已成功安装并正常运行。需要进一步配置。</ p> <p>有关在线文档和支持,请参阅<a href="http://nginx.org/"> nginx.org </a>。商业支持可以在<a href="http://nginx.com/">nginx.com</a>.</p>  <p><em>Thank you for using nginx.</em></p> </body> </html> $ openssl s_client -connect 0.0.0.0:3000 -CAfile root-ca.crt  CONNECTED(00000003) depth=1 /C=US/ST=CA/L=San Francisco/O=Docker/CN=Swarm Secret Example CA verify return:1 depth=0 /C=US/ST=CA/L=San Francisco/O=Docker/CN=localhost verify return:1 --- Certificate chain  0 s:/C=US/ST=CA/L=San Francisco/O=Docker/CN=localhost    i:/C=US/ST=CA/L=San Francisco/O=Docker/CN=Swarm Secret Example CA --- Server certificate -----BEGIN CERTIFICATE----- … -----END CERTIFICATE----- subject=/C=US/ST=CA/L=San Francisco/O=Docker/CN=localhost issuer=/C=US/ST=CA/L=San Francisco/O=Docker/CN=Swarm Secret Example CA --- No client certificate CA names sent --- SSL handshake has read 1663 bytes and written 712 bytes --- New, TLSv1/SSLv3, Cipher is AES256-SHA Server public key is 4096 bit Secure Renegotiation IS supported Compression: NONE Expansion: NONE SSL-Session:     Protocol  : TLSv1     Cipher    : AES256-SHA     Session-ID: A1A8BF35549C5715648A12FD7B7E3D861539316B03440187D9DA6C2E48822853     Session-ID-ctx:     Master-Key: F39D1B12274BA16D3A906F390A61438221E381952E9E1E05D3DD784F0135FB81353DA38C6D5C021CB926E844DFC49FC4     Key-Arg   : None     Start Time: 1481685096     Timeout   : 300 (sec)     Verify return code: 0 (ok)

  3. 除非你打算继续下一个例子,否则在运行这个例子之后通过删除nginx服务和存储的秘密和配置来清理。$ docker service rm nginx $ docker secret rm site.crt site.key $ docker config rm site.conf

您现在配置了一个Nginx服务,其配置与其映像分离。您可以使用完全相同的映像运行多个站点,但可以单独配置,而不需要根本建立自定义映像。

例如:旋转一个配置

要旋转一个配置,你首先保存一个与当前正在使用的名称不同的新配置。然后重新部署该服务,删除旧配置并在容器中的相同安装点添加新配置。此示例通过旋转site.conf配置文件构建在前一个示例上。

  1. site.conf本地编辑文件。添加index.php到该index行,并保存该文件。server {     listen                443 ssl;     server_name           localhost;     ssl_certificate       /run/secrets/site.crt;     ssl_certificate_key   /run/secrets/site.key;      location / {         root   /usr/share/nginx/html;         index  index.html index.htm index.php;     } }

  2. 使用新的site.conf叫做的新建一个Docker配置site-v2.conf。$ docker config create site-v2.conf site.conf

  3. 更新nginx服务以使用新配置而不是旧配置。$ docker service update \ --config -rm site.conf \ --config-add source = site-v2.conf,target = / etc / nginx / conf.d / site.conf \ nginx

  4. 验证nginx服务是否完全重新部署,使用docker service ls nginx。当它是,你可以删除旧的site.conf配置。$ docker config rm site.conf

  5. 为了清理,你可以删除nginx服务,以及秘密和配置。$ docker service rm nginx $ docker secret rm site.crt site.key $ docker config rm site-v2.conf

您现在已经更新了nginx服务的配置,而无需重新构建其映像。

Previous article: Next article: