r/selfhosted • • 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=XXXXXXXXX

But 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

45 comments sorted by

•

u/asimovs-auditor 1d ago

Expand the replies to this comment to learn how AI was used in this post/project.

→ More replies (1)

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 for docker secret create external 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.

7

u/GolemancerVekk 1d ago

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 secret files will borrow the chmod from the original file (outside the container). They also borrow the UID and GID that the container runs as. So if you have user: "1000:1000" and the secret file is 600, then the file inside the container will be owned by 1000:1000 and chmod 600.

It's not much but it's a bit more restrictive than an env var that all UIDs in the container can see.

2

u/Genesis2001 1d ago

Yeah, I've just taken to injecting env_file: in my compose files for specific services and having Ansible install a special <service>.env from Ansible Vault. Secrets live in a git repo but are decrypted on disk on the server.

If there were a better system where more container images supported _FILE envs, I'd probably do that. But then I'd have to rework my ansible playbooks to create secrets and inject them too.

4

u/GolemancerVekk 1d ago

It's the app that has to support reading passwords from a file or from a command. There's nothing more the container can do. Env vars and a file with restricted permissions is already pretty basic and yet some apps can't even bother to support the latter. If Docker offered some sort of API nobody would use it.

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

services:
   db:
     image: mysql:latest
     volumes:
       - db_data:/var/lib/mysql
     environment:
       MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD_FILE: /run/secrets/db_password
     secrets:
       - db_root_password
       - db_password

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD_FILE: /run/secrets/db_password
     secrets:
       - db_password

secrets:
   db_password:
     file: db_password.txt
   db_root_password:
     file: db_root_password.txt

volumes:
    db_data:

-

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.

environment:
  - DATABASE_URL={{ TEST }}

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.

-2

u/ibis--69 1d ago

this is the way ....

-3

u/Additional-Bit806 1d ago

Lo mismo pero con proton pass

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.

21

u/Ok_Expression_9152 1d ago

Docker compose also supports secrets https://docs.docker.com/compose/how-tos/use-secrets/

0

u/creamyatealamma 1d ago

Interesting. Must have changed recently like others I remember looking at secrets and seeing it required swarm or something and have up.

Anyone used this, worth switching to and works well? Any bitwarden/vaultwarden integration?

2

u/kress5 1d ago

it is just a dummy version, only mounts the file content into the container

i use it to separate secrets from my .env and compose files

2

u/GolemancerVekk 1d ago

It's worth switching if the app running inside the container supports reading secrets from a file.

  • Some have special env vars that will point to a file instead of having the password directly. For example Postgres has POSTGRES_PASSWORD and POSTGRES_PASSWORD_FILE.
  • Some apps let you run a command to supply the password and you can do cat /run/secrets/whatever to get it.
  • As a last resort if they have a config file and they expect the password in there you can make the whole config file a secret (assuming you can override the location of the config file).

But there are apps where none of the above will work and you'll have to use a secrets.env and read it with env_file: from compose and it will be an env var.

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/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

1

u/this_for_loona 1d ago

How would this work in cases where I’m using docker on NAS? Specifically Synology container 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. 

3

u/GolemancerVekk 1d ago

Secrets requires you to enable docker swarm.

Not anymore, it's supported by compose without swarm.