Steps to Manage Network in Docker

Steps to Manage Network in Docker - By default, containers use a bridge-type network driver called a bridge. There are other network drivers, namely host, overlay, and macvlan.


Network drivers

  1. bridge: connect containers using the same bridge name
  2. host: remove network isolation between the container and Docker host, directly use host's network
  3. overlay: connects multiple Docker daemons together and lets the swarm service talk to both
  4. macvlan: allows it to assign MAC addresses to containers, making them exist as physical devices on the network
  5. none: disables all networks. Commonly used in conjunction with custom network drivers
  6. Network plugins: install and use third-party network plugins available on Docker Hub or from third-party suppliers


Bridge network

  • Displays available networks in Docker.

docker network ls

  • Default bridge network

Created two containers with container names node1 and node2, image Nginx:stable-alpine, and without defining network options.

docker run -d --name node1 nginx:stable-alpine
docker run -d --name node2 nginx:stable-alpine

  • Displays detailed container information.

docker inspect node1

Network information from the container is found in the Networks section. The container uses the network driver bridge. The first 12 characters (1652fc7934a6) NetworkID is the same as the NETWORK ID shown earlier in the result of the docker network ls command with NAME and DRIVER bridge.

"Networks": {

    "bridges": {

        "IPAMConfig": null,

        "Links": null,

        "Aliases": null,

        "NetworkID": "1758fc7934a653e3852e24f289219ea1255292133d625563bf5905947a314d98",

        "EndpointID": "ce81598649bf6500e4fb6799f76801918bc9e69a7535fb8241a64a6cb7418b14",

        "Gateway": "172.17.0.1",

        "IPAddress": "172.17.0.2",


The IP address is 172.17.0.2 and Gateway 172.17.0.1. The gateway uses the network interface and IP of Docker0 on the host.

ifconfig docker0

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

        inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255

        inet6 fe80::42:b0ff:fe4b:4ff9 prefixlen 64 scopeid 0x20<link>

        ether 02:42:b0:4b:4f:f9 txqueuelen 0 (Ethernet)

        RX packets 0 bytes 0 (0.0 B)

        RX errors 0 dropped 0 overruns 0 frame 0

        TX packets 5 bytes 526 (526.0 B)

        TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


  • Displays network information with the bridge name.

docker network inspect bridge


"Containers": {

    "aba5c5373c2dc5cf0c416d4c9a64334f060d05a84695dbcfbb3866bb7722d036": {

        "Name": "node1",

        "EndpointID": "ce80598649bf6500e4fb6799f76801908bc9e69a7535fb8241a64a6cb7418b04",

        "MacAddress": "02:42:ac:11:00:02",

        "IPv4Address": "172.17.0.2/16",

        "IPv6Address": ""

    },

    "f0667f59710ee4a62299f6943a38f0b40bbe09ee950b6e62d8b35fc6bdb71b2f": {

        "Name": "node2",

        "EndpointID": "d272a549bdb743c4206eb4ef17fc10981c9cbb3a51c38e2fad22afd55175f374",

        "MacAddress": "02:42:ac:11:00:03",

        "IPv4Address": "172.17.0.3/16",

        "IPv6Address": ""

    }

},

Containers that use a bridge network can be seen for information in the Containers section. There are two containers, namely node1 with IP address 172.17.0.2 and node2 with IP address 172.17.0.3.

  • Test ping from container node1 to node2.

docker exec node1 ping 172.17.0.3


Creating a Bridge Network

Containers using the default bridge network cannot automatically communicate using the hostname of the container, they can only use the IP address. For example, it cannot ping node2, it can only ping 172.17.0.3.

To be able to communicate using a hostname, define the IP-hosts in the /etc/hosts container file manually. Alternatively, the container uses a user-defined bridge network.

  • Create a bridge network with the name net-web.

docker network create net-web

  • Display networks.

docker network ls

NETWORK ID NAME DRIVER SCOPE

1652fc7934a6 bridge bridge local

547776771fe2 host local host

2aa8cf07ab2e net-web bridge local

a4d149a845ae none null local


Creating a Container with network options

At the time of creating a container can simultaneously connect it to the network.

  • Create a container and connect it to the network with the name net-web.

docker run -d --name node1 --network net-web nginx:stable-alpine

docker run -d --name node2 --network net-web nginx:stable-alpine


Connecting existing containers

Pre-existing containers can be connected to the network.

  • Connects node1 and node2 containers to the net-web network.

docker network connect net-web node1
docker network connect net-web node2

  • Removing the node1 container from the net-web network.

docker network disconnect net-web node1


Docker Network Command

  • Displays all networks.

docker network ls

  • Create networks.

docker network create name-network

  • Connect the container to the network.

docker network connect container-name-network

  • Removing the container from the network.

docker network disconnect container-name network-name

  • Displays detailed network information.

docker network inspect network-name

  • Delete networks.

docker network rm name-network

  • Delete all unused networks.

docker network prune


Next Post Previous Post
No Comment
Add Comment
comment url