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

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",
        "Id":"3b7e1ad19ee8bec9628b18f9f3691adecd2ea3395ec248f8fa57a2ec85aa71c1",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1/16"
                }
            ]
        },
        "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

Conclusion

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

Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Ultahost
$2.90 /mo
Starting price
Visit Ultahost
Rating based on expert review
  • User Friendly
    4.3
  • Support
    4.8
  • Features
    4.5
  • Reliability
    4.0
  • Pricing
    4.8
  • Want to get top recommendations about best hosting? Just click this link!

How to Setup a Docker Swarm Cluster on a CentOS 7 VPS or Dedicated Server

This how-to article will help you install and configure Docker Swarm on CentOS 7
5 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How to Use Docker Containers with AWS EC2

This tutorial will help you deploy Docker containers in AWS and leverage the pow
4 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Deploy Docker Containers with OpenStack Heat

This guide is written to help users deploy Docker containers using OpenStackHeat
5 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How To Setup a Docker Swarm Cluster on Ubuntu 16.04 VPS or Dedicated Server

This article shows you how to set up a Docker Swarm Cluster on Ubuntu 16.04.
5 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top