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
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
–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
–env BLUE=Squirtle
–env RED=Charmander
–env GREEN=Bulbasaur
app
1.2.2. File Format
–env-file option
–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
services:
app:
build:
context: .
args:
– BLUE: Squirtle
– RED: Charmander
– GREEN: Bulbasaur
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
services:
app:
build:
context: .
environment:
BLUE: Squirtle
RED: Charmander
GREEN: Bulbasaur
services:
app:
build:
context: .
environment:
– BLUE=Squirtle
– RED=Charmander
– GREEN=Bulbasaur
2.2.2. File Format
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
–build-arg BLUE=“Squirtle”
–build-arg RED=“Charmander”
–build-arg GREEN=“Bulbasaur”
3.1.2. File Format
–env-file Option
–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.
compose –env-file .env
build –build-arg BLUE=“Squirtle”
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
–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.