How to use the command 'docker network' (with examples)

How to use the command 'docker network' (with examples)

The docker network command is used to create and manage networks in Docker. Networks allow containers to communicate with each other, either on the same Docker host or across different hosts. This command provides various operations for managing Docker networks, such as creating, listing, inspecting, connecting, and disconnecting networks.

Use case 1: List all available and configured networks on docker daemon

Code:

docker network ls

Motivation:

Listing all available and configured networks on the Docker daemon can provide an overview of the existing networks and their respective configurations. This information is useful for managing and troubleshooting network connectivity between containers.

Explanation:

The docker network ls command lists all the networks available and configured on the Docker daemon. It displays a table with information about each network, including the NETWORK ID, NAME, DRIVER, and SCOPE.

Example output:

NETWORK ID     NAME     DRIVER    SCOPE
abcdef123456   bridge   bridge    local
ghijkl789012   host     host      local
mnopqr345678   none     null      local

Use case 2: Create a user-defined network

Code:

docker network create --driver bridge mynetwork

Motivation:

Creating a user-defined network allows you to isolate containers and control their communication within a specific network. This is useful when you want to build complex multi-container applications or microservices architectures.

Explanation:

The docker network create command is used to create a user-defined network. In this example, we create a network named “mynetwork” using the “bridge” driver. The “bridge” driver is the default network driver in Docker and provides a bridge network that allows containers to communicate with each other.

Example output:

No output will be generated if the network is created successfully. To verify if the network has been created, you can use the docker network ls command again.

Use case 3: Display detailed information of a network

Code:

docker network inspect mynetwork

Motivation:

Inspecting a network allows you to view detailed information about its configuration, such as its IP range, gateway, and attached containers. This information can be useful for troubleshooting network-related issues or understanding the network setup of your Docker environment.

Explanation:

The docker network inspect command is used to display detailed information about a network. In this example, we inspect the “mynetwork” network. The command expects the name or ID of the network as an argument.

Example output:

[
    {
        "Name": "mynetwork",
        "Id": "abcdef123456",
        "Created": "2021-04-01T12:00:00Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "abcdefgh1234": {
                "Name": "mycontainer",
                "EndpointID": "ijklmnop5678",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Use case 4: Connect a container to a network

Code:

docker network connect mynetwork mycontainer

Motivation:

Connecting a container to a network allows it to communicate with other connected containers on the same network. This is useful when you want to ensure network connectivity between containers that need to interact with each other.

Explanation:

The docker network connect command is used to connect a container to a network. In this example, we connect the container named “mycontainer” to the “mynetwork” network. The command expects the network name or ID as the first argument and the container name or ID as the second argument.

Example output:

No output will be generated if the container is successfully connected to the network. To verify if the container is connected, you can use the docker network inspect command to view the network’s detailed information.

Use case 5: Disconnect a container from a network

Code:

docker network disconnect mynetwork mycontainer

Motivation:

Disconnecting a container from a network allows you to remove its connectivity to other containers on the same network. This is useful when you want to isolate a container and prevent it from communicating with other containers.

Explanation:

The docker network disconnect command is used to disconnect a container from a network. In this example, we disconnect the container named “mycontainer” from the “mynetwork” network. The command expects the network name or ID as the first argument and the container name or ID as the second argument.

Example output:

No output will be generated if the container is successfully disconnected from the network. To verify if the container is disconnected, you can use the docker network inspect command to view the network’s detailed information.

Use case 6: Remove all unused networks

Code:

docker network prune

Motivation:

Removing unused networks helps to keep your Docker environment clean and free of unnecessary network configurations. It can also conserve system resources and reduce clutter in the list of available networks.

Explanation:

The docker network prune command is used to remove all unused networks. Unused networks are those that are not referenced by any active containers. Running this command will prompt for a confirmation before removing the unused networks.

Example output:

Deleted Networks:
network1
network2

Use case 7: Remove specific unused networks

Code:

docker network rm network1 network2

Motivation:

Removing specific unused networks allows you to selectively clean up unnecessary network configurations. This can be useful when you want to remove specific networks that are no longer needed, instead of pruning all unused networks.

Explanation:

The docker network rm command is used to remove specific unused networks. In this example, we remove the “network1” and “network2” networks. These networks must be unused, meaning they are not referenced by any active containers.

Example output:

No output will be generated if the networks are successfully removed. To verify if the networks have been removed, you can use the docker network ls command again.

Conclusion:

The docker network command is a powerful tool for managing networks in Docker. It provides a wide range of operations for creating, listing, inspecting, connecting, disconnecting, and removing networks. These examples showcase the versatility of this command and its usefulness in various network-related scenarios. By leveraging the docker network command, you can effectively configure and manage networks to facilitate communication between containers within your Docker environment.

Related Posts

How to use the command findfs (with examples)

How to use the command findfs (with examples)

The findfs command is used to find a filesystem by its label or UUID.

Read More
How to use the command 'du' (with examples)

How to use the command 'du' (with examples)

The ‘du’ command is used to estimate and summarize the file and directory space usage on a system.

Read More
Using the timeout command (with examples)

Using the timeout command (with examples)

The timeout command is a powerful tool that allows you to run other commands with a time limit.

Read More