メモ書き

自分が後で見直すためにつけている作業のメモ書きです.それ以上の意味はありません

Dockerfile でイメージを生成

docker には,コンテナ構築手順を記載したスクリプト(Dockerfile)を用いてコマンド実行時にイメージを作成する機能があるのでその確認.

Dockerfile を用意

まずは次のようなスクリプトをカレントディレクトリに作ってみる.

FROM ubuntu:14.10
MAINTAINER 名前 <メールアドレス>
RUN apt-get update
RUN apt-get upgrade -y

docker build を実行してイメージを作る

docker build にはスクリプトの存在するパスまたはURLを指定するための引数が最低一つは必要.

# カレントディレクトリにファイルがある場合なので,"." を指定している 
$ docker build .
Sending build context to Docker daemon 5.632 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:14.10
14.10: Pulling from ubuntu

c87d97ed7d49: Pull complete 
3ed890b0e33e: Pull complete 
e9938c931006: Pull complete 
ab57dbafeeea: Already exists 
ubuntu:14.10: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.

Digest: sha256:789f05c5677eb52855df9e87ae617b41b64c4b15c627a25a63cf1e64500eca7c
Status: Downloaded newer image for ubuntu:14.10
 ---> ab57dbafeeea
Step 1 : MAINTAINER 名前 <アドレス>
 ---> Running in 6bd016f8a3c6
 ---> 3934ad113806
Removing intermediate container 6bd016f8a3c6
Step 2 : RUN apt-get update
 ---> Running in 1bb32947f47c
Ign http://archive.ubuntu.com utopic InRelease
Ign http://archive.ubuntu.com utopic-updates InRelease
Ign http://archive.ubuntu.com utopic-security InRelease
Hit http://archive.ubuntu.com utopic Release.gpg
Get:1 http://archive.ubuntu.com utopic-updates Release.gpg [933 B]
(中略)
Get:22 http://archive.ubuntu.com utopic-security/universe amd64 Packages [100 kB]
Fetched 20.7 MB in 28s (727 kB/s)
Reading package lists...
 ---> 4dbaa028ee64
Removing intermediate container 1bb32947f47c
Step 3 : RUN apt-get upgrade -y
 ---> Running in e1a317246300
Reading package lists...
Building dependency tree...
Reading state information...
The following packages will be upgraded:
  isc-dhcp-client isc-dhcp-common libpython3.4-minimal libpython3.4-stdlib
  python3.4 python3.4-minimal
6 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 5715 kB of archives.
After this operation, 1024 B of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ utopic-updates/main python3.4 amd64 3.4.2-1ubuntu0.1 [173 kB]
(略)
Removing intermediate container e1a317246300
Successfully built 8a60500e9329

一応成功したっぽいが,途中で以下のエラーらしきものが出ている.

debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin: 

これは,以下を参考にして,github.com

次の行をコマンド実行前の箇所に追加すれば良い.

ENV DEBIAN_FRONTEND noninteractive

(追記)
と,思っていたら Dockerfile のリファレンスに次のような文があった.

Note: Environment persistence can cause unexpected effects. For example, setting ENV DEBIAN_FRONTEND noninteractive may confuse apt-get users on a Debian-based image. To set a value for a single command, use RUN <key>=<value> <command>.

ので,おそらく次のようにしておいた方が無難である.

RUN DEBIAN_FRONTEND=noninteractive apt-get upgrade -y

ここで,イメージの一覧を表示

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              8a60500e9329        37 minutes ago      245.1 MB
ubuntu              14.10               ab57dbafeeea        3 weeks ago         194.5 MBMB

ubuntu は今回のイメージ作成の元として使用したイメージ.それを元に作成したイメージは名無しになってしまっている.
イメージ名(と,タグ名)がないと何かと不便なのでイメージ作成時に -t オプションを使って指定する.

$ docker build -t myubuntu1410:latest .
Sending build context to Docker daemon 5.632 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:14.10
 ---> ab57dbafeeea
(略)
Step 2 : ENV DEBIAN_FRONTEND noninteractive
 ---> Running in d7eeeb9e1d0e
 ---> bb2d44d6fb76
Removing intermediate container d7eeeb9e1d0e
Step 3 : RUN apt-get update
 ---> Running in 73c8a97cd386
(略)
Step 4 : RUN apt-get upgrade -y
 ---> Running in 88f9f670ec70
(略)
Successfully built 036fd49a7d50

それでイメージを確認すると,イメージに指定した名前が付けられていることが確認できた.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myubuntu1410        latest              036fd49a7d50        6 minutes ago       245.1 MB
<none>              <none>              8a60500e9329        37 minutes ago      245.1 MB
ubuntu              14.10               ab57dbafeeea        3 weeks ago         194.5 MB

とりあえずここまで.