Enviroment Variables In Docker

Enviroment Variables In Docker

Introduction

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

◯ Overview

1. Flow of Passing Environment Variables

2. Options and Properties

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

docker command

When building a Docker image

docker image build command

Key-value format

–build-arg option

File format

None

When running a Docker container

docker container run command

Key-value format

–env, -e option

File format

–env-file option

compose.yaml file

When building a Docker image

services.<service>.build property

Key-value format

args property

File format

None

When running a Docker container

services.<service> property

Key-value format

environment property

File format

env_file property

docker compose command

When building a Docker image

docker compose build command

Key-value format

–build-arg option

File format

–env-file option

When running a Docker container

docker compose up command

Key-value format

None

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 command

1.1.1. Key-value format

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

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

1.2.2. File Format

–env-file option

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 Property

2.1.1. Key-Value Format

args Property

# Map Syntax
services:
app:
build:
context: .
args:
BLUE: Squirtle
RED: Charmander
GREEN: Bulbasaur
# 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

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
# Array Syntax
services:
app:
build:
context: .
environment:
BLUE=Squirtle
RED=Charmander
GREEN=Bulbasaur

2.2.2. File Format

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 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”

3.1.2. File Format

–env-file Option

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.

docker –log-level debug
compose –env-file .env
build –build-arg BLUE=“Squirtle”
command command-option
subcommand subcommand-option
subsubcommand subsubcommand-option

–log-level is an option for the docker command*

–env-file is an option for the docker compose subcommand*

–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.

Checked Pages

Options – docker compose
Options – docker compose up

3.2.2. File Format

docker compose
–env-file .env.1
–env-file .env.2
–env-file .env.3
up

Conclusion

I have seen people struggling with the following issues:

docker-compose cannot understand my env_file – Stackoverflow

To avoid encountering similar issues…

◯ Question

I created some questions. Choose the appropriate word.

When you press docker container run app

Environment variables defined in .env can be used | cannot be used in Dockerfile

When you press docker container run –env-file .env.local app

Environment variables defined in .env.local can be used in Dockerfile | compose.yaml

When nothing is specified in the services.service.env_file property of compose.yaml

Environment variables defined in .env can be used | cannot be used in Dockerfile

When .env.local is specified in the services.service.env_file property of compose.yaml

Environment variables defined in .env.local can be used in Dockerfile | compose.yaml

When you press docker compose up or build

Environment variables defined in .env can be used in compose.yaml

When you press docker compose –env-file .env.local up or build

Environment variables defined in .env can be used | cannot be used in compose.yaml

Environment variables defined in .env.local can be used in Dockerfile | compose.yaml

◯ Answer

When you press docker container run app

Environment variables defined in .env cannot be used in Dockerfile

When you press docker container run –env-file .env.local app

Environment variables defined in .env.local can be used in Dockerfile

When nothing is specified in the services.service.env_file property of compose.yaml

Environment variables defined in .env cannot be used in Dockerfile

When .env.local is specified in the services.service.env_file property of compose.yaml

Environment variables defined in .env.local can be used in Dockerfile

When you press docker compose up or build

Environment variables defined in .env can be used in compose.yaml

When you press docker compose –env-file .env.local up or build

Environment variables defined in .env cannot be used in `compose.yaml**
Environment variables defined in .env.local can be used in Dockerfile | compose.yaml

Thank you.

Please follow and like us:
Pin Share