I'm currently working on a project where my backend runs in a Docker container, and I use a Docker Compose file to manage environment variables. In the development environment, I can easily use a .env file located in the same directory as the Docker Compose file. However, I'm transitioning to production and am planning to store these environment variables in Azure Key Vault. How can I adjust my setup to spin up the Docker container using Docker Compose while pulling environment variables from Azure Key Vault? I'm looking for the best approach to follow.
5 Answers
Your approach to managing secrets is key! If you're using containers in Azure, look into Azure Container Apps; they integrate directly with Key Vault for secret management, making it much easier to pull in your environment variables.
Best practice is to avoid .env files in production. Instead, use Managed Identity to fetch secrets from Azure Key Vault directly in your app at runtime. If you really need environment variables, consider fetching them via a startup script using Azure CLI before the container starts.
It's generally not recommended to pull secrets directly into your Docker Compose setup in production. A common strategy is to use a managed identity along with the Azure SDK in your app to fetch secrets from Key Vault at runtime. This way, your app handles the authentication and retrieval of sensitive information securely.
We use Terraform to provision and manage our backend services. It works well with Docker containers and allows us to securely inject environment variables from Key Vault, streamlining our deployment process.
If you're running Docker on a VM, consider adding a system-assigned identity to the VM. This allows your containers to pull secrets from Key Vault securely. You can create Docker secrets from these vault secrets, keeping your environment clean and secure.

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