docker tutorial on Ubuntu

今更だけど、dockerの使い方を自分なりにまとめてみる。
dockerのframeworkなどはここでは一切記載せず、あくまで使い方のみメモ。

###environment
```
>uname -a
Linux tomoyafujita-Inspiron-620s 4.4.0-37-generic #56-Ubuntu SMP Mon Aug 29 16:49:59 UTC 2016 i686 i686 i686 GNU/Linux
>cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
```

###install
このまま[docker install](https://docs.docker.com/)

###command example
dockerはimageからcontainerを起動してcontainerを操作する。
containerはprocessのように振る舞い、操作しているシェルにattach/detachなどもできる。
(solarisで言うところのzoneに等しい。imageはrepositoryのようなイメージ)

```
[docker hubからimage検索]
>sudo docker search centos
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                 The official build of CentOS.                   2756      [OK]  
...

[docker hubからimageをpull]
>sudo docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8d30e94188e7: Pull complete 
Digest: sha256:2ae0d2c881c7123870114fb9cc7afabd1e31f9888dac8286884f6cf59373ed9b
Status: Downloaded newer image for centos:latest

[localのimage一覧表示]
>sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              980e0e4c79ec        6 weeks ago         196.8 MB

[image idを指定してimageの詳細をprint]
>sudo docker inspect 980e0e4c79ec
[
    {
        "Id": "sha256:980e0e4c79ec933406e467a296ce3b86685e6b42eed2f873745e6a91d718e37a",
        "RepoTags": [
            "centos:latest"
        ],
...

[image削除]
>sudo docker rmi 980e0e4c79ec
...

[docker imageからcontainer起動]
>sudo docker run centos echo "this is centos"
docker: Error response from daemon: rpc error: code = 2 desc = "oci runtime error: exec format error".
...起動できなかった。。。

>sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9bcac91315bb        centos              "echo 'this is centos"   13 seconds ago      Created                                 hopeful_williams
...Created状態にはなっている。。。

>sudo docker start 9bcac91315bb
Error response from daemon: rpc error: code = 2 desc = "oci runtime error: exec format error"
Error: failed to start containers: 9bcac91315bb

>sudo docker stop 9bcac91315bb
9bcac91315bb

>sudo docker logs 9bcac91315bb


>sudo docker kill 9bcac91315bb
Error response from daemon: Cannot kill container 9bcac91315bb: Container 9bcac91315bbfe7842f9487abbfb07077359f708dacc056a104f9b712978d05e is not running

>sudo docker rm 9bcac91315bb
9bcac91315bb

仕方がないので、自分でbase imageを作ることにした。

[debootstrapでdebian環境を作成]
>ls
debian_wheezy/
>sudo chroot ./debian_wheezy /bin/bash
root# ls
bin  boot  dev	etc  home  lib	media  mnt  opt  proc  root  run  sbin	selinux  srv  sys  tmp	usr  var
root# exit

>cd debian_wheezy/
>sudo tar -c . | sudo docker import - debian-wheezy
sha256:fd93516c412f56393cbf7c14c9fcd3383f2dad9afbb919ab010225b09791e755

>sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian-wheezy       latest              fd93516c412f        25 seconds ago      223.5 MB
centos              latest              980e0e4c79ec        6 weeks ago         196.8 MB

debian-wheezyで起動をこころみる。

>sudo docker run debian-wheezy echo "this is debian"
this is debian

来ましたね。bugはupdateすれば直るようですが。ここではdebianで継続。

>sudo docker run -i -t debian-wheezy /bin/bash
root@c7b580639a09:/# ls
bin  boot  dev	etc  home  lib	media  mnt  opt  proc  root  run  sbin	selinux  srv  sys  tmp	usr  var

ちゃんと動いてattachできている。ここまで。
```

###Docker Hub
```
>sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian-wheezy       latest              fd93516c412f        11 minutes ago      223.5 MB
centos              latest              980e0e4c79ec        6 weeks ago         196.8 MB
>sudo docker tag fd93516c412f tomoyafujita/debian:latest
>sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
debian-wheezy         latest              fd93516c412f        17 minutes ago      223.5 MB
tomoyafujita/debian   latest              fd93516c412f        17 minutes ago      223.5 MB
centos                latest              980e0e4c79ec        6 weeks ago         196.8 MB
>sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
>sudo docker push tomoyafujita/debian:latest
The push refers to a repository [docker.io/tomoyafujita/debian]

```

###Dockerfile
imageからcontainerを作成する過程をMakefileのように記載可能。
http://www.atmarkit.co.jp/ait/articles/1407/08/news031.html

###bug
[引っかかったバグ](https://github.com/docker/docker/issues/22226)

索引:
[docker入門](http://qiita.com/naoki85/items/9fde4c5fd5aa5eb61eff#01-docker%E3%81%A8%E3%81%AF%E4%BD%95%E3%81%8B)
[さわって理解するDocker](https://www.ogis-ri.co.jp/otc/hiroba/technical/docker/part1.html)