Pass Environment Variables to Docker

Set Environment Variables in Dockerfile

To set environment variables in a Dockerfile, use the ENV directive. This allows developers to define key-value pairs accessible across containers spawned from the image.

Environment variables persist for each subsequent instruction during the build. This is useful for projects that need certain configurations in the image, yet might need them visible later for debugging or scaling.

Here's how to set environment variables in a Dockerfile:

FROM ubuntu:20.04 ENV APP_MODE=production ENV DB_USER=root DB_PASS=example

Build your docker image:

docker build -t my_app_image .

To verify the environment variables:

docker run --rm my_app_image env

For variable values that may change across environments, update during runtime:

docker run --rm --env APP_MODE=debug my_app_image env

This approach provides control and flexibility, allowing standardization while adapting to change when necessary.

Utilizing CLI for Environment Variables

The command line interface (CLI) offers flexibility for setting environment variables on the go. Use the --env or -e flag to pass variables when initiating a container:

docker run --rm --env APP_MODE=debug my_app_image

For multiple variables:

docker run --rm -e APP_MODE=staging -e DB_USER=admin -e DB_PASS=secure123 my_app_image

To use existing system environment variables:

export CONFIG_TYPE=test docker run --rm -e CONFIG_TYPE my_app_image

This method provides agility for adjusting container configurations without altering the Dockerfile.

.env Files for Environment Variables

.env files offer an organized approach to managing environment variables. Create a .env file with your variables:

APP_MODE=production DB_USER=root DB_PASS=example

Use the --env-file flag to pass these variables to your container:

docker run --rm --env-file ./.env my_app_image

This method keeps sensitive information separate from your code and allows for easy configuration changes across different environments.

Remember to secure your .env file with appropriate file permissions.

Docker Compose for Variable Management

Docker Compose excels at managing environment variables across multiple containers. Define variables globally or per service in your docker-compose.yml file:

version: '3.8' services: web: image: my_app_image environment: - APP_MODE=production - DB_USER=${DB_USER} db: image: postgres environment: - POSTGRES_USER=${DB_USER} - POSTGRES_PASSWORD=${DB_PASS}

Docker Compose reads from your .env file or shell environment, syncing settings to each service. You can set default values using ${VARIABLE:-default_value}.

This approach centralizes configuration management, streamlining deployment and enhancing maintainability.

Mastering environment variables in Docker can improve your development process, offering both flexibility and control. By understanding how to manage these settings effectively, you create a seamless workflow that adapts easily to different scenarios.

Get top-notch content with Writio, an AI writer for websites. This article was crafted by Writio.

  1. Docker Inc. Docker Compose environment variables. Docker Documentation. 2023.
  2. Kubernetes. Environment variables. Kubernetes Documentation. 2023.
  3. Mouat N. Using Docker: Developing and Deploying Software with Containers. O'Reilly Media; 2015.

Leave a Reply