Environment Variables in Docker and Docker Compose - Part 2. Options and Properties
Zundamon
Posted on June 9, 2024
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
- Key-value format
-
- When running a Docker container
-
docker container run
command- Key-value format
-
--env
,-e
option
-
- File format
-
--env-file
option
-
- Key-value format
-
- When building a Docker image
-
compose.yaml
file- When building a Docker image
-
services.<service>.build
property- Key-value format
-
args
property
-
- File format
- None
- Key-value format
-
- When running a Docker container
-
services.<service>
property- Key-value format
-
environment
property
-
- File format
-
env_file
property
-
- Key-value format
-
- When building a Docker image
-
docker compose
command- When building a Docker image
-
docker compose build
command- Key-value format
-
--build-arg
option
-
- File format
-
--env-file
option
-
- Key-value format
-
- When running a Docker container
-
docker compose up
command- Key-value format
- None
- File format
-
--env-file
option
-
- Key-value format
-
- When building a Docker image
◯ 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
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
1.2. When running a Docker container
docker container run
command
1.2.1. Key-Value Format
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
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 thedocker
command* -
--env-file
is an option for thedocker compose
subcommand* -
--build-arg
is an option for thedocker 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
3.2.2. File Format
docker compose \
--env-file .env.1 \
--env-file .env.2 \
--env-file .env.3 \
up
Conclusion
Thank you.
Posted on June 9, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024