I’m deploying a few apps in Docker containers on my DigitalOcean Droplet, and I’m currently passing environment variables directly in the docker run
command. It works, but doesn’t feel very secure.
What’s the best practice for managing secrets like API keys and database passwords in Docker on a VPS?
Should I be using .env
files, Docker secrets, or something else entirely? Curious what others are doing.
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!
Accepted Answer
Hey there 👋
For most small-to-medium setups on a VPS like a DigitalOcean Droplet, using a .env
file and passing it to your container like this:
docker run --env-file .env your-image
is a solid starting point — just make sure that .env
file isn’t committed to version control.
For more advanced setups (especially production), tools like Docker secrets or a secrets manager (like Vault or Doppler) are a better choice, but they can be a bit heavy for smaller projects.
Also, if you’re just getting started or want a refresher, check out this free Docker eBook: 👉 https://github.com/bobbyiliev/introduction-to-docker-ebook
Hope that helps!
- Bobby
heya, @fdb05b1e9847414ab859b0f7cb4b1e
As Bobby mentioned for small and medium setups using a .env
should be fine. The other approach will be using a secret manager like HashiCorp Vault or similar:
https://dev.to/darkedges/hashicorp-vault-quickstart-26g6
Regards
Heya,
Apart from what has been said,. If you’re scaling up or handling multiple secrets, you might consider HashiCorp Vault and using an API endpoint from it to get the secrets directly. Ofcourse this would be viable for a bigger project.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.