How to use docker secrets without a swarm cluster?

53,162

Solution 1

You can't... It does not support secrets without Swarm. Unless ''may be'' you ''Swarm'' using only one node.

The other solution would be, I think to use a third party vault software like this one:

https://www.vaultproject.io/

But then, to use the secrets in your containers from Vault, you would need to read the doc.

Hope this bring you to the right path to start.

Solution 2

Yes, 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.

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

Here is a example:

1) Project Structure:

|
|---    docker-compose.yml
|---    super_duper_secret.txt

2) docker-compose.yml contents:

version: "3.6"

services:

  my_service:
    image: centos:7
    entrypoint: "cat /run/secrets/my_secret"
    secrets:
      - my_secret

secrets:
  my_secret:
    file: ./super_duper_secret.txt

3) super_duper_secret.txt contents:

Whatever you want to write for a secret really.

4) Run this command from the project's root to see that the container does have access to your secret, (Docker must be running and docker-compose installed):

docker-compose up --build my_service

You should see your container output your secret.

Share:
53,162

Related videos on Youtube

Juan Sebastian
Author by

Juan Sebastian

Head of DevOps @ PSL, Im currently focused on overseeing the DevOps business offer for PSL, I help shape our vision on how to implement devops and ensure we are delivering that to our clients whether that is from the technical, process or cultural standpoint.

Updated on September 18, 2022

Comments

  • Juan Sebastian
    Juan Sebastian almost 2 years

    Currently we im a running application on a single docker container, the application needs all sorts of sensitive data to be passed as environments variables,

    Im putting those on the run command so they don't end up in the image and then on a repository, however i end up with a very non-secure run command,

    Now, i understand that docker secrets exist, however, how can i use them without deploying a cluster? or is there any other way to secure this data?

    Best Regards,

  • user23390
    user23390 over 6 years
    @JuanSebastian you should check out Docker 'build-args' for that use case.
  • yield
    yield over 6 years
    @JuanSebastian I might be wrong, but getting the local ENV would give you what's inside thoses build-args.... Not sure .....
  • BMitch
    BMitch over 5 years
    Can you show a working example of docker-compose using a secret? The documentation and my understanding of the implementation indicate that the secret will not be configured in the container.
  • Alwin Kesler
    Alwin Kesler over 5 years
    Docker secrets are only available to swarm services, not to standalone containers. To use this feature, consider adapting your container to run as a service. Stateful containers can typically run with a scale of 1 without changing the container code. docker
  • Lindsay-Needs-Sleep
    Lindsay-Needs-Sleep over 5 years
    @BMitch Added an example. Hope it helps you! (It has been awhile since I have worked with docker and my environment has quite a bit more going on... but I think this should work. Please let me know if I missed something!)
  • giorgiosironi
    giorgiosironi about 5 years
    I cannot replicate this on my services without a swarm (docker-compose.yml on a single node); when I start the container /run only contains an nginx.pid and there are no Mounts shown by docker inspect $container.
  • giorgiosironi
    giorgiosironi about 5 years
    I can replicate it with the original images used here however; going to find out what is the difference.
  • giorgiosironi
    giorgiosironi about 5 years
    My problem was that the container needed to be recreated after secrets is added to the service configuration. docker-compose down should be run before applying the change, so that a new container is created with the Mounts.
  • Tobias Uhmann
    Tobias Uhmann about 5 years
    This is awesome! After reading how secrets have been an issue for years and the problem has recently become solvable only via starting a swarm according to the official docs I'm so happy that it works via plain docker-compose now :D
  • lonix
    lonix over 4 years
    What is the owner and permission for the super_duper_secret.txt file?
  • Lindsay-Needs-Sleep
    Lindsay-Needs-Sleep over 4 years
    It has been very long since I have used docker, but I believe non-root owner-ship is fine. If it is root read-only, than you might need to run docker compose as root. Let us know how it goes!
  • Gabor Garami
    Gabor Garami over 4 years
    For me, it's not working, latest docker-compose says the secrets key is unknown for it. Could you add what docker-compose versions do you use and can you confirm it works on a plain new VM without Docker Swarm?
  • Lindsay-Needs-Sleep
    Lindsay-Needs-Sleep over 4 years
    I don't have access to the VM I was using anymore. I can definitely confirm it was without swarm. I never touched it. My docker-compose.yml is version 3.6. According to this, v3.6 works with docker engine 18.02.0+ and v3.7 works with 18.06.0+. v3.6 was the newest when I was working on this. I think it is safe to guess that my version was between 18.02.0 and 18.06.0. I think it was probably 18.03.0. Is your's above 18.06.0? If so, would you be able to try a lower version? If that works, I will update the answer with a max version. Thanks!
  • ssnobody
    ssnobody over 4 years
    Just thought I'd link to the PR that added this to plain docker-compose (without swarm). github.com/docker/compose/pull/4368 It really is in there and from the code it looks like min version for compose file is 3.1 and API is 1.13.0. Code is still in current master ( github.com/dnephin/compose/blob/… ), so wouldn't expect a max version.
  • Mohammad
    Mohammad over 4 years
    I've created an example with mysql as a gist: gist.github.com/ronaldb/d4b3d3327a5f80bfd12d748ca0ae91ae - same idea as the cat, but using a more practical application.
  • reinierpost
    reinierpost over 4 years
    This is helpful, but it doesn't use docker secret, which I believe the question is about.
  • Shōgun8
    Shōgun8 over 4 years
    How would Vault help in this scenario?
  • Juan Sebastian
    Juan Sebastian about 4 years
    @Octavian it would server as a secure storage that handles who gets access to which secret, and (im thinking what i was doing 3 years ago) you would then code your stuff so every secret had to be negotiated and accessed through vault, every time it needed to be used.
  • barbazoo
    barbazoo over 3 years
    Major caveats exist: docker-compose will only set the secrets on the local docker engine. Secrets will not be passed when docker-compose is talking to a remote daemon. Given we talk about the merits of swarm vs. docker-compose, it assumes the target is a multi-node set. And with that docker-compose becomes a mute point.
  • conny
    conny almost 3 years
    WHY would anyone do this, when without swarm, the effect is the same as a bind-mount but with (arguably) more complex config? The answer lies in compatibility, and predictability: Most of your service yaml will work the same (maybe you have modularized it) both for single-engine and swarm-mode. And software inside containers can make assumption about where to find secrets "/run/secrets" (read-only by default, btw) as opposed to each app coming up with its own "standard" mount points.