Docker tutorialsをやってみる

Linux 版 Swift を Mac 上で Docker を使って手っ取り早く試す - Qiita
をやってみたいと思いました、その前に Docker の環境作りと練習を。
本当は Parallels Desktop でやりたかったのだけど、情報が少なめなのと、いま入れてるバージョンが10(古い)のでいまひとつよく分からない、ので素直に VirtualBox を入れることにしました。

チュートリアル開始

チュートリアル全体の説明。

Docker Toolbox のインストール、及び Docker Quickstart Terminal の実行。

$ bash --login '/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/start.sh'

Creating Machine default...
Creating CA: /Users/riocampos/.docker/machine/certs/ca.pem
Creating client certificate: /Users/riocampos/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting VM...
Waiting for machine to be running, this may take a few minutes...
Machine is running, waiting for SSH to be available...
Detecting operating system of created instance...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect Docker to this machine, run: /usr/local/bin/docker-machine env default
Setting environment variables for machine default...

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/


docker is configured to use the default machine with IP 192.168.99.100
For help getting started, check out the docs at https://docs.docker.com

docker run hello-world の実行。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world

b901d36b6f2f: Pull complete 
0a6ba66e537a: Pull complete 
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
Status: Downloaded newer image for hello-world:latest

Hello from Docker.
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

docker コマンドの解説。

docker/whalesay イメージの場所の説明、実行。

$ docker run docker/whalesay cowsay boo
Unable to find image 'docker/whalesay:latest' locally
latest: Pulling from docker/whalesay
2880a3395ede: Pull complete 
515565c29c94: Pull complete 
98b15185dba7: Pull complete 
2ce633e3e9c9: Pull complete 
35217eff2e30: Pull complete 
326bddfde6c0: Pull complete 
3a2e7fe79da7: Pull complete 
517de05c9075: Pull complete 
8f17e9411cf6: Pull complete 
ded5e192a685: Pull complete 
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
 _____ 
< boo >
 ----- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

ところで、docker run コマンドですが、続く引数はイメージで、さらにそのイメージを起動した後の実行コマンドと引数を付けることができる、のですね。

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

run | reference | docker

イメージは Docker images コマンドでリストできる。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              0a6ba66e537a        11 weeks ago        960 B
docker/whalesay     latest              ded5e192a685        7 months ago        247 MB

docker/whalesay の実行引数を変更してみる。

$ docker run docker/whalesay cowsay boo-boo
 _________ 
< boo-boo >
 --------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

Dockerfile で指定した自分のイメージをつくる。
なお、ここで指定するのは VirtualBox で実行するコマンドになるので Mac であっても apt-get などになる*1

FROM docker/whalesay:latest

RUN apt-get -y update && apt-get install -y fortunes

CMD /usr/games/fortune -a | cowsay

実行。

$ docker build -t docker-whale .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM docker/whalesay:latest
 ---> ded5e192a685
Step 2 : RUN apt-get -y update && apt-get install -y fortunes
 ---> Running in bde8dbd6ec8f
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [64.4 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [64.4 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Get:3 http://archive.ubuntu.com trusty-updates/main Sources [309 kB]
Get:4 http://archive.ubuntu.com trusty-updates/restricted Sources [5219 B]
Get:5 http://archive.ubuntu.com trusty-updates/universe Sources [183 kB]
Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [851 kB]
Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [23.4 kB]
Get:8 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [431 kB]
Hit http://archive.ubuntu.com trusty Release
Get:9 http://archive.ubuntu.com trusty-security/main Sources [128 kB]
Get:10 http://archive.ubuntu.com trusty-security/restricted Sources [3920 B]
Get:11 http://archive.ubuntu.com trusty-security/universe Sources [36.5 kB]
Get:12 http://archive.ubuntu.com trusty-security/main amd64 Packages [495 kB]
Get:13 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [20.2 kB]
Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [158 kB]
Hit http://archive.ubuntu.com trusty/main Sources
Hit http://archive.ubuntu.com trusty/restricted Sources
Hit http://archive.ubuntu.com trusty/universe Sources
Hit http://archive.ubuntu.com trusty/main amd64 Packages
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Fetched 2773 kB in 12s (228 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  fortune-mod fortunes-min librecode0
Suggested packages:
  x11-utils bsdmainutils
The following NEW packages will be installed:
  fortune-mod fortunes fortunes-min librecode0
0 upgraded, 4 newly installed, 0 to remove and 60 not upgraded.
Need to get 1961 kB of archives.
After this operation, 4817 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe fortune-mod amd64 1:1.99.1-7 [39.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes-min all 1:1.99.1-7 [61.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes all 1:1.99.1-7 [1089 kB]
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: 
Fetched 1961 kB in 15s (125 kB/s)
Selecting previously unselected package librecode0:amd64.
(Reading database ... 13116 files and directories currently installed.)
Preparing to unpack .../librecode0_3.6-21_amd64.deb ...
Unpacking librecode0:amd64 (3.6-21) ...
Selecting previously unselected package fortune-mod.
Preparing to unpack .../fortune-mod_1%3a1.99.1-7_amd64.deb ...
Unpacking fortune-mod (1:1.99.1-7) ...
Selecting previously unselected package fortunes-min.
Preparing to unpack .../fortunes-min_1%3a1.99.1-7_all.deb ...
Unpacking fortunes-min (1:1.99.1-7) ...
Selecting previously unselected package fortunes.
Preparing to unpack .../fortunes_1%3a1.99.1-7_all.deb ...
Unpacking fortunes (1:1.99.1-7) ...
Setting up librecode0:amd64 (3.6-21) ...
Setting up fortune-mod (1:1.99.1-7) ...
Setting up fortunes-min (1:1.99.1-7) ...
Setting up fortunes (1:1.99.1-7) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
 ---> e50c3099531e
Removing intermediate container bde8dbd6ec8f
Step 3 : CMD /usr/games/fortune -a | cowsay
 ---> Running in 5ad27d0d8d44
 ---> 1d657e944bfb
Removing intermediate container 5ad27d0d8d44
Successfully built 1d657e944bfb

この時点でのイメージを確認。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
docker-whale        latest              1d657e944bfb        About a minute ago   274.2 MB
hello-world         latest              0a6ba66e537a        11 weeks ago         960 B
docker/whalesay     latest              ded5e192a685        7 months ago         247 MB

つくったイメージを実行。

$ docker run docker-whale
 ________________________________________ 
/ Beauty is one of the rare things which \
| does not lead to doubt of God.         |
|                                        |
\ -- Jean Anouilh                        /
 ---------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   
$ docker run docker-whale
 _________________________________________ 
/ Once the toothpaste is out of the tube, \
| it's hard to get it back in.            |
|                                         |
\ -- H. R. Haldeman                       /
 ----------------------------------------- 
    \
     \
      \     
                    ##        .            
              ## ## ##       ==            
           ## ## ## ##      ===            
       /""""""""""""""""___/ ===        
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
       \______ o          __/            
        \    \        __/             
          \____\______/   

おまけ:Docker イメージ Docker VirtualMachine のダウンロード先は(Mac の場合)~/.docker/machine/machines/default/ になる。

$ ls -l ~/.docker/machine/machines/default/
total 930240
-rw-r--r--  1 riocampos  staff   31457280  1  4 20:45 boot2docker.iso
-rw-r--r--  1 riocampos  staff       1029  1  4 20:47 ca.pem
-rw-r--r--  1 riocampos  staff       1070  1  4 20:47 cert.pem
-rw-------  1 riocampos  staff       2550  1  4 20:47 config.json
drwx------  6 riocampos  staff        204  1  4 21:10 default
-rw-------  1 riocampos  staff  444792832  1  4 23:24 disk.vmdk
-rw-------  1 riocampos  staff       1679  1  4 20:45 id_rsa
-rw-------  1 riocampos  staff        381  1  4 20:45 id_rsa.pub
-rw-------  1 riocampos  staff       1675  1  4 20:47 key.pem
-rw-------  1 riocampos  staff       1675  1  4 20:47 server-key.pem
-rw-r--r--  1 riocampos  staff       1119  1  4 20:47 server.pem

この中の disk.vmdk に Docker イメージが入ってるのだと思われ(よく分からんけど)。
VM の一覧は docker-machine ls コマンド。

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER   ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1.9.1   

おまけ:docker info コマンド実行結果

$ docker info
Containers: 20
Images: 14
Server Version: 1.9.1
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 54
 Dirperm1 Supported: true
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 4.1.13-boot2docker
Operating System: Boot2Docker 1.9.1 (TCL 6.4.1); master : cef800b - Fri Nov 20 19:33:59 UTC 2015
CPUs: 1
Total Memory: 1.956 GiB
Name: default
ID: RYHF:EQZ3:BIII:PWN2:6LQ6:WO3V:VMHV:PAWR:H2AY:TELO:Z3WM:NDDR
Debug mode (server): true
 File Descriptors: 11
 Goroutines: 20
 System Time: 2016-01-04T14:23:07.83919482Z
 EventsListeners: 0
 Init SHA1: 
 Init Path: /usr/local/bin/docker
 Docker Root Dir: /mnt/sda1/var/lib/docker
Labels:
 provider=virtualbox

これ以降

自分のイメージをアップするとかの部分は省略。

Docker Toolbox の解説である
Installation on Mac OS X
を読んでいくのが良さそう。なお Docker Machine を使った場合の説明であり、廃止された Boot2Docker のものではない、とのこと。

Docker 関係リンク

「Quickstartがやることをコマンドで実行する場合」が載っているのが良記事。

一つ上と同じ著者。

2015/1/12版。

眼で見て学習。

いろいろ便利。

*1:わざわざ brew に変更してしまって動作せず悩んだorz