Tuesday, August 11, 2020

Docker_Compose_With_Mulitple_Local_Containers

 To do below

i.e Get the number of Impressions.


and scale them as per need.

Same steps what we followed


https://automationknowledgesharing2020.blogspot.com/2020/08/dockerlearning.html


i.e


1.package.json

2.index.js

3.Dockerfile


Create new file


C:\Users\manjunathk\impressions>


add/create the below



Steps


docker build .

docker build -t manjunathk/impressions:latest .

docker run manjunathk/impressions

docker run redis


2 containers are isolated




Note: Get rid off Docker cli we shall use Docker Compose





services are  type of container 


2 services i.e redis and node-app inside docker-compose.yml file  take the form of 2 different docker container

so create docker-compose.yml as below








index.js

const express = require("express");

const redis = require("redis");

const process = require("process");


const app = express();

const client = redis.createClient({

  host: "redis-server",

  port: 6379

});

client.set("visits", 0);


app.get("/", (req, res) => {

  //process.exit(0)

  client.get("visits", (err, visits) => {

    res.send("Number of visits " + visits);

    client.set("visits", parseInt(visits) + 1);

  });

});


app.listen(8081, () => {

  console.log("listening on port 8081");

});


Note:

If running container crashes, we can follow belowsteps,






package.json

{
  "dependencies": {
    "express": "*",
    "redis": "2.8.0"
  },
  "scripts": {
    "start": "node index.js"
  }
}

Dockerfile

FROM node:alpine

WORKDIR '/app'

COPY package.json .
RUN npm install
COPY . .

CMD ["npm","start"]

docker-compose.yml

version: "3"
services:
  redis-server:
    image: "redis"
  node-app:
    restart: on-failure
    build: .
    ports:
      - "4001:8081"



docker ps

docker stop 6d40abcbf151 35f8c7f9bc18




after stopping containers





docker-compose up




docker-compose ps










No comments:

Post a Comment

Selenium_Grid_With_Docker_Compose(yml file)

 docker-compose.yml version: "3" services:   hub:     image: selenium/hub:3.141.59     ports:       - "4444:4444"   chro...