Docker Networks:
docker network ls
docker network inspect
docker network create --driver
docker network connect
docker network disconnect
Note:
(bridge) Defalt Docker virtual network is NATed with Host IP.
(host) It gains performance by skipping virtual networks and directly connectes the container to the Host interface, but sacrifies security of container model.
(none) Kind of equivalent of having an interface on your computer that's not attached to anything, but we can create our own.
PS C:\Users\MOHI\code> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a59253871ff nginx "nginx -g 'daemon of…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp nginx_web
8c8313534019 mysql "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_db
// Checking the configured port for the container
PS C:\Users\MOHI\code> docker container port nginx_web
80/tcp -> 0.0.0.0:80
// Checking the IP address of the container:
PS C:\Users\MOHI\code> docker container inspect --format '{{ .NetworkSettings.IPAddress }}' nginx_web
172.17.0.3
PS C:\Users\MOHI\code> docker network ls
NETWORK ID NAME DRIVER SCOPE
981b5c0569a0 bridge bridge local
a77057e70878 host host local
cdcd8436ab53 none null local
PS C:\Users\MOHI> docker network inspect bridge
[
{
"Name": "bridge",
"Id": "981b5c0569a0035a7c05d3e7094060c9f631a195389fd8eee81e8bb3da4898a1",
"Created": "2019-11-03T17:53:31.7228006Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466": {
"Name": "nginx_web",
"EndpointID": "21663238eb94c4af42e1bea175eed205b433ffab39b6067be6861a433fc629b1",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"8c83135340196a58031c7cca0012c83938b72d31851a10d920863e82e588caf2": {
"Name": "mysql_db",
"EndpointID": "e4fd935517985d081fe9c5bd7af39bfeb5ed712009286e8d8b5e8d1967f8f2a8",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"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": {}
}
]
// Creating a new Docker network:
PS C:\Users\MOHI> docker network create my_app_net
60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418
PS C:\Users\MOHI> docker network ls
NETWORK ID NAME DRIVER SCOPE
981b5c0569a0 bridge bridge local
a77057e70878 host host local
60095a7d865d my_app_net bridge local
cdcd8436ab53 none null local
Note: The new Docker network (my_app_net) is using the "bridge" driver
PS C:\Users\MOHI> docker network create --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by
Network driver (default map[])
--config-from string The network from which copying the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable IPv6 networking
--label list Set metadata on a network
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a
network segment
PS C:\Users\MOHI> docker network inspect my_app_net
[
{
"Name": "my_app_net",
"Id": "60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418",
"Created": "2019-11-07T15:38:45.9824155Z",
"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": {},
"Options": {},
"Labels": {}
}
]
// Creating a new container with virtual network option:
PS C:\Users\MOHI> docker container run -d --name new_nginx --network my_app_net nginx
872f240ce4928764c69bee37a69677c4e06fc12e0110f0c68c1d7cda705b6b11
PS C:\Users\MOHI> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
872f240ce492 nginx "nginx -g 'daemon of…" 40 seconds ago Up 37 seconds 80/tcp new_nginx
2a59253871ff nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:80->80/tcp nginx_web
8c8313534019 mysql "docker-entrypoint.s…" 4 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_db
PS C:\Users\MOHI> docker network inspect my_app_net
[
{
"Name": "my_app_net",
"Id": "60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418",
"Created": "2019-11-07T15:38:45.9824155Z",
"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": {
"872f240ce4928764c69bee37a69677c4e06fc12e0110f0c68c1d7cda705b6b11": {
"Name": "new_nginx",
"EndpointID": "8b78c2ff891147b1772e5d3b6d2e2de4cb14b831a75c32a018d9bb6d123a21b5",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
PS C:\Users\MOHI> docker network --help
Usage: docker network COMMAND
Manage networks
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
//Docker network connect:
Note: Dynamically creates a NIC in the container on an existing virtual network.
PS C:\Users\MOHI> docker network ls
NETWORK ID NAME DRIVER SCOPE
981b5c0569a0 bridge bridge local
a77057e70878 host host local
60095a7d865d my_app_net bridge local
cdcd8436ab53 none null local
docker network connect {newly created nw id} {container id)
docker network connect 60095a7d865d 2a59253871ff
PS C:\Users\MOHI> docker container inspect nginx_web
[
{
"Id": "2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466",
"Created": "2019-11-07T12:11:17.0763598Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4415,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-11-07T12:11:21.8310527Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:540a289bab6cb1bf880086a9b803cf0c4cefe38cbb5cdefa199b69614525199f",
"ResolvConfPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/hostname",
"HostsPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/hosts",
"LogPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466-json.log",
"Name": "/nginx_web",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
50,
120
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c-init/diff:/var/lib/docker/overlay2/08b90638737e516cb36bc344eed1af3b3031e37b47364996b51d8c3014380d9a/diff:/var/lib/docker/overlay2/64cfd2d2aebaa6155623792ba07ab098a755c9be60783bc93acd429352a01991/diff:/var/lib/docker/overlay2/6d7637e12181a5a3d9dd5250a2d6e04dec26d0e361bf118d6d494557d22095d3/diff",
"MergedDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/merged",
"UpperDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/diff",
"WorkDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "2a59253871ff",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.5",
"NJS_VERSION=0.3.6",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7ec0248663c9475baca243426089563c22a0b1039e778e69df0d667c38cdfc66",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/7ec0248663c9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "21663238eb94c4af42e1bea175eed205b433ffab39b6067be6861a433fc629b1",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "981b5c0569a0035a7c05d3e7094060c9f631a195389fd8eee81e8bb3da4898a1",
"EndpointID": "21663238eb94c4af42e1bea175eed205b433ffab39b6067be6861a433fc629b1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
},
"my_app_net": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"2a59253871ff"
],
"NetworkID": "60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418",
"EndpointID": "8ee15c7ef28cb4fb0265bcf8d844290bca04ec414008d2999b08a3ec2ef9eb10",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03",
"DriverOpts": {}
}
}
}
}
]
Note: Now we can see that this nginx_web container has two different networks (bridge and my_app_net) and different IPs (172.17.0.3 and 172.18.0.3).
Docker network disconnect:
Dynamically remove a NIC from a container on a specific virtual network:
PS C:\Users\MOHI> docker network disconnect 60095a7d865d 2a59253871ff
PS C:\Users\MOHI> docker container inspect nginx_web
[
{
"Id": "2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466",
"Created": "2019-11-07T12:11:17.0763598Z",
"Path": "nginx",
"Args": [
"-g",
"daemon off;"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4415,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-11-07T12:11:21.8310527Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:540a289bab6cb1bf880086a9b803cf0c4cefe38cbb5cdefa199b69614525199f",
"ResolvConfPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/hostname",
"HostsPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/hosts",
"LogPath": "/var/lib/docker/containers/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466/2a59253871ffed45115fc5d6778c7d5d24971dc3020c9f0a8bee551200b93466-json.log",
"Name": "/nginx_web",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
50,
120
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c-init/diff:/var/lib/docker/overlay2/08b90638737e516cb36bc344eed1af3b3031e37b47364996b51d8c3014380d9a/diff:/var/lib/docker/overlay2/64cfd2d2aebaa6155623792ba07ab098a755c9be60783bc93acd429352a01991/diff:/var/lib/docker/overlay2/6d7637e12181a5a3d9dd5250a2d6e04dec26d0e361bf118d6d494557d22095d3/diff",
"MergedDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/merged",
"UpperDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/diff",
"WorkDir": "/var/lib/docker/overlay2/21a32567dcfff9817453d9efdb9dd3b7fc8c0eba1cd62560ef975e08a85e3a9c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "2a59253871ff",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.17.5",
"NJS_VERSION=0.3.6",
"PKG_RELEASE=1~buster"
],
"Cmd": [
"nginx",
"-g",
"daemon off;"
],
"Image": "nginx",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
},
"StopSignal": "SIGTERM"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7ec0248663c9475baca243426089563c22a0b1039e778e69df0d667c38cdfc66",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
]
},
"SandboxKey": "/var/run/docker/netns/7ec0248663c9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "21663238eb94c4af42e1bea175eed205b433ffab39b6067be6861a433fc629b1",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "981b5c0569a0035a7c05d3e7094060c9f631a195389fd8eee81e8bb3da4898a1",
"EndpointID": "21663238eb94c4af42e1bea175eed205b433ffab39b6067be6861a433fc629b1",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
}
]
Note: Now we can see that 172.18.0.3 (my_app_net) has been removed for nginx_web
Docker networks DNS:
Note:
Docker daemon has a built-in DNS server that containers use by default.
Containers shouldn't rely on IP's for inter-communication.
PS C:\Users\MOHI> docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
872f240ce492 nginx "nginx -g 'daemon of…" 36 minutes ago Up 36 minutes 80/tcp new_nginx
2a59253871ff nginx "nginx -g 'daemon of…" 4 hours ago Up 4 hours 0.0.0.0:80->80/tcp nginx_web
8c8313534019 mysql "docker-entrypoint.s…" 4 hours ago Up 4 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_db
PS C:\Users\MOHI> docker network inspect my_app_net
[
{
"Name": "my_app_net",
"Id": "60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418",
"Created": "2019-11-07T15:38:45.9824155Z",
"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": {
"872f240ce4928764c69bee37a69677c4e06fc12e0110f0c68c1d7cda705b6b11": {
"Name": "new_nginx",
"EndpointID": "8b78c2ff891147b1772e5d3b6d2e2de4cb14b831a75c32a018d9bb6d123a21b5",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Note: DNS default names, Docker defaults the hostname to the container's name, but you can also set aliases
// Creating new container "my_nginx_new" (We are using alpine linux based nginx here since we need a "ping" command to work:
PS C:\Users\MOHI> docker container run -d --name my_nginx_alpine --network my_app_net nginx:alpine
Unable to find image 'nginx:alpine' locally
alpine: Pulling from library/nginx
89d9c30c1d48: Already exists
110ad692b782: Pull complete
Digest: sha256:085e84650dbe56f27ca3ed00063a12d5b486e40c3d16d83c4e6c2aad1e4045ab
Status: Downloaded newer image for nginx:alpine
64f0dba4ea987ae614310cef046f2d1032ef86d089a4f9d66a777b77b09cd00b
PS C:\Users\MOHI> docker network inspect my_app_net
[
{
"Name": "my_app_net",
"Id": "60095a7d865da0b55404ed123c8b0ed44cc99fb1c9af6b91ce40bf82d393f418",
"Created": "2019-11-07T15:38:45.9824155Z",
"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": {
"3cac4f533443bea5f1919acf3b0297e00b947d679bc2429f5ef596be61725245": {
"Name": "my_nginx",
"EndpointID": "eb8b0dacc10542cf1bc305df8cfb1eddf7964ce48f5f4f3a23db01e3d247b26b",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"64f0dba4ea987ae614310cef046f2d1032ef86d089a4f9d66a777b77b09cd00b": {
"Name": "my_nginx_alpine",
"EndpointID": "302b5679b9431bb4ed0faed713c1cc766be4e446b0520d552bf4e9e08f8c9747",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"872f240ce4928764c69bee37a69677c4e06fc12e0110f0c68c1d7cda705b6b11": {
"Name": "new_nginx",
"EndpointID": "8b78c2ff891147b1772e5d3b6d2e2de4cb14b831a75c32a018d9bb6d123a21b5",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Note: Now we can see new container (my_nginx_alpine). We can ping a same networks container by using it name.
PS C:\Users\MOHI> docker container exec -it my_nginx_alpine ping new_nginx
PING new_nginx (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.103 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.188 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.192 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.196 ms
64 bytes from 172.18.0.2: seq=4 ttl=64 time=0.190 ms
64 bytes from 172.18.0.2: seq=5 ttl=64 time=0.187 ms
^C
--- new_nginx ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max = 0.103/0.176/0.196 ms
Now we can see that DNS resolution is works fine.