Docker -- Boot2docker

とりあえず、Mac OSX 10.9 に、入れてみる。

Install

ここInstalling Docker on Mac OS Xの指示に従う。

VirtualBoxがインストールされる。
すでに入れてあったので、インストールはしなかったみたいだ。。



boot2docker 実行

クリックすると、こんな感じで、Terminalが開く。

bash
Last login: Sun Sep 14 17:59:05 on ttys000
MBA20120331:~ guutara$ bash
[MBA20120331:~ guutara]$ unset DYLD_LIBRARY_PATH ; unset LD_LIBRARY_PATH
mkdir -p ~/.boot2docker
[MBA20120331:~ guutara]$ mkdir -p ~/.boot2docker
if [ ! -f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2docker.iso ~/.boot2docker/ ; fi
boot2docker/ ; fiutara]$ if [ ! -f ~/.boot2docker/boot2docker.iso ]; then cp /usr/local/share/boot2docker/boot2docker.iso ~/. 
/usr/local/bin/boot2docker init 
/usr/local/bin/boot2docker up && export DOCKER_HOST=tcp://$(/usr/local/bin/boot2docker ip 2>/dev/null):2375
docker version
[MBA20120331:~ guutara]$ /usr/local/bin/boot2docker init 
Generating public/private rsa key pair.
Your identification has been saved in /Users/guutara/.ssh/id_boot2docker.
Your public key has been saved in /Users/guutara/.ssh/id_boot2docker.pub.
The key fingerprint is:
da:15:10:5d:ae:16:87:59:0c:8d:11:81:12:6e:39:81 guutara@MBA20120331.local
The key's randomart image is:

l):237520331:~ guutara]$ /usr/local/bin/boot2docker up && export DOCKER_HOST=tcp://$(/usr/local/bin/boot2docker ip 2>/dev/nul 
Waiting for VM and Docker daemon to start...
.............................................
Started.

To connect the Docker client to the Docker daemon, please set:
    export DOCKER_HOST=tcp://192.168.59.103:2375

[MBA20120331:~ guutara]$ docker version
Client version: 1.2.0
Client API version: 1.14
Go version (client): go1.3.1
Git commit (client): fa7b24f
OS/Arch (client): darwin/amd64
Server version: 1.2.0
Server API version: 1.14
Go version (server): go1.3.1
Git commit (server): fa7b24f 

この時点で、VirtulBoxに、VMが起動している。


docker pull

使用するコンテナのイメージを取って来る。とりあえず、centOSの7にした。

[MBA20120331:~ guutara]$ docker pull centos:centos7
Pulling repository centos
70214e5d0a90: Download complete 
511136ea3c5a: Download complete 
34e94e67e63a: Download complete 

docker images

持っているコンテナのイメージを確認する。

[MBA20120331:~ guutara]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos7             70214e5d0a90        2 weeks ago         224 MB

docker run -t -i

コンテナを起動する。
起動する際に、-t で、tty を確保して、-i で、標準出力を開き、/bin/bash で端末アクセスする。
exit で、抜けると停止する。

[MBA20120331:~ guutara]$ docker run -t -i --name centos7-up centos:centos7 /bin/bash
bash-4.2# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  2.7  0.1  11744  2864 ?        Ss   10:46   0:00 /bin/bash
root         9  0.0  0.1  19752  2164 ?        R+   10:46   0:00 ps aux
bash-4.2# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core) 
bash-4.2# uname -a
Linux b35fd83296dd 3.16.1-tinycore64 #1 SMP Fri Aug 22 06:40:10 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
bash-4.2# exit
exit

docker start -i

停止したコンテナを起動する。
起動する際に、-t で、tty を確保して端末アクセスする。
exit で抜けると、停止する。

[MBA20120331:~ guutara]$ docker start -i centos7-up
centos7-up
bash-4.2# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core) 
bash-4.2# uname -a
Linux b35fd83296dd 3.16.1-tinycore64 #1 SMP Fri Aug 22 06:40:10 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
bash-4.2# exit
exit

yum update

最新の状態にコンテナをアップデートしてみる。

[MBA20120331:~ guutara]$ docker start -i centos7-up
centos7-up
bash-4.2# yum update
Loaded plugins: fastestmirror
base                                                                                                      | 3.6 kB  00:00:00     
extras                                                                                                    | 3.4 kB  00:00:00     
updates                                                                                                   | 3.4 kB  00:00:00     
(1/4): extras/7/x86_64/primary_db                                                                         |  26 kB  00:00:00     
(2/4): base/7/x86_64/group_gz                                                                             | 157 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                                                        | 2.9 MB  00:00:02     
(4/4): base/7/x86_64/primary_db                                                                           | 4.9 MB  00:00:03     
Determining fastest mirrors
.
.
.

Complete!
bash-4.2# exit

docker commit

update したコンテナのイメージを作成する。
images で、確認しておく。

[MBA20120331:~ guutara]$ docker commit centos7-up chung/centos7-up
3a22ebba07c7741599a28eb21997f1cf6dbfdeb2202d230576051fc34d6eb4a6
[MBA20120331:~ guutara]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
chung/centos7-up    latest              3a22ebba07c7        34 seconds ago      288.3 MB
centos              centos7             70214e5d0a90        2 weeks ago         224 MB

docker rm

コンテナを削除する。
これで、yum update したコンテナは削除される。

MBA20120331:~ guutara$  docker rm centos7-up
centos7-up

docker ps -a

コンテナの状態を確認する。

  • a で、動いていないコンテナも表示する。
MBA20120331:~ guutara$  docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

MBA20120331:~ guutara$  docker run -d -i -t --name centos7-chung chung/centos7-up /bin/bash
df0684c80b17d8a90546cf7829e67f326fd172b5ea949bea00b966003c366878
MBA20120331:~ guutara$  docker run -d -i -t --name centos7-org centos:centos7 /bin/bash
ea817f2e6e08c3686680d6660eedd1d973962a0a01f24a2bbd248729d0e0f330
MBA20120331:~ guutara$  docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
ea817f2e6e08        centos:centos7            "/bin/bash"         7 seconds ago       Up 4 seconds                            centos7-org         
df0684c80b17        chung/centos7-up:latest   "/bin/bash"         35 seconds ago      Up 33 seconds                           ce

docker run -d , docker attach

デーモンで動かす時には、-d を付ける。
動かしたまま、抜けるには、CTR-p,CTR-q で、抜ける。

docker start -i でも、再接続可能だけど、attachで、繋げられる。

MBA20120331:~ guutara$  docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
ea817f2e6e08        centos:centos7            "/bin/bash"         5 minutes ago       Up 5 minutes                            centos7-org         
df0684c80b17        chung/centos7-up:latest   "/bin/bash"         6 minutes ago       Up 5 minutes                            centos7-chung       
MBA20120331:~ guutara$  docker attach centos7-org

bash-4.2# ps
  PID TTY          TIME CMD
    1 ?        00:00:00 bash
    8 ?        00:00:00 ps
CTR-p,CTR-q

[MBA20120331:~ guutara]$

MBA20120331:~ guutara$  docker ps -a
CONTAINER ID        IMAGE                     COMMAND             CREATED             STATUS              PORTS               NAMES
ea817f2e6e08        centos:centos7            "/bin/bash"         10 minutes ago      Up 10 minutes                           centos7-org         
df0684c80b17        chung/centos7-up:latest   "/bin/bash"         10 minutes ago      Up 10 minutes                           centos7-chung       

boot2docker stop

VMを止めておく。

MBA20120331:~ guutara$ boot2docker stop