Create and manage docker networks

Docker network serves as the backbone of docker for communication between host, containers, and remote. Docker’s networking subsystem is pluggable, using drivers. Several drivers exist by default and provide core networking functionality.

Docker creates three networks
*bridge - An automatically generated network with a subnet and a gateway.
*host - Allows a container to attach to the host's network.
*none - A container-specific network stack that lacks a network interface.

Network driver available by default: You can list network drivers available by default using the docker network ls command.
# docker network ls
NETWORK ID NAME DRIVER SCOPE
bedb42c97ecb bridge bridge local
c9490299e3d1 host host local
425050dc2e74 none null local

If we want to view details on the host network, that command would be docker network inspect host. The output of that command would give you all the information about that network.
# docker network inspect host
[
{
"Name": "host",
"Id": "c9490299e3d14cb0cf409df55dc8542ea455e29554cd26d2411cef24e931da1f",
"Created": "2021-03-15T12:04:52.531932314+05:30",
"Scope": "local",
"Driver": "host",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},

Creating a new network.
create a bridge network and deploy a container on that network. We'll create a network called isolated. The creation of this network can be achieved with a single command:
# docker network create --driver bridge isolated
e11e03f504511ae3b929ea3ed30704c7e4636129fd2bedabe58a63f23fe4bb42

Run the inspect command docker network inspect isolated to see that our new network has been automatically given its own subnet and gateway.
# docker network inspect isolated
[
{
"Name": "isolated",
"Id": "e11e03f504511ae3b929ea3ed30704c7e4636129fd2bedabe58a63f23fe4bb42",
"Created": "2021-04-29T10:45:09.862887839+05:30",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]

create a network with a subnet of 192.168.1.0/24, a gateway of 192.168.1.10, and the name new_subnet.
# docker network create --driver=bridge --subnet=192.168.2.0/24 --gateway=192.168.2.10 new_subnet
Once created, inspect the network, with the command
#docker network inspect new_subnet

Attaching a container to a networking
Let's attach a container to our newly created network, To do this, run the below command.
#docker run --network=isolated -itd --name=docker-nginx nginx
we already pulled down the nginx image and launch a container, named docker-nginx, attached to the isolated network.

If we run the command docker network inspect isolated, we will see that the container has been attached. 





Recent Comments

No comments

Leave a Comment