Report this

What is the reason for this report?

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

Posted on March 26, 2025

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!

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 👋

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.

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.