How do you manage Compose secrets on a plain VM?

November 28, 2026 5 answers
Asked by p.827+drivnelo

I'm looking for a practical way to pass secrets to Docker Compose containers without scattering env files around. Some apps want environment variables rather than files. What works on a single VM, and how can I check whether a secret ended up in an old image layer?

Answers

Nexmari+7v

Compose file-based secrets work without Swarm. Declare a secret with a file source and grant it to the service. Compose mounts it at a path like /run/secrets/db_password.

On plain Compose this is a bind mount, not a managed secret store. Keep the source file outside the repository, restrict its permissions, and make sure the process in the container can read it. Some images support a variable such as POSTGRES_PASSWORD_FILE, so the application can read the mounted file without putting the password directly in the container configuration.

p.827+drivnelo:

What if the app only accepts a regular environment variable?

Trelvani-5j:

A small entrypoint script can read the mounted file, export the variable, and then exec the app. That still puts the secret in the app's process environment, so it's not as contained as reading the file directly.

Reply to this answer
_f92!plovdari.5

For a small VM, a root-owned env file outside the repo with restrictive permissions is a reasonable starting point. It is simple, but the values still reach the container configuration if you pass them as environment variables. They can show up in docker inspect and anything that collects that configuration for troubleshooting.

For storage and deployment, SOPS with age is another option. Keep the encrypted file in the repo and decrypt during deployment. Decide where the plaintext goes and how long it remains there. Encryption at rest doesn't remove the need to protect it when the service runs.

Reply to this answer
_p36!drimvani.9

A secrets manager can cut down on manually copying files. For example, 1Password CLI can inject secret references into a Compose configuration as part of deployment. Other secret managers can serve a similar role.

Check what each integration actually writes or passes along. Injecting a value into an environment variable doesn't make it invisible to container inspection, even if the original secret was stored safely in the manager.

Reply to this answer
_g72+velnaro.3

For old images, look at both the build history and the layers. docker history --no-trunc can expose values or commands recorded in image history, but it won't reliably tell you everything that was in a copied file. To check that, inspect the image's saved layers, including layers before a file was deleted.

Deleting a secret in a later layer doesn't erase it from an earlier one. Build arguments are also a risky place for credentials. Use BuildKit secret mounts for build-time access instead, and make sure the build command doesn't copy the secret into the resulting image or print it into logs. If you find a credential in an image, treat it as exposed and rotate it.

c.482+torqueli:

That distinction matters for old images too. Fixing the Dockerfile stops future builds from repeating the mistake, but it doesn't clean up copies of the existing image.

Reply to this answer
p.571~zavquelo

Plan rotation before you need it. Changing a secret file doesn't guarantee the app will reread it, so check the app's reload behavior and be prepared to restart or recreate the service. If the credential provider supports an overlap period, use that to switch without cutting off the running app immediately.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions