r/selfhosted • u/Keensworth • 1d ago
Docker Management I've been using Docker compose secrets the wrong way
For months, I've thought the methods for using secrets was :
- MONGO_PASSWORD = ${MONGO_PWD}Then in a .env file say
MONGO_PWD=XXXXXXXXXBut apparently there's something called secrets in Docker Compose
How do you guys manages your secrets? Is it really more secure? How do you use it with 3rd party Docker managements (Komodo, Arcane, Portainer...) ? How come I've never seen secrets in the documentation of projets I uses online?
116
Upvotes
•
u/asimovs-auditor 1d ago
Expand the replies to this comment to learn how AI was used in this post/project.
56
u/Open-Adhesiveness-86 1d ago
file-based secrets do work on standalone Compose, no swarm needed:
secrets: mongo_pw: {file: ./mongo_pw.txt}and it gets bind-mounted at /run/secrets/mongo_pw. Swarm is only required fordocker secret createexternal secrets. The catch is the app has to read a file, so you need the _FILE variants, e.g. MONGO_INITDB_ROOT_PASSWORD_FILE or POSTGRES_PASSWORD_FILE. Plenty of third-party images never implemented those, which is why you rarely see it in their docs.18
u/Proud-Worldliness747 1d ago
The `${MONGO_PWD}` form is interpolated when Compose parses the file. If you then pass that value as an environment variable, it still lives in the container's env, so it can show up in `docker inspect`, `/proc/<pid>/environ`, and sometimes in logs or crash dumps.
Compose `secrets:` is mostly about mounting the value as a file, usually at `/run/secrets/<name>`, instead of putting it in the environment. On a normal Compose host (not Swarm), that file is still just a file on disk, so it isn't encrypted at rest and it isn't a stronger trust model than a `.env` file. The real gain is avoiding some of those environment leaks, plus a stable path the process can read.
Two practical limits: many images only document a password env var, and they'll only honor a file if they have a `*_FILE` variable or you point the app at the path yourself. And tools like Portainer or Komodo often still work from stack env files, especially outside Swarm; Swarm secrets are the actual Docker secret store, while a single-node Compose deploy is usually just a bind mount. That's a big reason project docs still show env vars: it works in all of those setups.
14
u/neoblitz 1d ago
this was the example from the Docker Compose docs. as you can see you can mount the secret file. this example is straight out from - https://docs.docker.com/compose/how-tos/use-secrets/#examples
you can also use env variable as secrets. https://docs.docker.com/compose/how-tos/use-secrets/#build-secrets
-
11
u/DeliciousFollowing48 22h ago
I use sops. It encrypts .env with key and now secrets can go to git for deployment. (decryption) age key remains on device with 600. It's simple extension to .env but works well.
10
u/neoblitz 1d ago
in komodo you can use variables (mark it as secret).
Then call the secret variable in a stack.
6
u/timeraider 22h ago
In the end, no matter what you do.. the env are visible from inside the docker containers. So in the end the best way is to keep the device itself secure.
3
u/siegfriedthenomad 12h ago
Personally I only deploy apps that support Docker secrets. If a nice app doesn’t support them yet it’s a good way for me to contribute back and submit a quick PR (most of the time only the entrypoint script need to be modified).
To answer how I deploy/manage secrets, I write a small ansible role for the first deployment. Secrets are generated and stored in the ansible vault that is version controlled. From there I manage the stacks in komodo
6
u/thetechnivore 1d ago
I’ve had great luck using Dockhand with 1Password as the secrets provider.
1
u/rchr5880 1d ago
I think you are referring to docker secrets which is part of docker swarm. Most docker compose examples you find on my projects are for standalone docker and you would use secrets in the way you mentioned.
As for secrets in swarm… the secrets are created on the swarm managers and then mounted into the container when the compose stack is started. The compose itself you use for the stack then references the secret which is created on the managers.
For managing the secrets I use Ansible which can create the secrets on the manager and the file which contains all the secrets in their plain text form is encrypted in Absible vault.
That’s it’s in a nutshell anyway.
1
u/lSilverBulletl 1d ago
I use Openbao with a custom script in Komodo’s Wrapper config, because I didn’t want to duplicate secrets across different systems so by having everything in Openbao I programmatically grab the secrets I need on the system that needs them
1
u/Aide_Sea 22h ago
honestly the file mount helps, but I think apps still log the connection string at boot, and backups just copy the secret files. maybe scrub that, otherwise compose secrets don't buy you much
1
u/shaltaSt 19h ago
most web management UIs struggle with file-based secrets because they expect key-value env vars they can inject through their own database. if you want to use compose secrets with them, you usually have to pre-create the text files on the host filesystem first and reference the path in the yaml
1
u/FrostwoodGoblet 7h ago
Secrets are definitely better!
1
u/RookeryNomad 7h ago
Interesting approach!
1
u/YarrowBeacon 7h ago
Secrets management definitely improves security for applications.
1
u/VaderBuysDonuts72 7h ago
Secrets are helpful!
1
u/WhereAreMyBoots83 7h ago
Secrets are definitely more secure overall.
1
u/undead-8 6h ago
Iam using psono to store my docker secrets and it's working great. Docker secrets on the other hand are mostly a function for professional environments and for docker swarm
1
u/Hostman_com 4h ago
Rotation is worth testing too. Does the app reread the secret file, or only read it at startup? It may need a restart to use the new value.
For database credentials, check whether the password-file setting only applies during initialization. The password in an existing database may need to be changed separately.
1
u/regretfullysentient1 4h ago
I keep secrets in encrypted files on the machine and decrypt them at deploy time. Nothing readable sits in the repo, nothing readable sits in the compose file. It's not elegant and there are probably 5 better ways. But I can explain exactly where every secret is and who can read it, which is more than I could say when they were all just sitting in a .env.
1
u/Awesome_Knowwhere 1h ago
Infisical
1
u/ascherbaum 1d ago
I blooged about how I use Docker Secrets (in Docker Compose) to hide secrets in the build output, and avoid copy&pasting secrets and tokens:
Docker Secrets and hiding sensitive information in Docker build output
In this particular problem, a token is part of an URL, and this approach avoids showing the token in the output. It also avoids having the token in the ENV of the built image, for anyone to see.
-3
u/Hemispheric_Camera 1d ago
You could switch to bitwarden secrets manager
-5
u/jerwong 1d ago
Secrets requires you to enable docker swarm. It's similar to secrets in Kubernetes. It won't work on standalone docker. Also, if you had swarm running, you would likely be using docker stack and not docker compose. You would also need to update the application to look in /var/run/secrets/<name of secret> to get the value.