2
1

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.

gcloud builds するときに、docker buildのoptionを渡したい

Last updated at Posted at 2021-03-10

はじめに

  • コンテナ イメージのビルド
    このへんのドキュメントを見ながら、 gcloud builds をしていると、ときにdocker buildのoptionを渡したくなるときがあると思います。
    • 個人的には --build-arg で外部から値をインジェクションしたくなりました。
  • 結論から言うと、(これを書いた時点では)コマンドライン引数で指定はできないので、 cloudbuild.yaml を使います。

具体例

当初のシンプルなコマンド

gcloud builds submit --tag gcr.io/PROJECT_ID/IMAGE_NAME

これに docker build --build-arg で変数を渡したい(そしてDockerfileの挙動を変えたい)。

どうするか

cloudbuild.yaml を作成

cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['build',
         '--build-arg',
         'FOO1=${_FOO1}',
         '--build-arg',
         '_FOO2=${_FOO2}',
         '-t',
         'gcr.io/$PROJECT_ID/FOOBAR_IMAGE',
         '.'
         ]
substitutions:

substitutions:
    _FOO1: foobar # default value
    _FOO2: foobar # default value
images: [
    'gcr.io/$PROJECT_ID/FOOBAR_IMAGE'
]

^ Dockerfileの ARG FOO1ARG FOO2 が置換される。

作成した cloudbuild.yaml を引数に指定して、以下のように --substitutions の指定も行う。

gcloud builds submit --config=cloudbuild.yaml \
    --substitutions=_FOO1="bar1",_FOO2="bar2"
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?