Giter VIP home page Giter VIP logo

21bcp275.github.io's Introduction

Sure, I can format and expand the Dockerfile explanation to make it more organized and clear. Here's the revised version:

# Docker File Explanation

The Dockerfile specifies the instructions to build an image for running our application inside a container.

### Base Image Selection

The starting of the Dockerfile specifies the author name and the Dockerfile version.

```Dockerfile
FROM node:18.16.0
FROM python:3.8-slim

These are the image distributions along with their versions which will be used for executing our application inside the container.

Setting Working Directory

WORKDIR /backend

With this command, we are setting the working directory as the backend, which specifies the reference directory and files from the /backend directory level in the container file system.

Updating System and Installing Dependencies

RUN apt-get update && apt-get upgrade -y && \
    npm

This command updates the system after package installation, as done in the first step.

First Stage of Installation

COPY package*.json ./
RUN npm install

Firstly, this copies the server package.json dependency file. We aim to first download all the necessary dependencies and then copy the entire codebase to run the application in the container.

COPY . .

This copies all the files from the home directory to the container directory i.e /backend.

Second Stage of Installation and Configuration

WORKDIR /backend/ml_model
COPY ./ml_model/requirements.txt .

Setting the working directory in the container file system 1 layer below of /backend for ml_model. Copying only the requirements first. This command will copy the contents of ml_model directory to the container /backend/ml_model directory.

RUN pip install -r requirements.txt

Installing all the requirements for running the Python application.

COPY ./ml_model ./backend/ml_model

Copying all the code in the ml_model local directory to the container directory.

COPY ./configure-cloudsql-password.sh ../scripts

In order to configure the password for Cloud SQL for storing the predictions of ml_model, I have written the bash file for performing and configuring the Google Cloud Cloud SQL. This copies the bash file into the /scripts directory at the /backend level.

Environment Variables Configuration

ENV GOOGLE_CLOUD_PROJECT ${GOOGLE_CLOUD_PROJECT}
ENV GOOGLE_CLOUD_SQL_USERNAME ${GOOGLE_CLOUD_SQL_USERNAME}
ENV GOOGLE_CLOUD_SQL_PASSWORD ${GOOGLE_CLOUD_SQL_PASSWORD}
ENV GOOGLE_CLOUD_SQL_DATABASE ${GOOGLE_CLOUD_SQL_DATABASE}
ENV GOOGLE_CLOUD_SQL_HOST ${GOOGLE_CLOUD_SQL_HOST}
ENV GOOGLE_CLOUD_SQL_PORT ${GOOGLE_CLOUD_SQL_PORT}

As you see, the environment variables specified after ${} needs to be configured by the automated workflow while initializing the VM on which the application needs to be run. Here VM is an abbreviation of the Linux system on which the containers are executed.

Running Python Script and Exposing Ports

RUN python3 db.py

Running the db.py file for using the Cloud SQL.

EXPOSE 3000

Exposing container port 3000 for open traffic to access the application.

CMD ["python3", "stock_market_prediction.py"]

Start executing the predictions by running the Python scripts.

CMD ["npm", "start"]

Now we will build the docker image and then upload it on the docker hub repository

Same the docker file of the frontend will look like this

Docker frontend code

docker build -t 21bcp275_backend .

Docker build backend

docker tag 21bcp275_backend priyankvaidya/21bcp275priyank:latest

Next step is to push to the docker hub

docker push priyankvaidya/21bcp275priyank:latest

Now finding all of the docker images on our local system

docker images

Docker images

Docker-compose.yml

The Docker Compose file specifies the configuration for running interdependent containers.

db

It uses the Postgres public image from the Docker registry and runs the necessary configuration set up from configuration-postgres.sh bash file.

There are some other parameters e.g. data path, volume, bridge.

app

Uses the Dockerfile as its dependency on executing application. It depends on the db services and is connected to the same network as the db, i.e. bridge.

psql

Depends on db service and connected to the same network for execution.


This structured explanation provides a clear understanding of the Dockerfile and Docker Compose configuration. Each section is broken down into smaller parts with explanations, making it easier to follow and comprehend.

Here's the explanation of the provided docker-compose.yml, formatted line by line:

```yaml
version: '3'

# Services: 
  # db
  # app
  # psql-command

services:
  # Configuration for the 'db' service
  db:
    image: postgres:latest  # Use the latest version of the Postgres image from Docker Hub
    restart: always  # Restart the container automatically if it exits
    env_file:  # Load environment variables from a file named .env
      - .env
    volumes:  # Mount the local ./data directory to /var/lib/postgresql/data in the container
      - ./data:/var/lib/postgresql/data
    networks:  # Connect the container to the 'bridge' network
      - bridge

  # Configuration for the 'app' service
  app:
    build:  # Build the Docker image using the Dockerfile in the current directory
      context: .
      dockerfile: dockerfile
    restart: always  # Restart the container automatically if it exits
    ports:  # Map port 3000 on the host to port 3000 on the container
      - "3000:3000"
    depends_on:  # Wait for the 'db' service to be ready before starting this service
      - db
    env_file:  # Load environment variables from a file named .env
      - .env
    networks:  # Connect the container to the 'bridge' network
      - bridge

  # Configuration for the 'psql-command' service
  psql-command:
    image: postgres:latest  # Use the latest version of the Postgres image from Docker Hub
    restart: always  # Restart the container automatically if it exits
    depends_on:  # Wait for the 'db' service to be ready before starting this service
      - db
    networks:  # Connect the container to the 'bridge' network
      - bridge
      
networks:
  bridge:
    driver: bridge  # Use the 'bridge' network driver

Explanation:

  • The version: '3' specifies the Docker Compose file format version being used.
  • Under services:, configuration for each service is defined.
  • For the db service:
    • image: postgres:latest specifies to use the latest version of the Postgres image from Docker Hub.
    • restart: always ensures that the container restarts automatically if it exits.
    • env_file: loads environment variables from a file named .env.
    • volumes: mounts the local ./data directory to /var/lib/postgresql/data in the container.
    • networks: connects the container to the bridge network.
  • For the app service:
    • build: specifies to build the Docker image using the Dockerfile in the current directory.
    • restart: always ensures that the container restarts automatically if it exits.
    • ports: maps port 3000 on the host to port 3000 on the container.
    • depends_on: specifies to wait for the db service to be ready before starting this service.
    • env_file: loads environment variables from a file named .env.
    • networks: connects the container to the bridge network.
  • For the psql-command service:
    • image: postgres:latest specifies to use the latest version of the Postgres image from Docker Hub.
    • restart: always ensures that the container restarts automatically if it exits.
    • depends_on: specifies to wait for the db service to be ready before starting this service.
    • networks: connects the container to the bridge network.
  • Under networks:, the bridge network is defined with the bridge network driver.

21bcp275.github.io's People

Contributors

priyank-vaidya avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.