Docker Create Network

Out of the box, Docker offers a few default networks, such as bridge, none, and host. However, there are scenarios where creating a custom network becomes essential, like when building multi-tier applications. In this guide, we will learn how to create a custom Docker network.

Why a Custom Network? 

Enhances DNS Resolution: Containers can easily communicate via their names. 

Offers Network Segmentation: Different apps can be isolated in different networks. 

Allows Network Customization: Custom IP address ranges, gateways, and subnets can be defined. 

Step-by-Step Creation of a Docker Network

1. Verify Existing Networks: 

Before creating a new network, it's advisable to check the current networks:

docker network ls

2. Creating the Custom Network: 

The general syntax is:

docker network create [OPTIONS] NETWORK_NAME

For instance, to create a bridge network named my_custom_network:

docker network create --driver bridge my_custom_network
The --driver option in the docker volume create command specifies which volume driver should be used to create the volume.

Local Driver: By default, if you don't specify a driver, Docker uses the local driver. This means the volume's data will be stored in a directory on the Docker host's local filesystem.
docker volume create myvolume
If a name is not specified, Docker generates a random name: 
$ docker volume create 

3. Specifying Subnets and Gateways (Advanced): 

For more granular control, you can specify the subnet, the IP range, and the gateway:

docker network create --driver bridge --subnet --ip-range --gateway my_advanced_network

4. Connecting Containers to the Network: 

Once created, you can either start a new container in this network or connect an existing container. Starting a new container in the network:

docker run --network=my_custom_network -d nginx

Connecting an existing container:

docker network connect my_custom_network container_name_or_id

5. Inspecting the Network: 

Let's see how to inspect the existing network:

docker network inspect my_custom_network

The docker network inspect command provides detailed information about a specific Docker network.

If my_custom_network doesn't exist, the command will return an error saying the network is not found.

6. Clean Up: Removing the Network 

Should you no longer require the network, you can easily remove it. Ensure no active containers are connected to it:

docker network rm my_custom_network


In this guide, we have learned how to create volume, remove volume, list volumes, and inspect volume.

Related Networking Guides