Docker Communication Between Containers: Bridge Network Driver
Docker is the tool that is used for containerizing projects in a full isolation with their required packages, environment variables and system configs etc. Isolation in simple terms means that a container in which a project is running on is not reachable from outside. Maintaining the networking between different containers can be a disaster for newbies however Docker is handling this network issue smoothly.
Although there are different types of network drivers such as host, macvlan, overlay and ipvlan, bridge network driver is the main subject of this article. Bridge network driver is the default driver and it is generally used when there are several containers communicating each other on the same host.
Before explaining the implementation, here is the link of the repository of the final project. This project is a basic project with the purpose of being an example for bridge network.
First create two different folders named sender and receiver and run
npm init -y
command to start node projects. Then, create index.js for each file. Content of the files is shown below.
sender/index.js
const { default: axios } = require('axios');
const express = require('express');
const app = express();
const sendRequest = async() => {
try{
const request = await axios.get('http://receiver-container:8000/test');
console.log(request);
}catch(err){
console.log(err)
}
}
app.listen(process.env.PORT || 8001, () => {
console.log(`Sender server is awake at port ${process.env.PORT || 8001}`)
})
sendRequest();
receiver/index.js
const express = require('express');
const app = express();
app.get('/test', (req, res) => {
try{
console.log('Test request is handled successfully');
}catch(err){
console.log(err)
res.status(400).send(err);
}
})
app.listen(process.env.PORT || 8000, () => {
console.log(`Receiver server is awake at port ${process.env.PORT || 8000}`)
})
In the next step, basic Dockerfiles are added inside folders. Keep in mind that additional steps and optimizations can be added but not mentioned.
sender/Dockerfile
FROM node
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
ENV PORT=8001
EXPOSE 8001
CMD ["node", "index.js"]
receiver/Dockerfile
FROM node
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
ENV PORT=8000
EXPOSE 8000
CMD ["node", "index.js"]
These are really simple and basic Dockerfiles to build images.
Before creating images, create a bridge network.
docker network create networkName
List the networks to see if new network is created. There may be several networks which you have not created but it is totally normal.
docker network ls
Now, navigate to the folders with cd sender and cd receiver and build images.
docker build -t imageName .
After building, images can be listed with the command of
docker images
It is time to run containers but run the receiver container first because sender container will send an axios request instantly.
docker run -p 8000:8000 -d --rm --name containerName --network networkName imageName
in my case for receiver:
docker run -p 8000:8000 -d --rm --name receiver-container --network sender-receiver-net receiver-image
docker run -p 8001:8001 -d --rm --name containerName --network networkName imageName
in my case for sender:
docker run -p 8001:8001 -d --rm --name sender-container --network sender-receiver-net sender-image
Be sure that containers are up and working fine. To list containers
docker ps
Finally, it is possible to see the ‘Test request is handled successfully’ message in the logs of the receiver-container via the following command.
docker logs containerName
in my case:
docker logs receiver-container
As it is seen in the logs of the receiver-container, bridge network driver is used successfully between containers and communication is handled. The important point to catch here is that the url in axios request in sender/index.js file must be the same as the receiver-container name.
Lastly, bridge network driver is a useful tool for maintaining communication between containers on the same host. Other drivers have different use cases and are efficient in different scenarios. Thanks for reading so far, claps will be appreciated.
If you find it helpful, you can buy me coffee.