Welcome to Day 10 of 21 Days of Docker. On Day 9, I discussed the basics of Docker Networking and we use the default bridge network.
- Let’s take one step further and now move to User-defined bridge networks are superior to the default
bridge
network.
- To create a user-defined bridge
$ docker network create my-network
42ed6f1adc7a9b4ec687e99a1cbbdde8a1a3d1f86bef5e5376e121b29c8f760b
- To verify the bridge network
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
45decc7a84de bridge bridge local
18ab553b68ea host host local
42ed6f1adc7a my-network bridge local
b49ee4b5f50f none null local
- To get detailed info about the network we just created
$ docker network inspect my-network
[
{
"Name": "my-network",
"Id": "42ed6f1adc7a9b4ec687e99a1cbbdde8a1a3d1f86bef5e5376e121b29c8f760b",
"Created": "2019-10-15T22:24:36.801033922Z",
"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": {}
}
]
Connect a container to a user-defined bridge
- When we create a new container, you can specify one or more
--network
flags
$ docker container run --name mycustomnetcontainer --network my-network -dt centos bash
a7b364c933b636bd1e0b454fcc7d1ab810dc60105716e1a0e1984a50486a6c20
- If we run the network inspect again
$ docker network inspect my-network
[
{
"Name": "my-network",
"Id": "42ed6f1adc7a9b4ec687e99a1cbbdde8a1a3d1f86bef5e5376e121b29c8f760b",
"Created": "2019-10-15T22:24:36.801033922Z",
"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": {
"a7b364c933b636bd1e0b454fcc7d1ab810dc60105716e1a0e1984a50486a6c20": {
"Name": "mycustomnetcontainer", <--------------------------------
"EndpointID": "5e0fe7027848591ce9c2305c2428b3a67f45f143976811f4419c2600f04fe63e",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
- If we want we can connect any existing network container on the fly to this network
$ docker network connect my-network mycentosnewcont
- If we run docker inspect again, you will see one more container
$ docker network inspect my-network
[
{
"Name": "my-network",
"Id": "42ed6f1adc7a9b4ec687e99a1cbbdde8a1a3d1f86bef5e5376e121b29c8f760b",
"Created": "2019-10-15T22:24:36.801033922Z",
"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": {
"402332f2de72b214077b6312bef8b891a6564fae0f2e386032fb0abff736b29c": {
"Name": "mycentosnewcont",<------
"EndpointID": "312d1d7fa00c5e638a5b7b23ee0734b01571df3601be64711288ebfad1b3d23d",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"a7b364c933b636bd1e0b454fcc7d1ab810dc60105716e1a0e1984a50486a6c20": {
"Name": "mycustomnetcontainer",
"EndpointID": "5e0fe7027848591ce9c2305c2428b3a67f45f143976811f4419c2600f04fe63e",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
Pages: 1 2