How do I pass environment variables to Docker containers?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
When working with Docker, environment variables provide a convenient way to pass configuration options to your Docker containers. This can be crucial for managing settings across different environments without hardcoding sensitive information into the Docker image or application code. Let's dive into the ways you can pass environment variables to Docker containers, including examples and best practices.
Using Dockerfile ENV Instruction
The ENV instruction in a Dockerfile sets the environment variable in the resulting image and hence in any container instantiated from it. This is a static method, suitable for default values and non-sensitive data.
This sets NODE_ENV to production in any container based on this image.
Using docker run Command
The docker run command allows you to pass environment variables directly from the command line using the -e or --env flag. This method is dynamic and suitable for passing sensitive data that shouldn't be written in the Dockerfile.
You can also pass an entire file of environment variables using the --env-file flag:
Using docker-compose
For orchestrating multi-container Docker applications, docker-compose is commonly used. This tool allows defining and running multi-container Docker applications. To pass environment variables, you can use the environment section in docker-compose.yaml.
Alternatively, similar to docker run, you can use an env file:
Security Considerations
When using environment variables for sensitive data (like API keys or passwords), ensure they are not logged or inadvertently exposed within application logs, error messages, or by other means.
Summary Table
Here’s a table summarizing the methods for passing environment variables to Docker containers:
| Method | Use Case | Example | Pros | Cons |
| Dockerfile ENV | Default, static values | ENV API_KEY=value | Simple, version-controlled | Not suitable for sensitive data |
| docker run -e | Single containers, sensitive data | docker run -e "API_KEY=value" | Dynamic, easy for single runs | Requires command line management |
| docker-compose | Multi-container setups | environment: [DEBUG=1] | Manages complexity, version-controlled | Separate syntax to learn |
| --env-file | Bundling multiple vars | docker run --env-file vars.env | Organize variables in file | Extra file management |
Best Practices
- Security: Never store sensitive data in your Dockerfile. Consider management tools like Docker secrets or external secrets management tools for sensitive data.
- Maintenance: Comment your Dockerfile and docker-compose files, explaining the use of each environment variable.
- Testing: Test containers in various environments (dev, prod) to ensure they behave as expected with the provided environment variables.
- Updates: When changing the base image in a Dockerfile, recheck environmental needs. Different base images might behave differently with the same variables.
By following these techniques and practices, you can efficiently manage configuration and ensure the security and proper operation of your Docker containers.
Related reading
- How do I pass environment variables to Docker containers?
- How do I perform a pairwise binary operation between the elements of two containers?
- How do I recreate docker-daemon's additional iptables rules?
- How do I redeploy everything in kubernetes after updating a dockerfile?
- How do I print an exception in Python?
- How do I print to console in pytest?
- How do I run a command on an already existing Docker container?
- How do I run a container from the command line in Kubernetes like docker run?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.