Wondering about docker network and how to create it? Here’s how we create it.
Here at Bobcares, we have seen several such Docker related queries as part of our Docker Hosting Support for Docker users, web hosts, and online service providers.
Today we’ll take a look at docker network and its creation.
More 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.
Viewing a network
Here is the command to 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
Create a docker network
Now let’s see how our Support Engineers create a bridge network and deploy a container on that network.
We run the below 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 isolated.
docker network create -d bridge isolated
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
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 any further assistance with Docker related queries? – We’re available 24*7]
Conclusion
In short, docker allows you to create networks and then deploy containers to it. Today, we saw how our Support Engineers create a docker network and attach a container to it.
0 Comments