Skip to main content

docker compose

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');
})

___________________________________________________________________________
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

Popular posts from this blog

opening multiple ports tunnels ngrok in ubuntu

Location for the config yml file /home/example/.ngrok2/ngrok.yml content of config file authtoken: 4nq9771bPxe8ctg7LKr_2ClH7Y15Zqe4bWLWF9p tunnels: app-foo: addr: 80 proto: http host_header: app-foo.dev app-bar: addr: 80 proto: http host_header: app-bar.dev how to start ngrok with considering the config file: ngrok start --all

rename field in elastic Search

https://qiita.com/tkprof/items/e50368eb1473497a16d0 How to Rename an Elasticsearch field from columns: - {name: xxx, type: double} to columns: - {name: yyy, type: double} Pipeline API and reindex create a new Pipeline API : Rename Processor PUT _ingest/pipeline/pipeline_rename_xxx { "description" : "rename xxx", "processors" : [ { "rename": { "field": "xxx", "target_field": "yyy" } } ] } { "acknowledged": true } then reindex POST _reindex { "source": { "index": "source" }, "dest": { "index": "dest", "pipeline": "pipeline_rename_xxx" } }

Sumeru enterprise tiger privacy policy

Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. Data Privacy Policy At Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. we are committed to providing you with digitalization software products and services to meet your needs. Our commitment includes protecting personally identifiable information we obtain about you when you register to use one of our websites or become our customer (“Personal Information”). We want to earn your trust by providing strict safeguards to protect your Personal Information. This Policy applies to members, customers, former customers, users, and applicants. In the course of our business activities, Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. collects, processes, and shares Personal Information. Indian law gives individuals the right to limit some but not all sharing. This Policy explains what Personal Information we collect, process, and share. We describe how we do so, and why. The Policy also describes your rights to access a...