Networking examples
Show networks
~> docker network ls
NETWORK ID NAME DRIVER SCOPE
ba3e2a49565d bridge bridge local
7e049ea0340c host host local
8375866ba0bb none null local
--network bridge
- Default Docker virtual network, which is NAT'ed behind the Host IP.--network host
- It gains performance by skipping virtual networks but sacrifices security of container model
Create network
~> docker network create my_app_net
4ce9337a1d89b2cffb7df08c13cf6ef6089a38dcb31f9b88c603a0d98b0992a8
~> docker network ls
NETWORK ID NAME DRIVER SCOPE
ba3e2a49565d bridge bridge local
7e049ea0340c host host local
4ce9337a1d89 my_app_net bridge local
8375866ba0bb none null local
Run container on a custom network
~> docker container run -d --name new_nginx --network my_app_net nginx
54456526a198b23339280baceaacc904492a2e826004b159ac9880bbce47822b
Inspect network config
docker network inspect my_app_net
[
{
"Name": "my_app_net",
"Id": "4ce9337a1d89b2cffb7df08c13cf6ef6089a38dcb31f9b88c603a0d98b0992a8",
"Created": "2017-09-30T12:34:22.7563603Z",
"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,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"54456526a198b23339280baceaacc904492a2e826004b159ac9880bbce47822b": {
"Name": "new_nginx",
"EndpointID": "47d02ebd6375e6d5bcb84281a1c0c03e39a6d52c5842846fb79115efd922c096",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Attach network to a running container
Check information, grab IDs
~> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54456526a198 nginx "nginx -g 'daemon ..." 5 minutes ago Up 5 minutes 80/tcp new_nginx
263eae2f885b nginx "nginx -g 'daemon ..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp webhost
~> docker network ls
NETWORK ID NAME DRIVER SCOPE
ba3e2a49565d bridge bridge local
7e049ea0340c host host local
4ce9337a1d89 my_app_net bridge local
8375866ba0bb none null local
And now attach
docker network connect 4ce9337a1d89 263eae2f885b
docker network connect
dynamically creates a NIC in a container on an existing virtual networkdocker network disconnect
dynamically removes a NIC from a container on a specific virtual network
~> docker container inspect -f '{{range $key, $_ := .NetworkSettings.Networks}}{{println $key}}{{end}}' webhost
bridge
my_app_net
Try to ping between containers inside custom virtual network
- I have 2 custom nginx:alpine versions connected to custom network
- ping command was removed from default nginx image, thus I had to use nginx:alpine
~> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49c23fcf44c0 nginx:alpine "nginx -g 'daemon ..." 29 minutes ago Up 29 minutes 80/tcp new_nginx
0edfeec327ad nginx:alpine "nginx -g 'daemon ..." 29 minutes ago Up 29 minutes 80/tcp my_nginx
I can easily ping from one to another
~> docker container exec -it my_nginx ping new_nginx
PING new_nginx (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.082 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.047 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.045 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.046 ms
64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.045 ms
^C
--- new_nginx ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.045/0.053/0.082 ms