13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

docker-composeの--build-argオプションを利用して、Dockerfileでビルドされるコンテナまで環境変数を渡す

Posted at

流れ

  1. --build-argオプションを利用して、環境変数をDocker-Composeに渡す
  2. Docker-Composeでは、あらかじめ--build-argに渡す予定の変数を宣言しておく
  3. Docker-Composeから読み込まれるDockerFileでは、ARGで変数を呼び出し、ENVでコンテナへ埋め込む

実行コマンド

docker-compose build --build-arg BUILDID=4d345663-5247-465d-b522-1d3c08b5d243

docker-compose

  • argsで指定しておく
  • この際、変数の値は上書きされるので適当でいい
docker-compose.yaml
version: '3.7'
services:
  app:
    build:                                                                                        
      context: ./app
      dockerfile: ./Dockerfile
      args:    
        - BUILDID=test

Dockerfile

  • ARGで読み込む
  • ENVで埋め込む
ARG BUILDID
ENV buildid=${BUILDID}
13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?