Showing posts with label Cloud. Show all posts
Showing posts with label Cloud. Show all posts

Tuesday, 21 January 2025

Docker: Accelerated Container Application Development

 

Docker: Accelerated Container Application Development

1. Developers can deploy, run, and manage applications.

2. Deploy applications quickly.

3. Docker is a containerization platform that simplifies the process of deploying and managing applications. Applications run along with their dependencies. 

Docker Images :

1. A copy of our project.

2. Images are made from source code, libraries, and external dependencies.

3. Images are created from source code.

4. Example: Node setup, application code, installed libraries, Docker files.

5. Once an image is created, it cannot be updated; we can create a new image instead.

Docker Container :

1. A container runs applications using images.

2. Containers run independently.

3. Example: If 3 images are available, all these images can run independently.  

Docker Commands :

1. docker build -t folder/dockername . // Creates a project image.

2. docker run -p 5000:5000 -d dockerImageName // Runs a container.

3. docker ps // Shows all running containers.

4. docker logs container_id // Displays project logs.

5. docker image prune // Removes unused images.

6. docker service scale web-app=2 // Scales the web-app service to two replicas.

7. docker service update --image <new-image> service_name // Updates a Docker container's image without causing downtime. 

Docker File Example :

1. Create a file named Dockerfile in the root directory of your backend project.

2. In this Dockerfile, write the following code:

# Use Node.js version 20 as the base image
FROM node:20  

# Set the working directory inside the container
WORKDIR /node-practical  

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./  

# Install dependencies
RUN npm install  

# Copy the entire project to the container
COPY . .  

# Specify the command to run the application
CMD ["node", "index.js"]

3. Here’s an example of a docker-compose.yml file for a microservices architecture:

    docker-compose.yml Example : 

version: '3.9'
services:
  db:
    image: postgres:14-alpine
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: DBPass@123
      POSTGRES_DB: DBNAME
      POSTGRES_HOST_AUTH_METHOD: trust        
     
  gateway:
    build:
      dockerfile: Dockerfile
      context: ./gateway
    command: npm run start:prod
    restart: always  
    ports:
      - "5000:5000"
    depends_on:
      - db      
    environment:
      JWT_SECRET: secret@123
      JWT_EXPIRESIN: 2d
      REDIS_HOST: localhost
      REDIS_PORT: 6379
      REDIS_DB: TestFinAssetMgmt //if use redis
    volumes:          
      - /gateway/src/app/node_modules
 
  auth:
    build:
      dockerfile: Dockerfile
      context: ./auth
    command: npm run start:prod
    restart: always
    ports:
      - "5100:5100"
    depends_on:
      - db      
    environment:
      DB_TYPE : postgres
      DB_HOST : HOST_name
      DB_PORT : 5432
      DB_USERNAME : DBUSERNAME
      DB_PASSWORD : DB PASS
      DB_DATABASE : DBDATABASENAME
      DB_SYNC : true
      JWT_SECRET : secret@123
      JWT_EXPIRESIN : 2d
      RESET_PASSWORD_URL : http://3.15.153.226:3000
    volumes:          
      - /auth/src/app/node_modules

  mail:
    build:
      context: ./mail
    command: npm run start:prod
    restart: always
    ports:
      - "5200:5200"
    depends_on:
      - db
      - auth
      - gateway      
    environment:
      EMAIL_HOST : smtp.gmail.com
      EMAIL_USER : SMTP-email-pass
      EMAIL_PASSWORD : smtp-password
    volumes:          
      - /mail/src/app/node_modules



AWS SES : Simple Email Service

 


AWS SES : Simple Email Service

1. Download access key and secret key excel file.

2. Go to verified identity -> Create Identity -> Verify email received in email account and verified.

3. npm install aws-sdk

const AWS = require("aws-sdk");

const awsConfig = {
  accessKeyId: AWS_ACCESS_KEY_ID,
  secretAccessKey: AWS_SECRET_ACCESS_KEY,
  region: process. AWS_REGION,
};

const SES = new AWS.SES(awsConfig);

const sendEmail = async () => {

  try {
    //prepare email to send

    const params = {
      Source: "verifiedemailaddress@gmail.com"  //verified email address
      Destination: {
        ToAddresses: "to@gmail.com",
      },
      Message: {
        Subject: {
          Charset: "UTF-8",
          Data: "OTP Verification",
        },
        Body: {
          Html: {
            Charset: "UTF-8",
            Data: `<h1>Your verification code is 12345</h1>`,
          },
        },
      },
    };

    const emailSent = SES.sendEmail(params).promise();
    emailSent
      .then((data) => {
        console.log(data);
      })
      .catch((err) => {
        console.log(err);
      });
  } catch (error) {
    console.log(error);
  }
};

module.exports = sendEmail;