How Do You Create Custom Docker Networks?

Creating custom Docker networks helps us manage how containers talk to each other better. We can keep them separate from the host network and from other containers. Custom networks give us better security and more choices. We can decide how our containers connect and work together. When we use Docker networks, we can arrange our application in a way that fits our needs.

In this article, we will look at how to create custom Docker networks. We will give a simple guide on how to do it. We will talk about the different kinds of Docker networks we can use. We will also learn how to use the Docker command to create a network. We will cover how to check custom networks, how to connect containers to them, and how to delete networks when we do not need them anymore. Here are the parts we will discuss:

  • How Can You Create Custom Docker Networks Step by Step?
  • What Are the Different Types of Docker Networks?
  • How Do You Use the Docker Network Create Command?
  • How Can You Inspect a Custom Docker Network?
  • How Do You Connect Containers to a Custom Docker Network?
  • How Can You Remove a Custom Docker Network?
  • Frequently Asked Questions

If you want to read more about Docker networks, you can check out what are Docker networks and why are they necessary and how do Docker containers communicate with each other. These resources can give us more details about Docker’s networking and how containers work together.

What Are the Different Types of Docker Networks?

Docker has many types of networks to help containers talk to each other. It is important to understand these networks. This helps us create custom Docker networks for our applications. Here are the main types of Docker networks:

  1. Bridge Network:
    • This is the default network type.

    • It is an isolated network for containers on the same host.

    • Containers can talk to each other using their IP addresses or names.

    • Here is an example of how to create a bridge network:

      docker network create my_bridge_network
  2. Host Network:
    • This network removes the isolation between the container and the Docker host.

    • The container uses the host’s networking stack.

    • This is good for applications that need high performance.

    • Here is an example of running a container with the host network:

      docker run --net host nginx
  3. Overlay Network:
    • This network lets containers on different Docker hosts communicate.

    • We normally use it in Docker Swarm for service discovery.

    • It needs a key-value store like etcd or Consul.

    • Here is an example of creating an overlay network:

      docker network create -d overlay my_overlay_network
  4. Macvlan Network:
    • This network allows containers to have their own MAC addresses.

    • It is useful for applications that need to connect directly to the physical network.

    • We can set it up to connect to existing networks.

    • Here is an example of creating a macvlan network:

      docker network create -d macvlan \
        --subnet=192.168.1.0/24 \
        --gateway=192.168.1.1 \
        -o parent=eth0 my_macvlan_network
  5. None Network:
    • This network turns off all networking for the container.

    • It is useful for containers that do not need network access.

    • Here is an example of running a container without a network:

      docker run --net none alpine

Each of these network types has different uses. We can pick one based on what our application needs. For more information on how Docker networks work, we can check What Are Docker Networks and Why Are They Necessary?.

How Do You Use the Docker Network Create Command?

We can create a custom Docker network using the docker network create command. This command helps us set network options. We can choose the network driver, subnet, and gateway.

Basic Syntax

docker network create [OPTIONS] NETWORK_NAME

Options

  • --driver or -d: This lets us choose the network driver like bridge, overlay, or macvlan.
  • --subnet: We can define a custom subnet for the network, for example, 192.168.1.0/24.
  • --gateway: We can set a custom gateway for the network like 192.168.1.1.
  • --attachable: This allows containers to be attached to the network when we use overlay networks.

Example: Creating a Bridge Network

docker network create --driver bridge my_bridge_network

Example: Creating a Network with Custom Subnet and Gateway

docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 my_custom_network

Viewing Created Networks

We can view all networks with this command:

docker network ls

This command shows us all available Docker networks. It includes the ones we have created. For more details about network types, you can check What Are the Different Types of Docker Networks?.

How Can We Inspect a Custom Docker Network?

To inspect a custom Docker network, we can use the docker network inspect command. This command gives us detailed info about the network. It shows us the network’s setup, the containers connected to it, and more.

Command Syntax

docker network inspect <network_name_or_id>

Example

If we want to inspect a network called my_custom_network, we run:

docker network inspect my_custom_network

Output

The output comes in JSON format. It usually includes these details:

  • Name: The name of the network.
  • Id: The unique ID of the network.
  • Created: When the network was created.
  • Scope: The network’s scope (local or global).
  • Driver: The network driver used (like bridge or overlay).
  • Containers: A list of containers that are connected to the network with their details.
  • IPAM: Info about the IP address management for the network. It includes the subnet and gateway.

Sample Output

[
    {
        "Name": "my_custom_network",
        "Id": "e3b1f3c8e5b1e40f7a1e8f7b5a1f3c8e5b1e4f7c8f7b5e1",
        "Created": "2023-10-20T10:00:00.000000000Z",
        "Scope": "local",
        "Driver": "bridge",
        "Containers": {
            "container_id_1": {
                "Name": "my_container",
                "EndpointID": "e3b1f3c8e5b1e40f7a1e8f7b5a1f3c8e5b1e4f7c8f7b5e1",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },
        "IPAM": {
            "Driver": "default",
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        }
    }
]

This command helps us manage and fix issues with custom Docker networks. For more information about Docker networking, we can visit What Are Docker Networks and Why Are They Necessary?.

How Do We Connect Containers to a Custom Docker Network?

To connect containers to a custom Docker network, we just need to follow a simple process. First, we make sure that the custom network is created. Then we can connect our containers to this network when we create them or by using the docker network connect command.

Connecting Containers When We Create Them

When we create a new container, we can use the --network flag to specify the custom network. Here is an example:

docker run -d --name my_container --network my_custom_network nginx

In this example, we create a new container called my_container and connect it to my_custom_network.

Connecting Existing Containers

If we have a container that is already running and we want to connect it to a custom network, we can use the docker network connect command:

docker network connect my_custom_network my_existing_container

This command connects my_existing_container to my_custom_network.

Disconnecting Containers

If we need to, we can also disconnect a container from a network like this:

docker network disconnect my_custom_network my_container

Verifying Connections

To check which containers are connected to a specific network, we can use:

docker network inspect my_custom_network

This command shows us detailed information about the network and the containers connected to it.

For more information on Docker networks, we can look at What Are Docker Networks and Why Are They Necessary?.

How Can We Remove a Custom Docker Network?

To remove a custom Docker network, we can use the docker network rm command. This command helps us delete a network that we do not need anymore. Here are the steps to remove a custom Docker network:

  1. List Existing Networks: Before we remove a network, we should list all networks. This helps us make sure we delete the right one.

    docker network ls
  2. Remove the Network: We can use the docker network rm command with the network name or ID.

    docker network rm <network_name_or_id>

    For example, if we want to remove a network called my_custom_network, we use:

    docker network rm my_custom_network
  3. Check for Containers: We need to make sure no containers are using the network we want to remove. If there are containers connected, we have to disconnect them first.

    docker network inspect <network_name_or_id>
  4. Disconnect Containers: If there are containers connected to the network, we disconnect them like this:

    docker network disconnect <network_name_or_id> <container_name_or_id>
  5. Force Remove: If we really want to remove a network while it is in use (not a good idea), we can use the -f flag:

    docker network rm -f <network_name_or_id>

After we follow these steps, the custom Docker network will be removed. For more info about Docker networks, we can check the article on what are Docker networks and why are they necessary.

Frequently Asked Questions

1. What is a custom Docker network?

A custom Docker network is a network that we make ourselves. It helps us control how containers talk to each other. Unlike the default bridge network, custom networks let us set specific details like IP addresses and network isolation. This can make our applications safer and work better. For more info on Docker networks, check out what are Docker networks and why are they necessary.

2. How do I troubleshoot issues with my Docker network?

To fix problems with the Docker network, we should first check the network setup with the command docker network inspect <network_name>. This shows us details about connected containers and their IP addresses. Next, we should check if the containers can reach each other by using commands like ping. Also make sure that firewall settings on the host do not block the needed ports. For more details on how containers talk to each other, visit how do Docker containers communicate with each other.

3. Can I use multiple custom Docker networks for a single container?

Yes, one Docker container can connect to many custom Docker networks. This gives us more options in how we design our apps. It allows different containers to talk over different networks. To add a container to another network, we use the command docker network connect <network_name> <container_name>. For more insights, you might find what are bridge networks in Docker helpful.

4. How can I list all Docker networks?

We can see all Docker networks on our system by using the command docker network ls. This command gives us a list of all networks, with their names and IDs. If we want to check a specific network more closely, we can use docker network inspect <network_name>. For a better understanding of Docker networks, check out how to use the Docker network create command.

5. What happens if I remove a custom Docker network?

If we remove a custom Docker network with the command docker network rm <network_name>, all containers that are using that network will be disconnected. But the containers will still be there and we can connect them to other networks. Make sure we don’t need the network for any container communication before we remove it. To learn more about managing networks, refer to how can you remove a custom Docker network.