How to connect two containers in Docker

Key takeaways:

  • Connecting containers in Docker is crucial for enabling communication and resource sharing between them. Docker’s networking features, particularly bridge networks and Docker Compose, simplify the setup and management of these connections for complex applications.

  • Docker networks help organize containers into logical groups for better communication. Containers use the bridge network by default, but users can also create custom networks. An example demonstrates building a Docker image with Ubuntu and the ping utility to test connectivity between two containers.

  • After creating and running two containers, users can inspect their IP addresses and network IDs to confirm they are on the same network. Pinging between the containers verifies successful communication, highlighting the importance of inter-container connectivity in application development.

Connecting two containers in Docker allows them to communicate with each other and share resources. Docker’s networking capabilities, such as bridge networks and Docker Compose, make setting up and managing these connections easy. This is crucial for creating complex applications where different services need to interact. Docker facilitates this connectivity through networking features, enabling seamless integration and container interaction.

Using Docker networks

Docker networks offer a means of organizing containers into logical groups, facilitating communication between them. While creating and assigning containers to custom networks is possible, the default behavior is for all containers to use the bridge network. In the upcoming example, we will leverage this default behavior. Specifically, we will build a basic Docker image featuring an Ubuntu base with the addition of the ping utility. Subsequently, we will establish two containers and observe their intercommunication within the bridge network.

Example: Connecting containers

Run the following code and copy/paste the commands we have mentioned below to connect Docker containers:

FROM ubuntu:latest

# Update the package index and install ping
RUN apt-get update && \
    apt-get install -y iputils-ping
Testing the connection

This Dockerfile creates a Docker image based on the latest Ubuntu distribution and installs the ping utility. The RUN instruction performs the update of the package index and then installs the ping command, which is useful for testing network connectivity between containers.

Create and run the first container

After clicking the “Run” button, the image will start building. Subsequently, you can execute the following command to create the first container:

docker run -it --name "firstcontainer" "ubuntu-image"
Creating and running the first container

Create and run the second container

Upon running the above command, click on the “+” to open a new terminal and execute the following command to create and run the second container:

docker run -it --name "secondcontainer" "ubuntu-image"
Creating and running the second container

Inspect network IDs and IP addresses

Once you have created both containers, open another terminal to check each container's IP address and network ID. This step helps verify that both containers share the same network. Execute the following commands:

docker inspect -f '{{ range .NetworkSettings.Networks }}{{ .NetworkID }} {{ .IPAddress }}{{ end }}' firstcontainer
Inspecting the network ID and IP address of first container

Similarly, inspect network ID and IP address of second container:

docker inspect -f '{{ range .NetworkSettings.Networks }}{{ .NetworkID }} {{ .IPAddress }}{{ end }}' secondcontainer
Inspecting the network ID and IP address of second container

Testing the connection

Copy the IP addresses and use them to ping from one container to the other. Move to the first container and run the following to test the connection:

ping [IP address of the second container]
Testing connection of second container in first container

Similarly, in the second container, run the following to test the connection:

ping [IP address of the first container]
Testing connection of first container in second container

This will confirm intercommunication between the containers.

Note: Press “Ctrl+C” to stop the execution when needed.

Conclusion

By using Docker’s default bridge network, we’ve demonstrated how containers can communicate with each other. This is essential for services that need to interact within the same application or across distributed systems.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can Docker containers interact with each other?

Yes, Docker containers can interact with each other. They can communicate over networks, share data through volumes, and even access each other’s services. By default, containers run in isolation, but they can be connected via Docker networking features, such as creating a custom bridge network or using Docker Compose for multi-container applications.


Can I run two Docker containers at once?

Yes, you can run multiple Docker containers simultaneously. Docker allows you to start and manage as many containers as needed, either individually or by grouping them together with Docker Compose. Containers share the host system’s resources, but each container operates in its own isolated environment.


How do I allow two containers to talk to each other?

To allow two Docker containers to communicate, you can place them on the same Docker network. By default, containers on the same bridge network can communicate with each other by referencing their container names. Another option is using Docker Compose to define services and their network settings, ensuring seamless communication between containers. You can also expose specific ports for direct access between containers.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved