Closed9

docker-compose.yaml の build.args 内の環境変数を dotenv file で指定したい

nbstshnbstsh

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
nbstshnbstsh

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: ""
nbstshnbstsh

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
nbstshnbstsh

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
nbstshnbstsh

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

docker-compose.yaml 内の ${MY_ENV} は、docker compose コマンド実行時の実行環境の環境変数が対象になるのか。

nbstshnbstsh

--env-fle で指定する

--env-file.env 以外の任意の env file を指定できる。

https://docs.docker.com/compose/environment-variables/set-environment-variables/#substitute-with---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

いけた!

nbstshnbstsh

まとめ

  • docker-compose.yaml 内の ${MY_ENV} は、docker compose コマンド実行時の、実行環境の環境変数が対象
    • デフォルトで .env file が読み込まれて環境変数として利用可能
    • .env 以外の env file を利用したい場合は、--env-file で指定する
  • docker-compose.yaml の environment (env_file) は Dockerfile 内で利用したい環境変数の指定
このスクラップは2023/09/19にクローズされました