使用docker-compose时如何阅读外部机密

时间:2018-04-21 12:00:49

标签: docker docker-compose

我想知道如何将外部机密传递给docker-compose产生的服务。我做了以下事情:

我创造了新的秘密

printf "some secret value goes here" | docker secret create wallet_password -

我的docker-compose.yml:

version: "3.4"
services:
  test:
    image: alpine
    command: 'cat /run/secrets/wallet_password'
    secrets: 
    - wallet_password

secrets:
  wallet_password:
    external: true

然后我跑:

docker-compose -f services/debug/docker-compose.yml up -d --build

docker-compose -f services/debug/docker-compose.yml up

我收到以下回复:

WARNING: Service "test" uses secret "wallet_password" which is external. External secrets are not available to containers created by docker-compose.
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Starting debug_test_1 ...
Starting debug_test_1 ... done
Attaching to debug_test_1
test_1  | cat: can't open '/run/secrets/wallet_password': No such file or directory

Sooo ....有没有办法将外部秘密传递到docker-compose产生的容器中?

2 个答案:

答案 0 :(得分:4)

不。

  

docker-compose创建的容器无法使用外部机密。

错误消息很好地总结了它。秘密是一种群模式功能,秘密存储在群体管理器引擎内部。该经理不会将这些秘密暴露给外部发起的容器。只有具有秘密的群集服务才能运行加载了秘密的容器。

您可以在swarm模式下运行提取秘密的服务,因为它只是容器内的文件,容器内的应用程序可以只是cat文件内容。您还可以通过将文件作为卷安装在秘密位置来复制以compose开头的容器中的机密功能。为此,您需要一个单独的撰写文件,因为卷装入和秘密装载会相互冲突。

答案 1 :(得分:1)

你需要运行一个集群。事情是这样的:

创建一个群:

docker swarm init

创建您的秘密(根据您的需要):

docker secret create <secret_name> <secret_content>

检查所有可用的秘密:

docker secret ls

现在,使用 docker-compose 作为服务的前身:

docker stack deploy --compose-file <path_to_compose> <service_name>

请注意,您将在位于 /run/secrets/<secret_name> 的纯文本文件中找到您的秘密。