Passing Environment Variables to Docker Containers

Environment variables are a crucial aspect of containerization, allowing you to decouple your application’s configuration from its code. In this tutorial, we will explore how to pass environment variables to Docker containers.

Introduction to Environment Variables in Docker

Docker provides several ways to set environment variables for containers. You can use the -e or --env flag with the docker run command to set a single variable, or the --env-file flag to load multiple variables from a file.

Setting Environment Variables with the -e Flag

The -e flag allows you to set a single environment variable for a container. The syntax is as follows:

docker run -e VARIABLE_NAME=VALUE image_name

For example, to set an environment variable named DATABASE_URL with the value amazon:rds/connection?string, you would use the following command:

docker run -e DATABASE_URL=amazon:rds/connection?string ubuntu bash

You can also use the -e flag multiple times to set multiple variables:

docker run -e VARIABLE1=value1 -e VARIABLE2=value2 image_name

Setting Environment Variables with the --env-file Flag

If you need to set multiple environment variables, it’s often more convenient to use an environment file. The --env-file flag allows you to load environment variables from a file in the format of VARIABLE_NAME=VALUE.

Here’s an example of how to create an environment file named env.list:

# env.list
VARIABLE1=value1
VARIABLE2=value2

You can then use the --env-file flag to load the variables from the file:

docker run --env-file ./env.list ubuntu bash

Note that comment lines in the environment file should be prefixed with a #.

Using Environment Variables in Docker Compose

If you’re using Docker Compose, you can set environment variables for services in your docker-compose.yml file. For example:

version: '3'
services:
  my-service:
    image: ubuntu
    environment:
      - VARIABLE1=value1
      - VARIABLE2=value2

You can also use the ${VARIABLE_NAME} syntax to reference environment variables set on your host machine:

version: '3'
services:
  my-service:
    image: ubuntu
    environment:
      - MY_VARIABLE=${MY_HOST_VARIABLE}

Passing Environment Variables from the Host Machine

If you need to pass an environment variable defined on your host machine to a Docker container, you can use the ${VARIABLE_NAME} syntax in your docker-compose.yml file. For example:

version: '3'
services:
  my-service:
    image: ubuntu
    environment:
      - MY_VARIABLE=${MY_HOST_VARIABLE}

You can also use the -e flag with the docker run command to pass an environment variable from your host machine:

sudo MY_VARIABLE=my_value docker run -e MY_VARIABLE ubuntu bash

Best Practices

When working with environment variables in Docker, it’s essential to follow best practices to ensure security and flexibility:

  • Avoid hardcoding sensitive information such as database credentials or API keys.
  • Use environment files or Docker Compose to manage multiple environment variables.
  • Use the ${VARIABLE_NAME} syntax to reference environment variables set on your host machine.
  • Keep your environment files secure by controlling access to them.

By following these best practices and using the methods outlined in this tutorial, you can effectively pass environment variables to your Docker containers and keep your application’s configuration flexible and secure.

Leave a Reply

Your email address will not be published. Required fields are marked *