GAGA LIFE.

インフラエンジニアブログ

スポンサーリンク

Dockerfileでの構成管理

Dockerfileによる構成管理

Dockerfile(基本構文)

Dockerfile基本書式

命令 引数
Commands Description
FROM ベースイメージ指定
RUN コマンド実行
CMD コンテナの実行コマンド
LABEL ラベルを設定
EXPOSE ポートのエクスポート
ENV 環境変数
ADD ファイル/ディレクトリ追加
COPY ファイルコピー
ENTRYPOINT コンテナの実行コマンド
VOKUME ボリュームマウンド
USER ユーザー指定
WORKDIR 作業ディレクトリ
ARG Dockerfile内の変数
ONBUILD ビルド完了後に実行される命令
STOPSIGNAL システムコールシグナルの設定
HEALTHCHECK コンテナヘルスチェック
SHELL デフォルトシェル設定

記述例:コメント書式

# コメント部分
命令 引数 #コメント部分②

Dockerfile作成

FROM命令(※必須項目)

FROM [イメージ名]
FROM [イメージ名]:[タグ名]
FROM [イメージ名]@[ダイジェスト]

設定例:CentOS7をベースイメージにしたDockerfile

# ベースイメージの設定
FROM centos:centos7

実行例:ダイジェスト確認
※ダイジェスト=イメージを一意に特定する際に利用。Docker Hubにアップすると自動付与される識別子。

docker@LesPaul:~$ docker image ls --digests undercoverism/webserver
REPOSITORY                TAG                 DIGEST                                                                    IMAGE ID            CREATED             SIZE
undercoverism/webserver   1.0                 sha256:204a9a8e65061b10b92ad361dd6f406248404fe60efd5d6a8f2595f18bb37aad   be1f31be9a87        3 weeks ago         109MB

設定例:ダイジェストを指定したDockerfile

# ベースイメージ設定
FROM undercoverism/webserver@sha256:204a9a8e65061b10b92ad361dd6f406248404fe60efd5d6a8f2595f18bb37aad

Dockerfileビルド&イメージレイヤー

DockerfileからのDockerイメージ作成

$ docker build -t [生成するイメージ名]:[タグ名] [Dockerfileの場所]

実行例:Dockerfileの作成

$ mkdir sample && cd $_
$ touch Dockerfile
$ ls
Dockerfile

設定例:Dockerfileの内容

# ベースイメージの設定
FROM centos:centos7

実行例:docker buildコマンド

$ docker build -t sample:1.0 /home/docker/sample/
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM centos:centos7
centos7: Pulling from library/centos
Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
Status: Downloaded newer image for centos:centos7
 ---> 75835a67d134
Successfully built 75835a67d134
Successfully tagged sample:1.0
$ docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
sample                    1.0                 75835a67d134        2 weeks ago         200MB
centos                    centos7             75835a67d134        2 weeks ago         200MB

実行例:新規イメージ作成(タグ名=2.0)

$ docker build -t sample:2.0 /home/docker/sample
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM centos:centos7
 ---> 75835a67d134
Successfully built 75835a67d134
Successfully tagged sample:2.0
$ docker image ls
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos                    centos7             75835a67d134        2 weeks ago         200MB
sample                    1.0                 75835a67d134        2 weeks ago         200MB
sample                    2.0                 75835a67d134        2 weeks ago         200MB

例:ファイル名指定のdocker buildコマンド実行

$ docker build -t sample -f Dockerfile.base

例:標準入力からのビルド

$ docker build - < Dockerfile

実行例:圧縮アーカイブによる標準入力からのビルド

$ tar tvfz docker.tar.gz 
-rw-rw-r-- docker/docker    20 2018-10-28 19:16 Dockerfile
-rw-rw-r-- docker/docker    20 2018-10-29 17:50 dummyfile
$ docker build - < docker.tar.gz
Sending build context to Docker daemon     170B
Step 1/1 : FROM centos:centos7
 ---> 75835a67d134
Successfully built 75835a67d134

Dockerイメージのレイヤー構造

# STEP:1 Ubuntu(ベースイメージ)
FROM ubuntu:latest

# STEP:2 Nginxインストール
RUN apt-get update && apt-get install -y -q nginx

# STEP:3 ファイルコピー
COPY index.html /usr/share/nginx/html

# STEP:4 Nginx起動
CMD ["nginx","-g","daemon off;"]

実行例:イメージレイヤー構造

$ ls -la | grep index
-rwxrwxrwx 1 docker docker  208 1029 18:01 index.html
$ cat index.html 
<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Webサイトをつくってみよう</title>
</head>
<body>
Hello world!
<img src="./images/html5.png" alt="html5ロゴ">
</body>
</html>
$ docker build -t webap .
Sending build context to Docker daemon   5.12kB
Step 1/4 : FROM ubuntu:latest
latest: Pulling from library/ubuntu
473ede7ed136: Pull complete 
c46b5fa4d940: Pull complete 
93ae3df89c92: Pull complete 
6b1eed27cade: Pull complete 
Digest: sha256:29934af957c53004d7fb6340139880d23fb1952505a15d69a03af0d1418878cb
Status: Downloaded newer image for ubuntu:latest
 ---> ea4c82dcd15a
Step 2/4 : RUN apt-get update && apt-get install -y -q nginx
 ---> Running in 8b729d7c3971
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1364 B]

~中略~

Setting up libgd3:amd64 (2.2.5-4ubuntu0.2) ...
Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.1) ...
Setting up nginx-core (1.14.0-0ubuntu1.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.14.0-0ubuntu1.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Removing intermediate container 8b729d7c3971
 ---> 490efe2721a4
Step 3/4 : COPY index.html /usr/share/nginx/html
 ---> 561c6ff715cd
Step 4/4 : CMD ["nginx","-g","daemon off;"]
 ---> Running in 60ea9dc4e0cd
Removing intermediate container 60ea9dc4e0cd
 ---> 0d4eb204077d
Successfully built 0d4eb204077d
Successfully tagged webap:latest

コマンド/デーモン実行

RUN命令(コマンド実行)

$ RUN [実行したいコマンド]

1.Shell形式
設定例:Shell形式のRUN命令

# Nginxインストール
RUN apt-get install -y nginx

2.Exec形式
設定例:Exec形式のRUN命令

# Nginxインストール
RUN ["/bin/bash","-c","apt-get install -y nginx"]

実行例:RUN命令実行例(複数行記述)

$ cat Dockerfile 
# ベースイメージ設定
FROM ubuntu:latest
# RUN命令実行
RUN echo Shell形式です
RUN ["echo","Exec形式です"]
RUN ["/bin/bash","-c","echo 'Exec形式でbash使用'"]
$ docker build -t run-sample .
Sending build context to Docker daemon   5.12kB
Step 1/4 : FROM ubuntu:latest
 ---> ea4c82dcd15a
Step 2/4 : RUN echo Shell形式です
 ---> Running in 2fd1afc5b638
Shell形式です
Removing intermediate container 2fd1afc5b638
 ---> bfaba146ab30
Step 3/4 : RUN ["echo","Exec形式です"]
 ---> Running in 36653da561de
Exec形式です
Removing intermediate container 36653da561de
 ---> cbfa1d81ea31
Step 4/4 : RUN ["/bin/bash","-c","echo 'Exec形式でbash使用'"]
 ---> Running in 5846307183eb
Exec形式でbash使用
Removing intermediate container 5846307183eb
 ---> 858a4939aa16
Successfully built 858a4939aa16
Successfully tagged run-sample:latest
docker@LesPaul:~/sample$ docker history run-sample
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
858a4939aa16        About a minute ago   /bin/bash -c echo 'Exec形式でbash使用'               0B                  
cbfa1d81ea31        About a minute ago   echo Exec形式です                                   0B                  
bfaba146ab30        About a minute ago   /bin/sh -c echo Shell形式です                       0B                  
~中略~

CMD命令(デーモン実行)

$ CMD [実行したいコマンド]

1.Exec形式

CMD ["nginx","-g","daemon off;"]

2.Shell形式

CMD nginx -g 'daemon off;'

3.ENTRYPOINT命令のパラメータとしての記述

$ cat Dockerfile 
# ベースイメージ設定
FROM ubuntu:18.04
# Nginxインストール
RUN apt-get -y update && apt-get -y update
RUN apt-get -y install nginx
# ポート指定
EXPOSE 80
# サーバ実行
CMD ["nginx","-g","daemon off;"]
$ docker build -t cmd-sample
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
docker@LesPaul:~/sample$ docker build -t cmd-sample .
Sending build context to Docker daemon   5.12kB
Step 1/5 : FROM ubuntu:18.04
18.04: Pulling from library/ubuntu
Digest: sha256:29934af957c53004d7fb6340139880d23fb1952505a15d69a03af0d1418878cb
Status: Downloaded newer image for ubuntu:18.04
 ---> ea4c82dcd15a
Step 2/5 : RUN apt-get -y update && apt-get -y update
 ---> Running in a89b0fb9e38a
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [112 kB]

~中略~

Reading package lists...
Removing intermediate container a89b0fb9e38a
 ---> b97ea6ff333b
Step 3/5 : RUN apt-get -y install nginx
 ---> Running in e69b5967182d
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database iproute2 libatm1 libbsd0
  libelf1 libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu60
  libjbig0 libjpeg-turbo8 libjpeg8 libmnl0 libnginx-mod-http-geoip
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
  libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5
  libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 libxtables12 multiarch-support nginx-common nginx-core ucf
Suggested packages:
  iproute2-doc libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database iproute2 libatm1 libbsd0
  libelf1 libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu60
  libjbig0 libjpeg-turbo8 libjpeg8 libmnl0 libnginx-mod-http-geoip
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
  libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5
  libwebp6 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
  libxslt1.1 libxtables12 multiarch-support nginx nginx-common nginx-core ucf
0 upgraded, 40 newly installed, 0 to remove and 2 not upgraded.
Need to get 16.9 MB of archives.
After this operation, 61.4 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 multiarch-support amd64 2.27-3ubuntu1 [6916 B]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libjpeg-turbo8 amd64 1.5.2-0ubuntu5.18.04.1 [110 kB]

~中略~

Setting up libgd3:amd64 (2.2.5-4ubuntu0.2) ...
Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.1) ...
Setting up nginx-core (1.14.0-0ubuntu1.1) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.14.0-0ubuntu1.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Removing intermediate container e69b5967182d
 ---> c30a2e6cc1d3
Step 4/5 : EXPOSE 80
 ---> Running in 75ccb40286e2
Removing intermediate container 75ccb40286e2
 ---> 818e8362a0f1
Step 5/5 : CMD ["nginx","-g","daemon off;"]
 ---> Running in 4408aba6ba38
Removing intermediate container 4408aba6ba38
 ---> 119eeb20ad82
Successfully built 119eeb20ad82
Successfully tagged cmd-sample:latest
$ docker container run -p 80:80 -d cmd-sample
31434c36d45efbd80a94807d75cd7d7cdd30200f96aa1ddad60ba81e6667e4fc

ENTRYPOINT命令(デーモン実行)

ENTRYPOINT [実行したいコマンド]

1.Exec形式

EXTRYPOINT ["nginx","-g","daemon off;"]

2.Shell形式(ENTRYPOINT命令)

ENTRYPOINT nginx -g 'daemon off;'

実行例:ENTRYPOINT命令とCMD命令の組み合わせ例

$ cat Dockerfile 
# Dockerイメージ取得
FROM ubuntu:18.04
# top実行
ENTRYPOINT ["top"]
CMD ["-d","10"]

例:docker container runコマンド例

# CMD命令で指定した10秒毎に更新する場合
$ docker container run -it sample
# 2秒毎に更新する場合
$ docker container run -it sample -d 2

ONBUILD命令(ビルド完了後に実行される命令)

ONBUILD [実行したいコマンド]

STOPSIGNAL命令(システムコールシグナル設定)

STOPSIGNAL [シグナル]

HEALTHCHECK命令(コンテナのヘルスチェック命令)

HEALTHCHECK [オプション] CMD 実行するコマンド
Options Description Default
--interval=n ヘルスチェック間隔 30s
--timeout=n ヘルスチェックのタイムアウト 30s
--retries=N タイムアウト回数 3

設定例:HEALTHCHECK命令

HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1

環境/ネットワーク設定

ENV命令(環境変数設定)

ENV [key] [value]
ENV [key]=[value]

1.key value指定

key value
myName "Undercover"
myOrder Water
myNickName UC

設定例:key value指定

ENV myName "Undercover"
ENV myOrder Water
ENV myNickName UC

2.key=value指定

ENV myName="Undercover" \
    myOrder=Water \
    myNickName=UC

WORKDIR命令(作業ディレクトリ指定)

WORKDIR [作業ディレクトリのパス]

設定例:絶対パス/相対パスによるWORKDIR命令

WORKDIR /first
WORKDIR second
WORKDIR third
RUN ["pwd"]

設定例:WORKDIR命令で環境変数を使用する例

ENV DIRPATH /first
ENV DIRNAME second
WORKDIR $DIRPATH/$DIRNAME
RUN ["pwd"]

USER命令(ユーザー指定)

USER [ユーザー名/UID]

設定例:USER命令の例

RUN ["adduser","undercover"]
RUN ["whoami"]
USER undercover
RUN ["whoami"]

LABEL命令(ラベル指定)

LABEL <キー値>=<値>

設定例:LABEL命令の例

LABEL maintainer "Undercover<undercover@mail.xxx.xxx>"
LABEL title="WebAP"
LABEL version="1.0"
LABEL description="This image is WebApplicationServer"

EXPOSE命令(ポート設定)

EXPOSE <ポート番号>

設定例:EXPOSE命令の例

EXPOSE 8080

ARG命令(Dockerfile内変数設定)

ARG <名前>[=デフォルト値]

設定例:ARG命令の例

ARG YOURNAME="undercover"
RUN echo $YOURNAME

SHELL命令(デフォルトシェル設定)

SHELL ["シェルのパス","パラメータ"]

設定例:RUN命令実行

# デフォルトシェル指定
SHELL ["/bin/bash","-c"]
# RUN命令実行
RUN echo hello

ファイル設定

ADD命令(ファイル/ディレクトリ追加)

ADD <ホストのファイルパス> <Dockerイメージのファイルパス>
ADD ["<ホストのファイルパス>""<Dockerイメージのファイルパス>"]

設定例:ADD命令例

ADD host.html /docker_dir/

例:ADD命令でのパターン例

# [hos]で始まるすべてのファイル追加
ADD hos* /docker_dir/
# [hos] +任意の1文字のルールに当てはまるファイル追加
ADD hos?.txt /docker_dir/

設定例:WORKDIR命令とADD命令例

WORKDIR /docker_dir
ADD host.html web/

COPY命令(ファイルコピー)

COPY <ホストのファイルパス> <Dockerイメージのファイルパス>
COPY ["<ホストのファイルパス>" "<Dockerイメージのファイルパス>"]

VOLUME命令(ボリュームマウント)

VOLUME ["/マウントポイント"]

スポンサーリンク