CodeGym /Courses /Docker SELF /Creating and Managing Networks

Creating and Managing Networks

Docker SELF
Level 17 , Lesson 1
Available

2.1 Basic docker network Commands

Networks in Docker allow containers to interact with each other and with external networks. The docker network commands provide flexible options for creating, configuring, and managing networks, ensuring isolation and control over container network interactions. In this lecture, we'll take a closer look at docker network commands and how to use them to manage networks.

Key docker network commands:

  1. docker network create
  2. docker network ls
  3. docker network inspect
  4. docker network connect
  5. docker network disconnect
  6. docker network rm

Creating a Network: docker network create Command

The docker network create command is used to create new networks. You can create networks with different drivers, such as bridge, host, overlay, and macvlan.

Example of creating a network with the bridge driver

Terminal


docker network create --driver bridge my_bridge_network 

Example of creating a network with the overlay driver

Terminal


docker network create --driver overlay my_overlay_network 

Example of creating a network with the macvlan driver

Terminal

docker network create --driver macvlan \
  --subnet=192.168.1.0/24   \
  --gateway=192.168.1.1 \
  -o parent=eth0 my_macvlan_network

2.2 Network Commands

1. Viewing available networks: command docker network ls

The docker network ls command displays a list of all networks created in Docker.

Terminal


docker network ls 

The output of the command will look like this:

Terminal


NETWORK ID          NAME                DRIVER              SCOPE
0e7e2d58fe94        bridge              bridge              local
9c84fdfc69ee        host                host                local
71cfb6a79d9e        none                null                local

2. Viewing network information: command docker network inspect

The docker network inspect command allows you to get detailed information about a specific network, including connected containers, subnet and gateway settings.

Example usage:

Terminal


docker network inspect my_bridge_network 

The output of the command will contain network information in JSON format:

JSON

[
    {
        "Name": "my_bridge_network",
        "Id": "0e7e2d58fe94",
        "Created": "2021-01-01T00:00:00.000000000Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {
            "container_id": {
                "Name": "my_container",
                "EndpointID": "6c52f8c75c1e",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

3. Connecting a container to a network: command docker network connect

The docker network connect command is used to connect an already running container to an existing network.

Example usage:

Terminal


docker network connect my_bridge_network my_container 

4. Disconnecting a container from a network: command docker network disconnect

The docker network disconnect command is used to disconnect a container from a network.

Example usage:

Terminal


docker network disconnect my_bridge_network my_container 

5. Removing a network: command docker network rm

The docker network rm command is used to remove a network. Note that a network can only be removed after all connected containers are disconnected.

Example usage:

Terminal


docker network rm my_bridge_network 

2.3 Examples of docker network Commands

Examples of using docker network commands

Example 1: Creating and Using a bridge Network

Creating a network:

Terminal

docker network create --driver bridge my_bridge_network 

Starting containers and connecting them to the network:

Terminal

docker run -d --name container1 --network my_bridge_network nginx

docker run -d --name container2 --network my_bridge_network redis 

Checking the network:

Terminal

docker network inspect my_bridge_network 

Output of the docker network inspect command will include detailed information about the network in JSON format. To make the output more readable, you can use the jq utility:

Terminal

docker network inspect my_bridge_network | jq

This is convenient for filtering and viewing structured network data, such as connected containers, their IP addresses, and subnet settings.

Example 2: Connecting and Disconnecting Containers

Starting a container:

Terminal

docker run -d --name my_container nginx 

Connecting a container to a network:

Terminal

docker network connect my_bridge_network my_container 

Disconnecting a container from a network:

Terminal

docker network disconnect my_bridge_network my_container 
3
Task
Docker SELF, level 17, lesson 1
Locked
Creating a bridge network
Creating a bridge network
3
Task
Docker SELF, level 17, lesson 1
Locked
Connecting to an existing network
Connecting to an existing network
3
Task
Docker SELF, level 17, lesson 1
Locked
Disconnecting the container from a network
Disconnecting the container from a network
3
Task
Docker SELF, level 17, lesson 1
Locked
Deleting a Network
Deleting a Network
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION