24
13

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 5 years have passed since last update.

Dockerfileで任意の引数を受ける(--build-args)

Last updated at Posted at 2017-04-06

目的

Dockerでコンテナイメージをビルドする環境の都合上、
同一Dockerfileを使いながら内部のパラメータを変更したいことがある。
そんな事象にどうやって対応するかを説明。

前提

  • Ubuntu 16.04.1
  • Docker 1.13.0

概要

Dockerfileの中でARG句(?)を用いることで
--build-argによる設定を受け付けられるようにでき、
コンテナイメージをビルドする際に任意のパラメータを含めることができる。

具体例

Dockerfile
FROM busybox:latest
ARG REPO="www.example.com"
RUN echo $REPO

ARG REPOの部分で、REPOにwww.example.comを与えている。
何もせずにビルドするとwww.example.comの文字列が表示される。

ARG REPO='www.example.com'の部分で、REPOのデフォルトの値として
www.example.comを指定している。

--build-argを指定せずにビルド
$ sudo docker build -t arg_sample:default .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM busybox:latest
 ---> 00f017a8c2a6
Step 2/3 : ARG REPO="www.example.com"
 ---> Running in e79c545c3047
 ---> 2ec3517ca6c3
Removing intermediate container e79c545c3047
Step 3/3 : RUN echo $REPO
 ---> Running in cde7f37ccdb7
www.example.com
 ---> aba88a71e772

では、次に--build-argでREPOに対して値(www.example.**org**)を入れる。

--build-argを指定してビルド
$ sudo docker build -t arg_sample:arg_given --build-arg REPO='www.example.org' .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM busybox:latest
---> 00f017a8c2a6
Step 2/3 : ARG REPO="www.example.com"
---> Using cache
---> 2ec3517ca6c3
Step 3/3 : RUN echo $REPO
---> Running in bbcd61d682ac
www.example.org
---> 3e540692c9ff
Removing intermediate container bbcd61d682ac
Successfully built 3e540692c9ff

echo $REPOの部分で、www.example.orgが表示されている。

24
13
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
24
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?