×


Docker Network How to create it

Are you trying to create a docker network?

This guide is for you.


Docker networking allows you to attach a container to as many networks as you like. You can also attach an already running container.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Docker related tasks.

In this context, we shall look into the steps to create docker network.


More information about Docker network ?

Docker allows you to create networks and then deploy containers to it. By default, docker creates three networks. they are bridge, none, and host.

Bridge network is created automatically with a subnet and a gateway.

None lacks a network interface, however, it provides a container-specific network stack.

Host network allows a container to attach to the host’s network. It means the configuration within the container matches the configuration outside the container.


How to view a docker network ?

With the command provided below, you can view the current list of Docker networks:

docker network ls

To access further details of a particular network, run the below command where NAME is the name of the particular network.

docker network inspect NAME

To view the details of a bridge network, the command is:

docker network inspect bridge

How to create a docker network ?

Here, we will create a bridge network and deploy a container on that network.

To do this, execute the command to create a network:

docker network create -d [driver] [new network name]

Note: The -d flag allows you specify the driver for the network. For a bridge network, use the bridge driver.

For instance, we are creating a bridge network named project1, simply execute:

docker network create -d bridge project1

As a result, the output of the above command will be a long string of characters that will represent the ID of the newly created network.

In case, if you want to create a network with a specific subnet and gateway then run the below command:

docker network create –driver=bridge –subnet=1xx.1xx.x.x/24 –gateway=1yy.1yy.y.yy new_subnet

Once this is created, we inspect it by running the command:

docker network inspect new_subnet

How to attach a container to a network ?

Here is the default command for attaching a container to a network.

docker run –network=[network name] -itd –name=[container name] [image]

For instance, let's attach a container to our newly created network. Also, let’s consider that we have already pulled an image nginx. Then we want to launch a container called docker-nginx, attached to the isolated network. 

Here is the command for it:

docker run –network=isolated -itd –name=docker-nginx nginx

Then if you run 'docker network inspect isolated' command, you will see that the container is attached.

Moreover, if you create any further container on this network, it will automatically connect to each other.


[Need urgent assistance with Docker related queries? – We're available 24*7. ]


Conclusion

This article will guide you on how to create a #docker #network and attach a #container to it. If you want to add a container to a network after the container is already running, use the docker network connect subcommand. You can connect multiple containers to the same network. Once connected, the containers can communicate using only another container's #IP address or name.

There are three common Docker network types:

1. bridge networks, used within a single host.

2. overlay networks, for multi-host communication.

3. macvlan networks which are used to connect Docker containers directly to #host network interfaces.