Real time project based on docker with explanation with example in simple words

 


A real-time project using Docker could involve setting up a microservices-based application. Let’s break it down with a simple example of a multi-tier web application (like an e-commerce app). Here's how Docker plays a role:


Project Overview:


The application is composed of three main components:


1. Frontend (Web UI)



2. Backend (API)



3. Database (Data storage)




Each of these components will run in its own container.


Docker in Action:


1. Frontend (UI): The frontend could be a simple website built using React.js. It will serve the user interface where users can interact with the app.


Docker Role: We’ll package the React app in a Docker container, so it can be deployed easily without worrying about the local machine’s configuration.


Dockerfile example for frontend:


# Start with a base image that includes Node.js

FROM node:14


# Set the working directory inside the container

WORKDIR /app


# Copy the app files into the container

COPY . .


# Install dependencies

RUN npm install


# Start the application

CMD ["npm", "start"]



2. Backend (API): The backend could be built using Node.js and Express.js. It will serve the API that the frontend will call to get or send data.


Docker Role: We’ll put the backend code into a Docker container to isolate the environment and ensure that it works across all systems.


Dockerfile example for backend:


# Use an official Node.js image

FROM node:14


# Set the working directory inside the container

WORKDIR /backend


# Copy the backend code

COPY . .


# Install dependencies

RUN npm install


# Expose port for the API

EXPOSE 3000


# Start the server

CMD ["node", "server.js"]



3. Database: Let’s say the database is MySQL. Docker provides an official MySQL image that we can use to set up the database without installing it on our machine.


Docker Role: The database will also run in a container, and it will be easy to manage and scale.


Docker command to run MySQL:


docker run --name my-database -e MYSQL_ROOT_PASSWORD=rootpassword -e MYSQL_DATABASE=mydb -p 3306:3306 -d mysql:latest




Using Docker Compose:


We can use Docker Compose to orchestrate the containers, so they can interact with each other. Docker Compose is a YAML file that defines how to run multiple containers at once.


Example docker-compose.yml file:


version: '3'

services:

  frontend:

    build: ./frontend

    ports:

      - "3000:3000"

  

  backend:

    build: ./backend

    ports:

      - "5000:5000"

    depends_on:

      - db


  db:

    image: mysql:latest

    environment:

      MYSQL_ROOT_PASSWORD: rootpassword

      MYSQL_DATABASE: mydb

    ports:

      - "3306:3306"


How it Works:


The frontend container will run the React app and expose it on port 3000.


The backend container will run the API on port 5000 and connect to the database.


The database container will run MySQL on port 3306.



Real-Time Benefits of Docker:


1. Portability: You can easily move the app from your local machine to a cloud environment with the same Docker images.



2. Consistency: Every developer has the same environment, reducing the "works on my machine" issue.



3. Isolation: Each component (frontend, backend, database) runs in its own container, avoiding conflicts.



4. Scaling: If traffic increases, you can scale individual services like the backend by creating more instances (containers).




Conclusion:


Docker simplifies the process of running, scaling, and managing applications by containerizing each component, ensuring they can be deployed consistently in any environment.


Comments

Popular posts from this blog

Python & Shell Scripting Real Time Course Book & Videos

Top Five Devops Technical Interview QA Books

Linux-Command Hands-On (DF)