Docker, Get started Learn by exampleやってみた
うん、面白い。習うより慣れろでちょっと触ってみた。Learn by example. Network containers.
参考:Network containers | Docker Documentation
Hello! Docker
$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b04784fba78d: Pull complete Digest: sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f 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. 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 Share images, automate workflows, and more with a free Docker ID: https://cloud.docker.com/ For more examples and ideas, visit: https://docs.docker.com/engine/userguide/
Dcoker上で、bashを起動してみる。かな?
$ sudo docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu bd97b43c27e3: Pull complete 6960dc1aba18: Pull complete 2b61829b0db5: Pull complete 1f88dc826b14: Pull complete 73b3859b1e43: Pull complete Digest: sha256:ea1d854d38be82f54d39efe2c67000bed1b03348bcc2f3dc094f260855dff368 Status: Downloaded newer image for ubuntu:latest
ネットワークでmy_bridgeを作る
$ sudo docker network create -d bridge my_bridge 192d0ca4764442d62f634dfda5afdf58d8412ed4743da2c80f1b36a907864c02
my_bridgeをチェック
$ docker network inspect my_bridge [ { "Name": "my_bridge", "Id": "192d0ca4764442d62f634dfda5afdf58d8412ed4743da2c80f1b36a907864c02", "Created": "2017-06-19T15:41:11.497066293+09:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Containers": {}, "Options": {}, "Labels": {} } ]
最初からあるbridgeを覗いてみる
$ docker network inspect bridge [ { "Name": "bridge", "Id": "87df18fc20cd4f07134043df4e6128c8976c805fd9e0975df7a8ed7a5e943062", "Created": "2017-06-19T14:55:57.194021424+09:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Containers": {}, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ]
db
$ docker inspect --format='{{json .NetworkSettings.Networks}}' db {"my_bridge":{"IPAMConfig":null,"Links":null,"Aliases":["6cd19d6243fd"],"NetworkID":"192d0ca4764442d62f634dfda5afdf58d8412ed4743da2c80f1b36a907864c02","EndpointID":"12d728caa2060e6239dd237cb0708ecd6464fdfc0d4f84de8292778c963d09af","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02"}}
app.pyを走らせる
$ sudo docker run -d --name web training/webapp python app.py Unable to find image 'training/webapp:latest' locally latest: Pulling from training/webapp e190868d63f8: Pull complete 909cd34c6fd7: Pull complete 0b9bfabab7c1: Pull complete a3ed95caeb02: Pull complete 10bbbc0fc0ff: Pull complete fca59b508e9f: Pull complete e7ae2541b15b: Pull complete 9dd97ef58ce9: Pull complete a4c1b0cb7af7: Pull complete Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d Status: Downloaded newer image for training/webapp:latest c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78
webをinspect
$ docker inspect --format='{{json .NetworkSettings.Networks}}' web {"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"87df18fc20cd4f07134043df4e6128c8976c805fd9e0975df7a8ed7a5e943062","EndpointID":"cbc41fe982497845ac8c19c5b31e01610d5f408b18ce9f40bf4c1e17c5948c14","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}
フォーマットを端折ってみた
$ docker inspect web [ { "Id": "c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78", "Created": "2017-06-19T08:29:52.027401227Z", "Path": "python", "Args": [ "app.py" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7947, "ExitCode": 0, "Error": "", "StartedAt": "2017-06-19T08:29:53.087901528Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:6fae60ef344644649a39240b94d73b8ba9c67f898ede85cf8e947a887b3e6557", "ResolvConfPath": "/var/lib/docker/containers/c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78/hostname", "HostsPath": "/var/lib/docker/containers/c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78/hosts", "LogPath": "/var/lib/docker/containers/c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78/c2acecacb7b73a123f1b84d62138c73b7e5d24866b51de5b0fc39efe8c844f78-json.log", "Name": "/web", "RestartCount": 0, "Driver": "aufs", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": null, "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": -1, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0 }, "GraphDriver": { "Name": "aufs", "Data": null }, "Mounts": [], "Config": { "Hostname": "c2acecacb7b7", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "5000/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "python", "app.py" ], "Image": "training/webapp", "Volumes": null, "WorkingDir": "/opt/webapp", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "744f84e01741f3a5e8c687e272b9a7859dd7f000422c72da7c3ba79696b4be52", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "5000/tcp": null }, "SandboxKey": "/var/run/docker/netns/744f84e01741", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "cbc41fe982497845ac8c19c5b31e01610d5f408b18ce9f40bf4c1e17c5948c14", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "87df18fc20cd4f07134043df4e6128c8976c805fd9e0975df7a8ed7a5e943062", "EndpointID": "cbc41fe982497845ac8c19c5b31e01610d5f408b18ce9f40bf4c1e17c5948c14", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02" } } } } ]
IPアドレスを表示
$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web 172.17.0.2
pingがロスしているのを確認
$ docker exec -it db bash root@6cd19d6243fd:/# ping 172.17.0.2 PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data. ^C --- 172.17.0.2 ping statistics --- 12 packets transmitted, 0 received, 100% packet loss, time 11087ms
ネットワークを見てみた
root@6cd19d6243fd:/# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:ac:12:00:02 inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1455 errors:0 dropped:0 overruns:0 frame:0 TX packets:21 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:172944 (172.9 KB) TX bytes:1866 (1.8 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:275 errors:0 dropped:0 overruns:0 frame:0 TX packets:275 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:24545 (24.5 KB) TX bytes:24545 (24.5 KB)
$ docker exec -it db bash root@6cd19d6243fd:/# root@6cd19d6243fd:/# ping web PING web (172.18.0.3) 56(84) bytes of data. 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=1 ttl=64 time=0.099 ms 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=2 ttl=64 time=0.079 ms 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=3 ttl=64 time=0.079 ms ^C --- web ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.079/0.085/0.099/0.014 ms root@6cd19d6243fd:/# root@6cd19d6243fd:/# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:ac:12:00:02 inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1495 errors:0 dropped:0 overruns:0 frame:0 TX packets:26 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:177126 (177.1 KB) TX bytes:2244 (2.2 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:283 errors:0 dropped:0 overruns:0 frame:0 TX packets:283 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:25226 (25.2 KB) TX bytes:25226 (25.2 KB) root@6cd19d6243fd:/# ping 172.17.0.2 PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data. ^C --- 172.17.0.2 ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 3022ms root@6cd19d6243fd:/#
my_bridgeとwebをコネクトする。一瞬。
$ docker network connect my_bridge web
pingすると通る。素晴らしい。
$ docker exec -it db bash root@6cd19d6243fd:/# root@6cd19d6243fd:/# ping web PING web (172.18.0.3) 56(84) bytes of data. 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=1 ttl=64 time=0.099 ms 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=2 ttl=64 time=0.079 ms 64 bytes from web.my_bridge (172.18.0.3): icmp_seq=3 ttl=64 time=0.079 ms ^C --- web ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.079/0.085/0.099/0.014 ms