Let's Dockerize

Let's Dockerize

What is Docker ?

Docker helps developers build, share, run, and verify applications anywhere — without tedious environment configuration or management.

It Look's way bookish right i mean docish uff simply these kind off documentational languages are very difficult for us beginners to understand lets understand what docker is by knowing what problems it solved

I was working on a MERN stack project it took me couple of weeks to finish it for someone who is totally new in to coding will definetly take his time but finishing the project and completing a task is much greater success

Lets say you created a project and that specifically runs only for you??

I tried sending the git to my friend i said man please help me in deploying this project he said bro your project isnt even ready yet it says lots of bugs and mongodb cluster and ip network issues i was like wait man i worked on this for week have a look into this video it works just fine for me

So we got into confusion why isnt this working we definelty know we need to have a standard versions,dependencies,mongodb connectiveties, jwts,bcrypt and other npm packages installed too

THIS IS WHERE DOCKER COMES IN

Docker emerged as a beacon of hope, offering a solution to the longstanding problem of inconsistent environments. By encapsulating my application and dependencies in a portable, self-contained environment, Docker provided the consistency and reliability I craved. With Docker, I could build once and run anywhere, freeing myself from endless troubleshooting sessions and compatibility issues. Armed with this newfound knowledge, I Dockerized my project, empowering myself to conquer any environment. Watching my once-troubled project deploy seamlessly, I realized Docker had transformed not just my code, but my entire approach to development. With Docker by my side, I embarked on new projects unencumbered by compatibility concerns, confident in its ability to guide me towards success.

Docker Architecture

docker run :-Executes a Docker image, creating a container and running the associated application within it.

docker build :-Constructs a Docker image from a Dockerfile, incorporating all necessary dependencies and configurations.

docker pull :-Retrieves a Docker image from a registry, downloading it locally for later use.

Let's Dockerize a MERN project

Forget those docker icons files Lets write them now

Go to Backend dir

create Dockerfile

FROM node

WORKDIR /app

# Copy package.json and package-lock.json separately to leverage Docker layer caching
COPY package.json package-lock.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose port 4000 (assuming your Node.js application listens on this port)
EXPOSE 4000

# Command to run the application
CMD ["npm", "start"]

create .dockerignore

node_modules
Dockerfile
.git;

Go to Frontend dir

create Dockerfile

FROM node:latest

WORKDIR /app

COPY . .

RUN npm install

EXPOSE 3000

CMD ["npm", "start"]

create .dockerignore

node_modules
Dockerfile
.git;

now finally create a docker-compose.yaml

version: '3.8'

services:
  mongodb:
    image: mongo
    volumes:
      - data:/data/db

  backend:
    build: ./backend
    ports:
      - "4000:4000"
    volumes:
      - logs:/app/logs
      - ./backend:/app
      - /app/node_modules
    depends_on:
      - mongodb

  frontend:
    build: ./frontend
    ports:
      - "3000:3000"
    volumes:
      - ./frontend/src:/app/src
    stdin_open: true
    tty: true
    depends_on:
      - backend

volumes:
  data:
  logs:

docker-compose up: Command used to start and run all services defined in a Docker Compose configuration file, creating and linking containers as specified.

Now run the frontend backend and the app u created via docker-dekstop enter the url congrats u r project is available in localhost:/${PORT}

u r mentioned port number

Did you find this article valuable?

Support Thirumalai by becoming a sponsor. Any amount is appreciated!