Connecting Two Docker Containers Over the Same Network
Last Updated :
23 Jul, 2025
Whenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.
Docker offers an extensive set of networking options for controlling exactly how your containers connect to each other and making sure that it is secure and is the same as the way you wanted.

Before we dive into this article, We would recommend you to go through the basics of Docker and Computer Networking in order to get the picture more clear of what exactly is happening behind the scenes.
Communication of Containers:
Despite the isolation, containers frequently need to interact with one another and even with the outside world. This processing of interacting can be achieved by networking between containers. It essentially implies that an application in one container will establish a network connection over a port in another container. In this article, we will learn the basics of linking containers and making them communicate with each other. We will run ubuntu on two different containers, define a port number and let them communicate over a network.
Open the terminal.
Let's first have a look at the existing networks and see what's there by default.
docker network ls

We can see that there are 3 networks present by default:
- Bridge is the network used by containers and they don't specify the preference to be put into any other network. This means a bridge network gives you very basic communication between containers on the same host.
- Host is when you don't want the containers to have any network isolation. This implies this network can have security concerns.
- None is when your container can have no networking.
Create your own network with the command below:
docker network create learn-networking

In this way, we have created a network named learn-networking. You can give the network any name you wish. Now we will be connecting our containers over this network.
Open two terminals side by side. In one of the terminals type the below command to create one new container named conatiner1 over the network learn-networking.
docker run --rm -ti --net learn-networking --name container1 ubuntu:14.04 bash

Here:
- --rm commands to remove the container as soon as we stop it.
- -ti stands for terminal interactive.
- --net is used to set the network of the container.
- --name is used to specify the name of the container. Here the name of the container is container1.
Now on the other terminal create a new container named container2 over the same network learn-networking.
docker run --rm -ti --net learn-networking --name container2 ubuntu:14.04 bash
Now we have two containers running, container1 and container2.

Also, if you open your Docker Desktop and go to the Containers section, you'll see two containers that we just created running.
Now we'll specify a port number that container2 will listen to. So for this, the terminal where container2 is running, write the command
nc -lp 1234
This command says, netcat listen port 1234. You can specify any port number here.
On the terminal where container1 is running, connect it to container2 via port 1234 by writing the following command
nc container2 1234

After this, both the container are connected via port number 1234 on network learn-networking.
Type anything on terminal 1 and it will be replicated on terminal 2. This means sharing of data between two containers over a network has started.
Note: For sharing data and making the containers communicate with each other, they must be on the same network.
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle.The goals of DevOps are:Faster and continuous software releases.Reduces manual errors through a
7 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps