CodeGym /Courses /Docker SELF /Connection between containers

Connection between containers

Docker SELF
Level 17 , Lesson 2
Available

3.1 Basics of bridge-network

Bridge network is the default network driver in Docker and is used to create an isolated network where containers can interact with each other. This network lets containers communicate using IP addresses and hostnames, providing an easy way to organize interaction between various components of multi-container applications.

Bridge network creates a private internal network at the host level that is isolated from the host's external network. Containers connected to this network can communicate with each other but cannot be directly accessed from outside without configuring port forwarding.

Automatic creation of bridge-network

By default, Docker creates a bridge network during installation, and containers launched without specifying a particular network automatically connect to this network.

Example of creating and using containers in a bridge-network

1. Launching containers in the bridge network:

In this example, we launch two containers: container1 with Nginx and container2 with Busybox. Both containers are connected to the default bridge network.

Terminal

docker run -d --name container1 nginx
docker run -d --name container2 busybox sleep 1000

2. Checking the containers' IP addresses:

To check the IP addresses of the containers, use the docker inspect command.

These commands will output the IP addresses of the containers, which can be used for communication between them.

Terminal

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container1
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container2

3. Communicating between containers using IP addresses:

To test the communication between containers, use the ping command from container2 to ping container1.

If the containers are properly connected to the bridge network, the ping command will succeed.

Terminal

docker exec container2 ping -c 4 <IP_ADDRESS_OF_CONTAINER1>

Explanation: The bridge network provides a simple solution for local applications where containers need to interact with each other while remaining isolated from the external network. Pinging via IP address is a basic way to test network connection, but for more complex applications, DNS configuration or additional network drivers might be required.

3.2 Using Hostnames

Using hostnames to connect containers

Aside from using IP addresses, Docker also lets you use container names to connect them. This makes configuration easier since container names stay constant, unlike IP addresses, which can change when containers are restarted.

Example of connecting via hostname

1. Start containers specifying the bridge network:

Terminal

docker run -d --name webserver --network bridge nginx
docker run -d --name appserver --network bridge busybox sleep 1000
    

2. Check connection via hostname:

Use the ping command from the appserver container to ping webserver:

Terminal

docker exec appserver ping -c 4 webserver 
    

Creating a custom bridge network

Instead of using the default bridge network, you can create a custom bridge network to have better control over container interactions.

Example of creating a custom network

1. Create a custom network:

Terminal

docker network create --driver bridge my_bridge_network 
    

2. Start containers in the custom network:

Terminal

docker run -d --name webserver --network my_bridge_network nginx
docker run -d --name appserver --network my_bridge_network busybox sleep 1000 
    

3. Check connection via hostname:

Terminal

docker exec appserver ping -c 4 webserver 
    

3.3 Examples of Multiple Hosts

Examples of practical usage

Example 1: Web Server and Database

Create a file docker-compose.yml for a web server and database:

Yaml

version: '3.8'

services:
  web:
    image: nginx:latest
    networks:
      - my_bridge_network
        
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    networks:
      - my_bridge_network
        
networks:
  my_bridge_network:
    driver: bridge

Run Docker Compose:

Terminal


docker compose up -d 

Check the connection between containers:

Terminal


docker compose exec web ping -c 4 db 

Example 2: Microservices Architecture

Create a file docker-compose.yml for a microservices application:

Yaml

version: '3.8'

services:
  frontend:
    image: nginx:latest
    networks:
      - my_bridge_network
        
  backend:
    image: mybackend:latest
    networks:
      - my_bridge_network
        
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    networks:
      - my_bridge_network
        
networks:
  my_bridge_network:
    driver: bridge

Run Docker Compose:

Terminal

        
docker compose up -d 

Check the connection between containers:

Terminal

docker compose exec backend ping -c 4 db

docker compose exec frontend ping -c 4 backend 
3
Task
Docker SELF, level 17, lesson 2
Locked
Checking connectivity by hostname
Checking connectivity by hostname
3
Task
Docker SELF, level 17, lesson 2
Locked
Creating a custom network
Creating a custom network
3
Task
Docker SELF, level 17, lesson 2
Locked
Using Docker Compose for communication between containers
Using Docker Compose for communication between containers
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION