Question

How do you handle environment variables securely in Docker on a Droplet?

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.


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
March 26, 2025
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

alexdo
Site Moderator
Site Moderator badge
March 27, 2025

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

KFSys
Site Moderator
Site Moderator badge
March 27, 2025

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.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.