I'm looking for some effective ways to secure sensitive information within my Docker Compose files. Right now, I've got too much sensitive data included directly in my YAML files, and I know that's not a good approach. I'm using TrueNAS with a custom YAML option for deployment. Should I opt for Docker secrets, or would it be better to use an `env_file` with permissions set to 600 or 400? I'm trying to make my Docker setup as secure as possible. Any best practices or recommendations would be greatly appreciated!
4 Answers
Using `docker exec` and `export` can put your sensitive environment variables at risk if someone gains access. I suggest setting your docker-compose.yml permissions to 0600 as a safeguard. While using env files is essential for keeping secrets out of public repositories like GitHub, you should combine that approach with solid file permissions to limit unauthorized access.
The .env file is particularly useful when you're copying your folder with the docker-compose.yml file, as you can easily exclude the .env file to keep sensitive information private. I suggest implementing both approaches — managing your env files carefully and securing them with the right permissions.
I recommend using Docker secrets and mounting them into the container at the standard `/run/secrets/X` location. It's crucial to avoid exposing sensitive values directly in the Compose file. I pass my secrets in using environment variables that are loaded from an `.env` file. These are decrypted temporarily before running `docker compose up`. Just remember, if your host is compromised, so are your secrets, so always be ready to rotate them.
The standard way to handle this is to use a .env file. It loads automatically during the build, which simplifies things. You can verify your variables by running `docker compose config` to see the final version Docker will use. Also, if possible, run the container in rootless mode to boost security. Don't forget to avoid checking your .env file into version control. As a pro-tip, create an `.env.example` file to hold the keys you use, and check that into Git instead.

Related Questions
How To Get Your Domain Unblocked From Facebook
How To Find A String In a Directory of Files Using Linux