为什么我需要处于 Swarm 模式才能使用 Docker 个秘密?

Why do I need to be in Swarm mode to use Docker secrets?

我正在玩弄单个容器 docker 图像。我想在不使用 compose 的情况下将我的数据库密码存储为秘密(现在有问题和 Gradle)。我以为即使没有撰写我仍然可以使用秘密但是当我尝试时我得到...

$ echo "helloSecret" | docker secret create helloS -

Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

为什么我需要使用集群模式才能使用秘密?为什么我不能在没有集群的情况下使用它们?

查看此答案:https://serverfault.com/a/936262 由用户 sel-en-ium 提供:-

You can use secrets if you use a compose file. (You don't need to run a swarm).

You use a compose file with docker-compose: there is documentation for "secrets" in a docker-compose.yml file.

I switched to docker-compose because I wanted to use secrets. I am happy I did, it seems much more clean. Each service maps to a container. And if you ever want to switch to running a swarm instead, you are basically already there.

Unfortunately the secrets are not loaded into the container's environment, they are mounted to /run/secrets/

您需要 运行 集群模式来获取秘密,因为这就是 docker 实施秘密的方式。秘密的价值在于工作人员永远不会将秘密写入磁盘,秘密是在需要知道的基础上(其他工作人员在任务被安排在那里之前不会收到秘密),并且管理人员会在磁盘上加密该秘密。 manager上secret的存储使用raft数据库

您可以使用命令docker swarm init轻松部署单节点swarm集群。从那里,docker-compose up 更改为 docker stack deploy -c docker-compose.yml $stack_name


swarm 模式中的秘密和配置提供了将单个文件卷安装到容器中以进行配置的替代方法。所以在单节点没有swarm模式的情况下,总可以这样定义:

version: '2'
services:
  app:
    image: myapp:latest
    volumes:
    - ./secrets:/run/secrets:ro

或者您可以通过将这些机密加载到命名卷中,将这些机密与您的应用稍微分开。为此,您可以执行以下操作:

tar -cC ./secrets . | docker run -i -v secrets:/secrets busybox tar -xC /secrets

然后挂载该命名卷:

version: '2'
volumes:
  secrets:
    external: true
services:
  app:
    image: myapp:latest
    volumes:
    - secrets:/run/secrets:ro