Environment Variables in Docker and Docker Compose - Part 2. Options and Properties
Published: 2024-06-09 00:41:56


## Introduction

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

### ◯ Overview

#### 1. Flow of Passing Environment Variables


![Docker Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmur1234ciffngnozppy.jpg)


![Docker Container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh648y6u1iomg7jiwjq0.jpg)


#### 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
```

* https://github.com/domodomodomo/docker-env-sample



---


## 1. docker command


### 1.1. When building a Docker image

[docker image build](https://docs.docker.com/reference/cli/docker/image/build/) command

#### 1.1.1. Key-value format

```bash
docker image build \
    --build-arg BLUE=Squirtle \
    --build-arg RED=Charmander \  
    --build-arg GREEN=Bulbasaur \
    .
```


#### 1.1.2. File format

Not found.

* Pages checked
    * [Options - docker image build](https://docs.docker.com/reference/cli/docker/image/build/#options)




### 1.2. When running a Docker container

[`docker container run`](https://docs.docker.com/reference/cli/docker/container/run/) command


#### 1.2.1. Key-Value Format

[`--env`](https://bit.ly/45crk4r) option or [`-e`](https://bit.ly/45crk4r) option

```bash
docker container run \
    --env BLUE=Squirtle \
    --env RED=Charmander \
    --env GREEN=Bulbasaur \
    app
```

#### 1.2.2. File Format

[`--env-file`](https://bit.ly/45crk4r) option

```bash
docker container run \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    app
```




---

## 2. compose.yaml File

### 2.1. When building a Docker image

[`services.<service>.build`](https://docs.docker.com/compose/compose-file/build/) Property


#### 2.1.1. Key-Value Format

[`args`](https://bit.ly/3Kw92Se) Property

```yaml
# Map Syntax
services:
  app:
    build:
      context: .
      args:
        BLUE: Squirtle
        RED: Charmander
        GREEN: Bulbasaur
```
```yaml
# Array Syntax
services:
  app:
    build:
      context: .
      args:
        - BLUE=Squirtle
        - RED=Charmander
        - GREEN=Bulbasaur
```

#### 2.1.2. File Format

Not found.

* Checked Pages
    * [`services.<service>.build`](https://docs.docker.com/compose/compose-file/build/)




### 2.2. When running a Docker container

[`services.<service>`](https://docs.docker.com/compose/compose-file/05-services/) Property


#### 2.2.1. Key-Value Format

[`environment`](https://bit.ly/4aQqifF) Property

```yaml
# Map Syntax
services:
  app:
    build:
      context: .
    environment:
      BLUE: Squirtle
      RED: Charmander
      GREEN: Bulbasaur
```
```yaml
# Array Syntax
services:
  app:
    build:
      context: .
    environment:
      - BLUE=Squirtle
      - RED=Charmander
      - GREEN=Bulbasaur
```



#### 2.2.2. File Format

```yaml
services:
  app:
    build:
      context: .
    env_file:
      - .env.1
      - .env.2
      - .env.3
```



---

## 3. docker compose Command

### 3.1. When building a Docker image

[`docker compose build`](https://docs.docker.com/reference/cli/docker/compose/build/) Command

#### 3.1.1. Key-Value Format

[`--build-arg`](https://bit.ly/3xaWZX9) Option

```bash
docker compose build \
    --build-arg BLUE="Squirtle" \
    --build-arg RED="Charmander" \
    --build-arg GREEN="Bulbasaur"
```

#### 3.1.2. File Format

[`--env-file`](https://bit.ly/3RcrGlK) Option

```bash
docker compose \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    build
```

#### ◯ good to know

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

```bash
docker --log-level debug \
    compose --env-file .env \
    build --build-arg BLUE="Squirtle"
```
```
command command-option \
    subcommand subcommand-option \
    subsubcommand subsubcommand-option
```

1. `--log-level` is an option for the `docker` command[*](https://docs.docker.com/reference/cli/docker/#options)
2. `--env-file` is an option for the `docker compose` subcommand[*](https://docs.docker.com/reference/cli/docker/compose/#options)
3. `--build-arg` is an option for the `docker compose build` sub-subcommand[*](https://docs.docker.com/reference/cli/docker/compose/build/#options)


### 3.2. When running a Docker container

[`docker compose up`](https://docs.docker.com/reference/cli/docker/compose/up/) Command

#### 3.2.1. Key-Value Format

Not found.

* Checked Pages
    * [Options - docker compose](https://docs.docker.com/reference/cli/docker/compose/#options)
    * [Options - docker compose up](https://bit.ly/3X9sUlr)


#### 3.2.2. File Format

```bash
docker compose \
    --env-file .env.1 \
    --env-file .env.2 \
    --env-file .env.3 \
    up
```



---

## Conclusion

Thank you.

* [Part 1. ARG and ENV - Hashnode](https://bit.ly/4ebZqtj)
* [Part 2. Options and Properties - DEV Community](https://bit.ly/3yO7LmG)
* [Part 3. Overall Flow - Hashnode](https://bit.ly/45i3S5Q)