Communication/networking between containers
so let us say we have 2 dockers running independently eg. a node app and a Redis server
there are two ways to make them communicate
1. Using the Docker CLI and setting up port etc
but we have to do this every time and this is a pain so kinda not used much in the industry
2. Using docker compose
The big purpose of docker compose is that we can avoid writing all CLI docker commands everytime we want to use it and start a container. Here we can start multiple Docker containers at the same time and automatically connect them with some form of networking. Helps issue multiple commands very quickly.
we will see services a lot in the commands and anytime we see this, it will essentially be CONTAINER
eg.
version: '3'
services:
redis-server:
image:'redis'
node-app:
build: .
ports:
- "4001":"8081"
with services, we are specifying that we want these two containers
since we want to download the redis we specify the image name for redis
and bcz we want to build node-app from our local code we tell it to build
'-' in the yml file means array so can have multiple ports mapped
4001 is our local server and 8081 is the port inside of the container
by using docker-compose to set up the two containers it will automatically set up the networking between the two. The two containers will have free access to each other without opening up any ports between the two/
The opening of the port that we have done here is not needed for the communication between the 2 containers, its just there to communicate with the main machine
const express = require('express');
const redis = require('redis')
const app = express();
const client = redis.createClient({
// host:"ideally will have the url of redis"
host:"redis-server"// name give for service in the docker-compose
});
//initialise visits in redis
client.set('visits', 0);
app.get('/',(req, res) =>{
client.get('visits',(err , visits) => {
res.send('Number of visits is'+ visits);
client.set('visits', pareseInt(visits) + 1);
});
});
app.listen(8081, () => {
console.log('listening on port 8081');
})
so let us say we have 2 dockers running independently eg. a node app and a Redis server
there are two ways to make them communicate
1. Using the Docker CLI and setting up port etc
but we have to do this every time and this is a pain so kinda not used much in the industry
2. Using docker compose
The big purpose of docker compose is that we can avoid writing all CLI docker commands everytime we want to use it and start a container. Here we can start multiple Docker containers at the same time and automatically connect them with some form of networking. Helps issue multiple commands very quickly.
we will see services a lot in the commands and anytime we see this, it will essentially be CONTAINER
eg.
version: '3'
services:
redis-server:
image:'redis'
node-app:
build: .
ports:
- "4001":"8081"
with services, we are specifying that we want these two containers
since we want to download the redis we specify the image name for redis
and bcz we want to build node-app from our local code we tell it to build
'-' in the yml file means array so can have multiple ports mapped
4001 is our local server and 8081 is the port inside of the container
by using docker-compose to set up the two containers it will automatically set up the networking between the two. The two containers will have free access to each other without opening up any ports between the two/
The opening of the port that we have done here is not needed for the communication between the 2 containers, its just there to communicate with the main machine
const express = require('express');
const redis = require('redis')
const app = express();
const client = redis.createClient({
// host:"ideally will have the url of redis"
host:"redis-server"// name give for service in the docker-compose
});
//initialise visits in redis
client.set('visits', 0);
app.get('/',(req, res) =>{
client.get('visits',(err , visits) => {
res.send('Number of visits is'+ visits);
client.set('visits', pareseInt(visits) + 1);
});
});
app.listen(8081, () => {
console.log('listening on port 8081');
})
___________________________________________________________________________
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4001:8081"
_____________________________________________________________________
To run the docker of images through docker-compose
old -> docker run myimage
new-> docker-compose up
new command will read the docker-compose.yml file
if you want to rebuild the images
old -> docker build .
new -> docker-compose up --build
To launch a group of containers in the background
docker-compose up -d
to stop containers
docker-compose down
______________________________________________________________________
How to handle container when it has crashed
Restart policy inside docker container file:
"no" -> never attempt to restart
always -> always attempt to restart
on-failure -> only with container stops with error code ie non-zero
unless-stopped -> always restart unless we forcibly stop it
for the no we need to put "no" bcz no in yml is taken as false
version: '3'
services:
redis-server:
image: 'redis'
node-app:
restart: always
build: .
ports:
- "4001:8081"
to check the running docker-compose processes we use
docker-compose ps
to get output from this we need to run this command only where docker-compose is bcz it reads the docker-compose.yml file
Comments
Post a Comment