When working with Docker containers, you’ll often want to pass configuration options and other data into containers via environment variables. There are several ways to inject environment variables into Docker containers both at build and run time.
This guide will walk through the main methods of setting and using environment variables with Docker using clear examples. By the end, you’ll understand how to effectively pass env vars into containers to keep your images portable and configurable.
The most straightforward way to inject env vars into a Docker container is right in the Dockerfile during the image build process:
- ENV DB_HOST=database
- ENV DB_NAME=appdb
- ENV DB_USER=dbadmin
This sets default values for database connection parameters. The ENV instruction adds persistent environment variables accessible in the running container.
You can also set variables dynamically using shell commands like env or export:
ENV APP_VERSION=$(cat version.txt)
This reads the version.txt file and sets APP_VERSION to match.
In addition to bake-time Dockerfile variables, you can also pass env vars during docker container run using -e:
docker container run -e APP_ENV=prod -e LOG_LEVEL=info app:latest
This temporarily sets two environment variables when starting the container.
You can pass multiple -e flags to specify more than one variable:
docker container run \
-e DB_HOST=prod-db \
To enhance security, consider placing your authentication credentials within a configuration file. Subsequently, when employing the docker run command, employ the –env-file flag as outlined here. This approach empowers you to manage the configuration file’s accessibility, ensuring that individuals with entry to the system won’t have visibility of your confidential credentials.
Using the docker-compose.yml file
The docker-compose.yml file is a way to define and run multiple Docker containers together. It can also be used to pass environment variables to containers.
The syntax for passing environment variables in a docker-compose.yml file is as follows:
For example, the following docker-compose.yml file would set the DB_PASSWORD environment variable to the value mysecretpassword for all containers:
- version: ‘3’
- image: myimage
- DB_PASSWORD: mysecretpassword
For setting many environment variables, it can be easier to use env files rather than long docker run commands.
To pass a file of env vars, use the –env-file option:
docker run –env-file ./config/dev.env myapp:latest
The dev.env file should define env vars like:
- # dev.env
This is great for switching between different configurations quickly.
So how do you actually make use of passed-in environment variables from within your containers?
Most programming languages and frameworks provide access to process environment variables:
- import os
- db_host = os.environ[“DB_HOST”]
- dbHost = process.env.DB_HOST;
- String dbHost = System.getenv(“DB_HOST”);
This allows your app code to dynamically react to values passed in without hard-coding configs.
For additional information, look into the Docker help manual:
docker run –help
Or visit the official documentation about Docker container environment variables.
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.