CLOVER🍀

That was when it all began.

「docker run」で遊ぶ

こちらの本を見ながら、Dockerイメージの取得、それから「docker run」を試していってみます。

Dockerエキスパート養成読本[活用の基礎と実践ノウハウ満載!] (Software Design plus)

Dockerエキスパート養成読本[活用の基礎と実践ノウハウ満載!] (Software Design plus)

Hello World」を掘り下げる

と言いつつ、まずはチュートリアルも含めてもう1度。

Install Docker
http://docs.docker.com/linux/step_one/

「docker run」で「hello-world」を実行。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from hello-world
a8219747be10: Pull complete 
91c95931e552: Already exists 
hello-world:latest: 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:aa03e5d0d5553b4c3473e89c8619cf79df368babd18681cf5daeb82aab55838d
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.
    (Assuming it was not already locally available.)
 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

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

この時、「docker ps -a」でrunning以外のコンテナも見れると。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
37946448c85a        hello-world         "/hello"            33 seconds ago      Exited (0) 32 seconds ago                       condescending_leakey

「docker images」で、Dockerイメージの一覧も見れると。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world         latest              91c95931e552        10 weeks ago        910 B

不要になったDockerコンテナは、「docker rm」で削除。

$ docker rm 37946448c85a
37946448c85a

いなくなりましたよ。

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

Dockerイメージは、「docker rmi」で消せると。

$ docker rmi hello-world
Untagged: hello-world:latest
Deleted: 91c95931e552b11604fea91c2f537284149ec32fff0f700a4769cfd31d7696ae
Deleted: a8219747be10611d65b7c693f48e7222c0bf54b5df8467d3f99003611afa1fd8

こちらもいなくなりました。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE

また、「--rm」を付けることで、終了と同時にDockerコンテナを削除することもできるのだとか。

$ docker run --rm hello-world

この場合、「ps -a」でもコンテナは残りません。

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

もちろん、Dockerイメージは残ったままですが…。

ubuntu:latest」を試す

Ubuntu LinuxのDockerイメージで遊びます。まずは、Dockerイメージの取得。

$ docker pull ubuntu:latest
latest: Pulling from ubuntu
428b411c28f0: Pull complete 
435050075b3f: Pull complete 
9fd3c8c9af32: Pull complete 
6d4946999d4f: Already exists 
ubuntu:latest: 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:45e42b43f2ff4850dcf52960ee89c21cda79ec657302d36faaaa07d880215dd9
Status: Downloaded newer image for ubuntu:latest

bashを起動して、そのままDockerコンテナを破棄。

$ docker run -it --rm ubuntu:latest /bin/bash
root@98ebb0974c89:/# ls -l
total 64
drwxr-xr-x   2 root root 4096 Jun 12 11:35 bin
drwxr-xr-x   2 root root 4096 Apr 10  2014 boot
drwxr-xr-x   5 root root  380 Jun 28 09:51 dev
drwxr-xr-x  64 root root 4096 Jun 28 09:51 etc
drwxr-xr-x   2 root root 4096 Apr 10  2014 home
drwxr-xr-x  12 root root 4096 Jun 12 11:35 lib
drwxr-xr-x   2 root root 4096 Jun 12 11:35 lib64
drwxr-xr-x   2 root root 4096 Jun 12 11:34 media
drwxr-xr-x   2 root root 4096 Apr 10  2014 mnt
drwxr-xr-x   2 root root 4096 Jun 12 11:34 opt
dr-xr-xr-x 531 root root    0 Jun 28 09:51 proc
drwx------   2 root root 4096 Jun 12 11:35 root
drwxr-xr-x   7 root root 4096 Jun 12 11:35 run
drwxr-xr-x   2 root root 4096 Jun 12 15:32 sbin
drwxr-xr-x   2 root root 4096 Jun 12 11:34 srv
dr-xr-xr-x  13 root root    0 Jun 28 09:15 sys
drwxrwxrwt   2 root root 4096 Jun 12 11:35 tmp
drwxr-xr-x  11 root root 4096 Jun 12 15:32 usr
drwxr-xr-x  12 root root 4096 Jun 12 15:32 var
root@98ebb0974c89:/# exit
exit

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

「nginx:latest」を試す

続いて、nginx。こちらは、とりあえず「--rm」なしで実行してみます。

$ docker run -d nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from nginx
64e5325c0d9d: Pull complete 
bf84c1d84a8f: Pull complete 
aaa04ccb1cff: Pull complete 
c25a494fd450: Pull complete 
2b4392756878: Pull complete 
a58c6ba7d446: Pull complete 
6391a6648407: Pull complete 
0b6365661275: Pull complete 
5c1f0ec6cd7b: Pull complete 
36f0b0306c21: Pull complete 
107c338c1d31: Pull complete 
319d2015d149: Already exists 
nginx:latest: 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:0952acc92b56f936b5b5b93a6b7abb9205ec7ec64b920e113a6d9dae50f6f49d
Status: Downloaded newer image for nginx:latest
ae885f5bb704ffe6edb90025e22247f7501868323848cfd8d9d33a20515fab88

動いていることを確認して

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
ae885f5bb704        nginx:latest        "nginx -g 'daemon of   13 seconds ago      Up 13 seconds       80/tcp, 443/tcp     sleepy_mccarthy

試しに、コマンド実行。

$ docker exec -it ae885f5bb704 ps -ef | grep nginx
root          1      0  0 10:13 ?        00:00:00 nginx: master process nginx -g
nginx         6      1  0 10:13 ?        00:00:00 nginx: worker process

nginxがいることを確認。

Dockerコンテナ停止。

$ docker stop ae885f5bb704

Dockerコンテナ削除。

$ docker rm ae885f5bb704

で、起動してみたこのnginxですが、ホスト側からは見れないのでホストの80ポートで見れるようにして実行。

$ docker run -p 80:80 -d nginx:latest
5f91ff49e3ccb8c3ab4230781985159ddd03c710c0cf02c520f85d913dfb9d75

確認。

$ curl http://localhost/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

OKですね。

とりあえず、こんなところで。