読者です 読者をやめる 読者になる 読者になる

こしごぇ(B)

旧:http://d.hatena.ne.jp/koshigoeb/

Docker 入門(1)

写経。

準備

$ brew install docker boot2docker
$ boot2docker init
$ boot2docker up
$ export DOCKER_HOST=tcp://192.168.59.103:2375
$ boot2docker ssh
docker@boot2docker:~$ docker pull ubuntu
docker@boot2docker:~$ docker run ubuntu /bin/echo hello world

基本操作

インタラクティブ実行

docker@boot2docker:~$ docker run -i -t -h ubuntu ubuntu /bin/bash
root@ubuntu:/# exit

バックグランドで実行

docker@boot2docker:~$ docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 10; done"
cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
cbfbe8f4de11        ubuntu:14.04        /bin/sh -c 'while tr   41 seconds ago      Up 40 seconds                           silly_turing
docker@boot2docker:~$ docker logs -f cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world

コンテナの停止

docker@boot2docker:~$ docker stop cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567
cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

コンテナの再実行

docker@boot2docker:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                           PORTS               NAMES
cbfbe8f4de11        ubuntu:14.04        /bin/sh -c 'while tr   4 minutes ago       Exited (-1) About a minute ago                       silly_turing
198ac0c5f14f        ubuntu:14.04        /bin/bash              3 hours ago         Exited (0) 3 hours ago                               desperate_darwin
0aa4a140fb2c        ubuntu:14.04        /bin/echo hello worl   3 hours ago         Exited (0) 3 hours ago                               romantic_jones
docker@boot2docker:~$ docker restart cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
cbfbe8f4de11        ubuntu:14.04        /bin/sh -c 'while tr   4 minutes ago       Up 26 seconds                           silly_turing

コンテナへのアタッチ

docker@boot2docker:~$ docker attach cbfbe8f4de11
Hello world
Hello world
Hello world
Hello world
^C
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

コンテナの強制終了

※ stop ではタイムアウトするまでコンテナの終了を待つが、kill は即時にコンテナを終了させる。

docker@boot2docker:~$ docker restart cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
cbfbe8f4de11        ubuntu:14.04        /bin/sh -c 'while tr   7 minutes ago       Up 4 seconds                            silly_turing
docker@boot2docker:~$ docker kill cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

コンテナ内プロセスの監視

docker@boot2docker:~$ docker restart cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker top cbfbe8f4de11
PID                 USER                COMMAND
998                 root                /bin/sh -c while true; do echo Hello world; sleep 10; done
1018                root                sleep 10

コンテナの状態の確認

docker@boot2docker:~$ docker inspect cbfbe8f4de11
[{
    "Args": [
        "-c",
        "while true; do echo Hello world; sleep 10; done"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/sh",
            "-c",
            "while true; do echo Hello world; sleep 10; done"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": null,
        "Hostname": "cbfbe8f4de11",
        "Image": "ubuntu",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Created": "2014-08-18T09:43:46.165018471Z",
    "Driver": "aufs",
    "ExecDriver": "native-0.2",
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "Dns": null,
        "DnsSearch": null,
        "Links": null,
        "LxcConf": [],
        "NetworkMode": "bridge",
        "PortBindings": {},
        "Privileged": false,
        "PublishAllPorts": false,
        "VolumesFrom": null
    },
    "HostnamePath": "/mnt/sda1/var/lib/docker/containers/cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567/hostname",
    "HostsPath": "/mnt/sda1/var/lib/docker/containers/cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567/hosts",
    "Id": "cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567",
    "Image": "c4ff7513909dedf4ddf3a450aea68cd817c42e698ebccf54755973576525c416",
    "MountLabel": "",
    "Name": "/silly_turing",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "IPAddress": "172.17.0.5",
        "IPPrefixLen": 16,
        "PortMapping": null,
        "Ports": {}
    },
    "Path": "/bin/sh",
    "ProcessLabel": "",
    "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/cbfbe8f4de110601bd884fb7fa3e1c252d4abcc4b29e2f996b2c642c0cc1b567/resolv.conf",
    "State": {
        "ExitCode": 0,
        "FinishedAt": "2014-08-18T09:50:53.222406799Z",
        "Paused": false,
        "Pid": 998,
        "Running": true,
        "StartedAt": "2014-08-18T09:52:09.613630607Z"
    },
    "Volumes": {},
    "VolumesRW": {}
}
]
docker@boot2docker:~$ docker inspect -f "{{.State}}" cbfbe8f4de11
map[ExitCode:0 FinishedAt:2014-08-18T09:50:53.222406799Z Paused:false Pid:998 Running:true StartedAt:2014-08-18T09:52:09.613630607Z]

コンテナのコミット

docker@boot2docker:~$ docker commit cbfbe8f4de11
d7a471af5180a8185859e0684058b73cbd004e53cfb858b5859e5c5d0e731246
docker@boot2docker:~$ docker images | grep d7a471af5180
<none>              <none>              d7a471af5180        54 seconds ago      225.4 MB

コンテナの削除、イメージの削除

docker@boot2docker:~$ docker rm cbfbe8f4de11
Error response from daemon: Impossible to remove a running container, please stop it first or use -f
2014/08/18 09:57:16 Error: failed to remove one or more containers
docker@boot2docker:~$ docker kill cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker rm cbfbe8f4de11
cbfbe8f4de11
docker@boot2docker:~$ docker rmi d7a471af5180
Deleted: d7a471af5180a8185859e0684058b73cbd004e53cfb858b5859e5c5d0e731246

コンテナのイメージを共有する

Usage: docker push NAME[:TAG]

Dockerfile

$ docker build .
  • FROM
  • RUN
  • CMD
  • EXPOSE
  • ENV
  • ADD
  • ENTRYPOINT
  • VOLUME
  • USER
  • WORKDIR
  • ONBUILD