Docker Create Network

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

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 
d7fb659f9b2f6c6fd7b2c796a47441fa77c8580a080e50fb0b1582c8f602ae2f

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 192.168.0.0/16 --ip-range 192.168.1.0/24 --gateway 192.168.1.1 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

Conclusion 

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

Related Networking Guides

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare