docker commitまでメモ


### 環境
CentOS 6.5 on VirtualBox

### Dockerインストール
```console:
# yum install docker-io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.tsukuba.wide.ad.jp
 * extras: ftp.tsukuba.wide.ad.jp
 * updates: centos.tt.co.kr
Setting up Install Process
No package docker-io available.
Error: Nothing to do
```


[document](https://docs.docker.com/installation/rhel/)に従いEPELを入れて再度試す

```console:
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
Retrieving http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.SXgnRV: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

# yum install docker-io

# docker -v
Docker version 1.3.2, build 39fa2fa/1.3.2

# service docker start
Starting cgconfig service:                                 [  OK  ]
Starting docker:                                       [  OK  ]
```

### rootユーザーでコンテナ作成

[このへん](http://blog.ieknir.com/blog/try-docker/)がわかりやすい。結果的には、後述の作業で一般ユーザーに以下の作業を許可したほうがよい気がする。

```console:
# docker run centos /bin/echo "hello world"
Unable to find image 'centos' locally
centos:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete 
5b12ef8fd570: Pull complete 
34943839435d: Pull complete 
Status: Downloaded newer image for centos:latest
hello world

# docker run centos /bin/cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 
```
動いた。:beers:


### [Docker Hub](https://registry.hub.docker.com/_/centos/)上のイメージをpullしてみる

```console:
# docker pull centos:centos6

# docker run centos:centos6 /bin/cat /etc/redhat-release 
CentOS release 6.6 (Final)
```

### コンテナ起動

```console:
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

# docker run --name="intro" -t  -i -d 25c5 /bin/bash
722f3a7dad6897d9749ac93b979ae8d8c420220af8728bc326cc76f32ae857aa

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
722f3a7dad68        centos:centos6      "/bin/bash"         14 seconds ago      Up 13 seconds                           intro       

# docker attach 722f3a7dad68
[root@722f3a7dad68 /]# 

```

### 変更を保存する
変更を加える。例えばユーザーを追加してみる。

```console:
[root@722f3a7dad68 /]# useradd myuser
[root@722f3a7dad68 /]# tail -1 /etc/passwd
myuser:x:500:500::/home/myuser:/bin/bash
```
detach(`CTRL-p`+`CTRL-q`) する。`exit`ではない。その後`commit`。

```console:
# docker commit -m "useradd" 722f3a7dad68 mycontainer 
4d15b0eac3b0f2e41b607fef77b6bccf507f709cf8d4453d6e8c44e20e4a877a

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycontainer         latest              4d15b0eac3b0        47 seconds ago      215.9 MB
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB
```
新しいイメージ`4d15b0eac3b0`ができた。名前(mycontainer)は適当すぎたか。

### 変更を保存しない場合
再び`attach`してもう一人ユーザー追加。

```console:
[root@722f3a7dad68 /]# useradd myuser2
[root@722f3a7dad68 /]# tail -2  /etc/passwd
myuser:x:500:500::/home/myuser:/bin/bash
myuser2:x:501:501::/home/myuser2:/bin/bash
```

detachして、保存したイメージからコンテナ起動

```console:
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
722f3a7dad68        centos:centos6      "/bin/bash"         50 minutes ago      Up 50 minutes                           intro  

# docker run --name="intro2" -t  -i -d 4d /bin/bash
4942e5a4d043209032b0c2e76d6cbadfbc3d63f5b6bfaa2e4cf5dda6108d8997

# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         13 seconds ago      Up 12 seconds                           intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         52 minutes ago      Up 51 minutes                           intro    

# docker attach 49
[root@4942e5a4d043 /]# tail -2 /etc/passwd
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
myuser:x:500:500::/home/myuser:/bin/bash

```
保存済の変更点から再開した。（保存後に追加した2人目のユーザーはいない）


### 散らかしたのでコンテナ片付け
```console:
# docker ps
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         5 minutes ago       Up 5 minutes                            intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         56 minutes ago      Up 56 minutes                           intro        

# docker stop 4942e5a4d043
4942e5a4d043

# docker stop 722f3a7dad68
722f3a7dad68

# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
```
`stop`しても残っているので`rm`する


```
# docker ps -a
CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS                      PORTS               NAMES
4942e5a4d043        mycontainer:latest   "/bin/bash"         14 minutes ago      Exited (-1) 8 minutes ago                       intro2              
722f3a7dad68        centos:centos6       "/bin/bash"         About an hour ago   Exited (-1) 8 minutes ago                       intro       

# docker rm 4942e5a4d043
4942e5a4d043

# docker rm 722f3a7dad68
722f3a7dad68

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
```

### イメージも片付け

```
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycontainer         latest              4d15b0eac3b0        50 minutes ago      215.9 MB
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB

# docker rmi 4d15b0eac3b0
Untagged: mycontainer:latest
Deleted: 4d15b0eac3b0f2e41b607fef77b6bccf507f709cf8d4453d6e8c44e20e4a877a

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB
centos              latest              34943839435d        2 weeks ago         224 MB
```

### 一般ユーザーでやってみる
[document](https://docs.docker.com/articles/basics/)に倣い、一般ユーザーを`docker`グループに追加

```console:
$ sudo usermod -a -G docker satzz


$ sudo vi /etc/sysconfig/docker
# /etc/sysconfig/docker
#
# Other arguments to pass to the docker daemon process
# These will be parsed by the sysv initscript and appended
# to the arguments list passed to docker -d

#other_args=
other_args="-H 127.0.0.1:4243" # add


$ export DOCKER_HOST=tcp://127.0.0.1:4243

$ docker pull centos:centos6
centos:centos6: The image you are pulling has been verified
511136ea3c5a: Already exists 
5b12ef8fd570: Already exists 
25c5298b1a36: Already exists 
Status: Image is up to date for centos:centos6
```
既存のイメージが使えるとのこと。。`DOCKER_HOST`は`.bashrc`などに書いておくべきでしょうね。

```console:
$ docker run --name="intro" -t  -i -d 25c5 /bin/bash
0c890bd6d900ae4cc609fb85b7ea36ba91b93bd96bd8c6628f8aa717b4dcdd2c

$ docker attach 0c890bd6d900ae4cc609fb85b7ea36ba91b93bd96bd8c6628f8aa717b4dcdd2c
[root@0c890bd6d900 /]# 

```
`run`と`attach`もできた。

### detachでなく`exit`するとどうなるのか

```
$ docker run --name="intro" -t  -i -d 25c5 /bin/bash
3b2d8ffaaac414f3a5e96fddb2ff7303be8c6db5a615cca2f81b9cabde2d7410

$ docker attach 3b2d8ffaaac414f3a5e96fddb2ff7303be8c6db5a615cca2f81b9cabde2d7410

[root@3b2d8ffaaac4 /]# exit
exit

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
3b2d8ffaaac4        centos:centos6      "/bin/bash"         15 seconds ago      Exited (0) 3 seconds ago                       intro

$ docker attach 3b2d8ffaaac4
2014/12/18 18:49:59 You cannot attach to a stopped container, start it first
```
`STATUS`が`UP`だったのが`Exited`になる。動いていないので`attach`できないが、`start`すれば`attach`できる。

```
$ docker start 3b2d8ffaaac4
3b2d8ffaaac4

$ docker attach 3b2d8ffaaac4
[root@3b2d8ffaaac4 /]# 

```