I’m running a few Docker containers on my Droplet, and some of these apps require sensitive data like API keys and database credentials. Right now, I’m passing them directly in the Docker run command with -e
, but I’m worried this isn’t secure. What are some best practices for securely storing and managing environment variables in Docker containers? Should I be using something like Docker secrets, and how does that work on a single Droplet?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hey there 👋,
If you’re using Docker in a Swarm or Kubernetes setup, Docker Secrets is a great way to securely manage sensitive information. Secrets are encrypted and are only available to the containers that need them. However, this feature isn’t available for standalone Docker instances (like on a single Droplet), but it’s something to keep in mind if you’re using orchestration tools.
For Docker Swarm:
You can then access the secret inside your container via a mounted file.
Another simpler approach you can take for single-Droplet setups is using
.env
files. Store your environment variables in a.env
file and then reference them inside yourdocker-compose.yml
ordocker run
commands.Example
.env
file:You can load this file when running the container:
Or in your
docker-compose.yml
:If you don’t want to use
.env
files, you can also mount a volume with a file containing the sensitive data. This way, you don’t expose secrets as environment variables but instead as files in your container.Example:
Hope that helps!
- Bobby