How to Pass Environment Variables to Docker Containers

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.

Read: Docker container orchestration tools

Setting Dockerfile environment variables

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.

Passing Vars at Container Run: (Docker run environment variables)

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 \

-e DB_PORT=3306

app:latest

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.

Read: How to clean up unused Docker containers, images and volumes

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:

environment:

<variable_name>: <value>

For example, the following docker-compose.yml file would set the DB_PASSWORD environment variable to the value mysecretpassword for all containers:

  • version: ‘3’
  • services:
  • mycontainer:
  • image: myimage
  • environment:
  • DB_PASSWORD: mysecretpassword

Read: How to run and manage a Docker container on Linux Ubuntu/Debian

Environment Variable Files

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
  • DEBUG=true
  • DB_NAME=devdb

This is great for switching between different configurations quickly.

Reading Env Vars Inside Containers

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.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

Leave a Reply