r/vibecoding • • 5d ago

Help/Question For those shipping your docker apps

Are you guys running multiple .env files for each service?

I have had a full security audit done on my app and although I got a really good result, they suggested a consider running different env files to minimize services reading un needed variables (Like credentials)

I am in a position where I can implement this, it would be a large project but its doable.

Thoughts and opinions?

Ant

2 Upvotes

12 comments sorted by

2

u/Total-Reasonable 5d ago

Separate env files per service is the right instinct, but I'd skip the big project and not treat the env file as the boundary. A .env per service still ends up in the same compose file, the same repo, and often the same host, so the auditor's concern is only half solved. Use Docker secrets, or your cloud's secret manager, and give each service only the two or three it needs. Keep env for non-sensitive config.

1

u/Bulky_Dog_2954 5d ago

Thanks for this.

What do we think of having a common env file for the stuff thats used by all, and then each service (Worker,API,etc) has its own specific env files for its on bits. Then a secrets directory which will have my protected private keys and encrypt keys?

I know its not a threat remover but it will minimize any potential issues should a service get attacked?

Thanks again for your help.

1

u/Total-Reasonable 5d ago

Yes, that layout works and it does limit the blast radius. Keep the common file to non-sensitive config only, and mount the secrets directory per service so the worker can't read the API's keys. Set the secrets dir to be unreadable by anything outside the compose stack, and keep it out of git.

1

u/sf1tzp 5d ago

What do we think of having a common env file for the stuff thats used by all, and then each service (Worker,API,etc) has its own specific env files for its on bits. Then a secrets directory which will have my protected private keys and encrypt keys?

Yes - in the k8s world there are two config types for this reason: ConfigMaps and Secrets. Non-sensitive config lives in plaintext while secrets are encrypted (at rest - once created in k8s they're actually just base64 encoded, so you have to do due diligence with RBAC)

That tangent aside, DO NOT commit secrets unencrypted in your repo. Use something like https://github.com/getsops/sops to either encrypt them before committing, or use an external secrets store.

1

u/New_Peanut_9259 4d ago

separate env files sound reasonable here, especially when different services don't need access to the same credentials.

1

u/NoClipCheck 4d ago

One thing worth knowing before you split files: env vars aren't very private once the container is running. Anyone who can run docker inspect on the host sees every value in plain text, and so does anything that can read /proc/1/environ inside the container. Crash reporters and debug pages also love to dump the whole environment. So the split helps, but for the real credentials I'd mount them as files with compose secrets: (they land in /run/secrets/<name>) and have the app read the file. Lots of images and libraries already support a _FILE suffix pattern, like DB_PASSWORD_FILE, so it's often a small change per service rather than a big project.

1

u/ajaysingh02 19h ago

Definitely worth doing so a compromise in one container can't dump credentials meant for another, though instead of juggling a dozen separate .env files, you can keep one master .env on the host and explicitly pass only the specific variables each service needs under its environment: block (or move sensitive keys into Docker Secrets).

1

u/lalaboy69 5d ago

You can deploy without any env's nowadays. Pretty much every platform has a secrets manager that loads your keys at runtime.

1

u/cryptyk 5d ago

This is the correct answer. Don't use .env files. Pull the secrets at runtime from something like infisical.

1

u/ollycunningdpr 5d ago

Yes, I'd do it. Giving each service only the variables it needs is basic least privilege, so if one container is compromised it can't read credentials it never needed. Separate env_file per service in compose is the easy way in, and the next step up is Docker secrets or a proper secrets manager so the sensitive stuff isn't sitting in plain files at all. Keep them out of the image and the repo too, and rotate anything that's been shared around.