How to Create a Network of Docker Containers (v18.03) on a single machine

If you are planning to deploy your web app with Docker, you’ll definitely benefit from docker networks knowledge. Docker has revolutionized the microservices architecture in recent years. Applications built on microservices architecture and deployed with docker are immensely flexible and scalable. These applications contain more than one containers, each running as a microservice.

In this case, we need to build a docker network in which these containers communicate with each other to fulfill the apps functional and non-functional requirements.

For such apps, we use docker’s user-defined bridge network. By default, every container runs in a network which is also called bridge but it is different from a user-defined bridge network.

Also by running your container’s in the same network, you increase security. All the ports of containers running in the same network are exposed to each other but not to outside world.

For example, If your app is running in 3 standalone containers frontend, backend and database then you only need to expose frontend ports to the outside world. Backend and database are to be accessed by frontend only, and it will be able to access them because of running in the same network without exposing them to the outside world.

Let’s follow the above example for a demonstration.

How to Create a User Defined Bridge Network

Run the following command to create your own bridge network named “my-net”.

$ sudo docker network create my-net

Run the following command to confirm that your network “my-net” is created and is listed in output of all docker networks present currently.

$ sudo docker network ls

Run the following command to see the details of your network “my-net”.

$ sudo docker network inspect my-net

You will see a similar output.

        "Name": "my-net",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                    "Subnet": "",
                    "Gateway": ""
        "Internal": false,
        "Containers": {},
        "Options": {},
        "Labels": {}

Currently no containers are running in this network.

How to Run a Container in Your Own Network

Now let’s jump back to our example of an app with 3 containers.

Run the following command to run your “frontend” container in network “my-net”.

$ sudo docker run --network=my-net frontend

Run “backend” and “database” containers similarly with flag –network=my-net.

$ sudo docker run --network=my-net backend
$ sudo docker run --network=my-net database

Now run the following command to see the details of you network “my-net”. You will see all three containers attached to your network “my-net” in the output.

$ sudo docker network inspect my-net

Congrats! All your containers are running in a custom bridge network and are able to communicate with each other.

How to Connect an Already Running Container to Your Network

You can also connect an already running container to your network. Let suppose your “frontend” containers were already running before you created network “my-net”, then you can connect “frontend” (name of the container) with “my-net” with the following command.

$ sudo docker network connect my-net frontend


So you can easily connect your standalone containers to the same network either at the creation time or after they are already in running state. You should Always prefer docker networking instead of making containers communicate through their assigned IP addresses manually which is very messy and vulnerable.

Also, it gives you a lot of flexibility e.g using other containers’ names inside a container, running within the same network, instead of IP addresses for communicating.

Check out these top 3 Best web hosting services

  • Want to get top recommendations about best hosting? Just click this link!

Was this article helpful?