Closed9
docker-compose.yaml の build.args 内の環境変数を dotenv file で指定したい
docker-compose.yaml の environment, env_file 周りの挙動がよくわからないので調べる。
やりたいこととしては、build.args の環境変数の指定を .env.local で行いたい。
こんな感じ↓
version: '3'
services:
myservice:
build:
context: .
dockerfile: Dockerfile
args:
MY_ARG1: ${MY_ARG1}
MY_ARG2: ${MY_ARG2}
MY_ARG3: ${MY_ARG3}
.env.local
MY_ARG1=value1
MY_ARG2=value2
MY_ARG3=value3
config を print してみる。
docker compose -f docker-compose.yaml config
WARN[0000] The "MY_ARG1" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG2" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG3" variable is not set. Defaulting to a blank string.
services:
myservice:
build:
context: /path/to/context
dockerfile: Dockerfile
args:
MY_ARG1: ""
MY_ARG2: ""
MY_ARG3: ""
environment を指定する
version: '3'
services:
myservice:
build:
dockerfile: Dockerfile
args:
MY_ARG1: ${MY_ARG1}
MY_ARG2: ${MY_ARG2}
MY_ARG3: ${MY_ARG3}
environment:
- MY_ARG1=value1
config を print してみる。
docker compose -f docker-compose.yaml config
args の環境変数には展開されない。
WARN[0000] The "MY_ARG1" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG2" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG3" variable is not set. Defaulting to a blank string.
services:
myservice:
build:
context: /path/to/context
dockerfile: Dockerfile
args:
MY_ARG1: ""
MY_ARG2: ""
MY_ARG3: ""
environment:
MY_ARG1: value1
env_file 指定してみる
.env.local
MY_ARG1=value1
MY_ARG2=value2
MY_ARG3=value3
version: '3'
services:
myservice:
build:
dockerfile: Dockerfile
args:
MY_ARG1: ${MY_ARG1}
MY_ARG2: ${MY_ARG2}
MY_ARG3: ${MY_ARG3}
env_file:
- .env.local
config を print してみる。
docker compose -f docker-compose.yaml config
environment に .env.local の内容が反映される。
args の環境変数には展開されない。
WARN[0000] The "MY_ARG1" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG2" variable is not set. Defaulting to a blank string.
WARN[0000] The "MY_ARG3" variable is not set. Defaulting to a blank string.
services:
myservice:
build:
context: /path/to/context
dockerfile: Dockerfile
args:
MY_ARG1: ""
MY_ARG2: ""
MY_ARG3: ""
environment:
MY_ARG1: value1
MY_ARG2: value2
MY_ARG3: value3
.env file なら設定せずにうまくいく
.env
MY_ARG1=value1
MY_ARG2=value2
MY_ARG3=value3
version: '3'
services:
myservice:
build:
dockerfile: Dockerfile
args:
MY_ARG1: ${MY_ARG1}
MY_ARG2: ${MY_ARG2}
MY_ARG3: ${MY_ARG3}
config を print してみる。
docker compose -f docker-compose.yaml config
services:
myservice:
build:
context: /path/to/context
dockerfile: Dockerfile
args:
MY_ARG1: value1
MY_ARG2: value2
MY_ARG3: value3
--env-fle で指定する
--env-file で .env 以外の任意の env file を指定できる。
.env.local
MY_ARG1=local_value1
MY_ARG2=local_value2
MY_ARG3=local_value3
config を print してみる。
docker compose --env-file .env.local -f docker-compose.yaml config
services:
myservice:
build:
context: /path/to/contetxt
dockerfile: Dockerfile
args:
MY_ARG1: local_value1
MY_ARG2: local_value2
MY_ARG3: local_value3
いけた!
まとめ
- docker-compose.yaml 内の
${MY_ENV}は、docker composeコマンド実行時の、実行環境の環境変数が対象- デフォルトで
.envfile が読み込まれて環境変数として利用可能 -
.env以外の env file を利用したい場合は、--env-fileで指定する
- デフォルトで
- docker-compose.yaml の
environment(env_file) は Dockerfile 内で利用したい環境変数の指定
このスクラップは2023/09/19にクローズされました