Docker
Environment Variables
Containerization
DevOps
Docker Containers

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.

Practice system design

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.

dockerfile
1# Syntax in Dockerfile
2ENV MY_VAR_NAME=my_value
3
4# Example
5FROM node:14
6ENV NODE_ENV=production
7WORKDIR /app
8COPY . /app
9RUN npm install
10CMD ["node", "index.js"]

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.

bash
1# Syntax
2docker run -e "MY_VAR_NAME=my_value" my_image
3
4# Example
5docker run -e "API_KEY=123456" my_image

You can also pass an entire file of environment variables using the --env-file flag:

bash
1# Create env.list file
2API_KEY=123456
3DB_HOST=mysql
4
5# Pass environment variables from file
6docker run --env-file ./env.list my_image

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.

yaml
1# docker-compose.yaml
2version: '3'
3services:
4  web:
5    image: "webapp:latest"
6    environment:
7      - DEBUG=1
8      - SECRET_KEY=abcdef12345
9    ports:
10      - "5000:5000"

Alternatively, similar to docker run, you can use an env file:

yaml
1services:
2  db:
3    image: postgres
4    env_file:
5      - db_env.list

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:

MethodUse CaseExampleProsCons
Dockerfile ENVDefault, static valuesENV API_KEY=valueSimple, version-controlledNot suitable for sensitive data
docker run -eSingle containers, sensitive datadocker run -e "API_KEY=value"Dynamic, easy for single runsRequires command line management
docker-composeMulti-container setupsenvironment: [DEBUG=1]Manages complexity, version-controlledSeparate syntax to learn
--env-fileBundling multiple varsdocker run --env-file vars.envOrganize variables in fileExtra file management

Best Practices

  1. Security: Never store sensitive data in your Dockerfile. Consider management tools like Docker secrets or external secrets management tools for sensitive data.
  2. Maintenance: Comment your Dockerfile and docker-compose files, explaining the use of each environment variable.
  3. Testing: Test containers in various environments (dev, prod) to ensure they behave as expected with the provided environment variables.
  4. 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.