DEV Community

Zundamon
Zundamon

Posted on • Updated on

Environment Variables in Docker and Docker Compose - Part 2. Options and Properties

Introduction

This document summarizes command options and properties of compose.yaml.

◯ Overview

1. Flow of Passing Environment Variables

Docker Image

Docker Container

2. Options and Properties

There was some confusion due to the presence or absence of command option names and compose.yaml property names...

  1. docker command
    1. When building a Docker image
      • docker image build command
        1. Key-value format
          • --build-arg option
        2. File format
          • None
    2. When running a Docker container
      • docker container run command
        1. Key-value format
          • --env, -e option
        2. File format
          • --env-file option
  2. compose.yaml file
    1. When building a Docker image
      • services.<service>.build property
        1. Key-value format
          • args property
        2. File format
          • None
    2. When running a Docker container
      • services.<service> property
        1. Key-value format
          • environment property
        2. File format
          • env_file property
  3. docker compose command
    1. When building a Docker image
      • docker compose build command
        1. Key-value format
          • --build-arg option
        2. File format
          • --env-file option
    2. When running a Docker container
      • docker compose up command
        1. Key-value format
          • None
        2. File format
          • --env-file option

◯ Sample Code

Directory names correspond to chapters

gh repo clone domodomodomo/docker-env-sample  
cd docker-env-sample/part2/111

# for Bash - macOS, Ubuntu
bash cmd.sh

# for PowerShell - Windows  
powershell ./cmd.ps1
Enter fullscreen mode Exit fullscreen mode

1. docker command

1.1. When building a Docker image

docker image build command

1.1.1. Key-value format

docker image build \
    --build-arg BLUE=Squirtle \
    --build-arg RED=Charmander \  
    --build-arg GREEN=Bulbasaur \
    .
Enter fullscreen mode Exit fullscreen mode

1.1.2. File format

Not found.

1.2. When running a Docker container

docker container run command

1.2.1. Key-Value Format

--env option or -e option

docker container run \
    --env BLUE=Squirtle \
    --env RED=Charmander \
    --env GREEN=Bulbasaur \
    app
Enter fullscreen mode Exit fullscreen mode

1.2.2. File Format

--env-file option

docker container run \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    app
Enter fullscreen mode Exit fullscreen mode

2. compose.yaml File

2.1. When building a Docker image

services.<service>.build Property

2.1.1. Key-Value Format

args Property

# Map Syntax
services:
  app:
    build:
      context: .
      args:
        BLUE: Squirtle
        RED: Charmander
        GREEN: Bulbasaur
Enter fullscreen mode Exit fullscreen mode
# Array Syntax
services:
  app:
    build:
      context: .
      args:
        - BLUE=Squirtle
        - RED=Charmander
        - GREEN=Bulbasaur
Enter fullscreen mode Exit fullscreen mode

2.1.2. File Format

Not found.

2.2. When running a Docker container

services.<service> Property

2.2.1. Key-Value Format

environment Property

# Map Syntax
services:
  app:
    build:
      context: .
    environment:
      BLUE: Squirtle
      RED: Charmander
      GREEN: Bulbasaur
Enter fullscreen mode Exit fullscreen mode
# Array Syntax
services:
  app:
    build:
      context: .
    environment:
      - BLUE=Squirtle
      - RED=Charmander
      - GREEN=Bulbasaur
Enter fullscreen mode Exit fullscreen mode

2.2.2. File Format

services:
  app:
    build:
      context: .
    env_file:
      - .env.1
      - .env.2
      - .env.3
Enter fullscreen mode Exit fullscreen mode

3. docker compose Command

3.1. When building a Docker image

docker compose build Command

3.1.1. Key-Value Format

--build-arg Option

docker compose build \
    --build-arg BLUE="Squirtle" \
    --build-arg RED="Charmander" \
    --build-arg GREEN="Bulbasaur"
Enter fullscreen mode Exit fullscreen mode

3.1.2. File Format

--env-file Option

docker compose \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    build
Enter fullscreen mode Exit fullscreen mode

◯ good to know

Separating --env-file and --build-arg by the command they belong to can help understand the Docker command format more easily.

docker --log-level debug \
    compose --env-file .env \
    build --build-arg BLUE="Squirtle"
Enter fullscreen mode Exit fullscreen mode
command command-option \
    subcommand subcommand-option \
    subsubcommand subsubcommand-option
Enter fullscreen mode Exit fullscreen mode
  1. --log-level is an option for the docker command*
  2. --env-file is an option for the docker compose subcommand*
  3. --build-arg is an option for the docker compose build sub-subcommand*

3.2. When running a Docker container

docker compose up Command

3.2.1. Key-Value Format

Not found.

3.2.2. File Format

docker compose \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    up
Enter fullscreen mode Exit fullscreen mode

Conclusion

Thank you.

Top comments (0)