<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ratan Gulati</title>
    <description>The latest articles on DEV Community by Ratan Gulati (@ratangulati).</description>
    <link>https://dev.to/ratangulati</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1604640%2F4fc40483-cb5a-43c9-84a0-4a0cf71ad353.jpeg</url>
      <title>DEV Community: Ratan Gulati</title>
      <link>https://dev.to/ratangulati</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ratangulati"/>
    <language>en</language>
    <item>
      <title>Seamlessly Containerize Your MERN Stack Application with Docker: A Step-by-Step Guide</title>
      <dc:creator>Ratan Gulati</dc:creator>
      <pubDate>Tue, 11 Jun 2024 03:26:21 +0000</pubDate>
      <link>https://dev.to/ratangulati/seamlessly-containerize-your-mern-stack-application-with-docker-a-step-by-step-guide-5h5c</link>
      <guid>https://dev.to/ratangulati/seamlessly-containerize-your-mern-stack-application-with-docker-a-step-by-step-guide-5h5c</guid>
      <description>&lt;p&gt;In today's software development, Docker has become an essential tool for deploying and managing applications. Through Docker, we ensure that our application runs consistently across different environments from development to production. The main benefit of Docker is that it helps run processes in isolated environments&lt;/p&gt;

&lt;p&gt;As the heading implies I will be showing how to containerize a MERN (MongoDB, Express.js, React, Node.js) stack application using Docker.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization.&lt;br&gt;
Containers are a way to package and distribute software applications in a way that makes them easy to deploy and run consistently across different environments. They allow you to package an application, along with all its dependencies and libraries, into a single unit that can be run on any machine with a container runtime, such as Docker.&lt;/p&gt;
&lt;h3&gt;
  
  
  But why do we even need containers?
&lt;/h3&gt;

&lt;p&gt;Everyone has different operating systems like Windows, Linux, or MacOS. Everyone has different environments in which they work, and the steps to run a project can vary based on the OS. When the project grows it also becomes extremely hard to keep track of dependencies used by the project.&lt;/p&gt;
&lt;h2&gt;
  
  
  Benefits of Using Containers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Docker allows us to configure our application in a single file.&lt;/li&gt;
&lt;li&gt;Docker can run in isolated environments.&lt;/li&gt;
&lt;li&gt;Setting up an OS project is easier.&lt;/li&gt;
&lt;li&gt;Installing DBs and auxiliary services is also easier through Docker.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d -p 27017:27017 mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;This command starts Mongo in all the operating systems.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  An example of what docker does
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37w6gum42djpayg115jl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37w6gum42djpayg115jl.png" alt="How docker works" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker isn't the only containerization tool, there are many &lt;a href="https://www.techrepublic.com/article/docker-alternatives/"&gt;alternatives&lt;/a&gt; depending on the usage.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is MERN?
&lt;/h2&gt;

&lt;p&gt;"MERN" refers to a stack of technologies used for building web applications. The acronym stands for &lt;code&gt;MongoDB&lt;/code&gt;, &lt;code&gt;Express.js&lt;/code&gt;, &lt;code&gt;React&lt;/code&gt;, and &lt;code&gt;Node.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MongoDB&lt;/strong&gt; - It is a  NoSQL database that stores data in JSON-like documents. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Express.js&lt;/strong&gt; - It is a web application framework for Node.js, designed for building web applications and APIs. It provides a robust set of features that simplifies the development process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; - Our most popular and widely used frontend framework. It allows developers to create large web applications that can update and render efficiently in response to data changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js&lt;/strong&gt; - You must have heard that the JavaScript runtime is built on Chrome's V8 JavaScript engine. It allows developers to use JavaScript for server-side scripting, running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.&lt;/p&gt;
&lt;h2&gt;
  
  
  Now let's get to the main part
&lt;/h2&gt;

&lt;p&gt;For creating a Dockerfile for a MERN app we must know the application's structure and how it runs locally. I am taking the MERN project &lt;a href="https://github.com/Ratangulati/CompileX"&gt;CompileX&lt;/a&gt; I created for the illustration.&lt;br&gt;
It is a real-time compiler built using &lt;code&gt;React&lt;/code&gt; for the &lt;code&gt;frontend&lt;/code&gt;, &lt;code&gt;Node.js&lt;/code&gt;, and &lt;code&gt;Express.js&lt;/code&gt; for the &lt;code&gt;backend&lt;/code&gt;. I have used &lt;code&gt;socket.io&lt;/code&gt; for &lt;code&gt;real-time communication&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CompileX/
├── src/. //Frontend
│   ├── assets/
│   ├── components/
│   ├── pages/
│   ├── constants/
│   ├── App.css
│   ├── App.jsx
│   ├── index.css
│   ├── main.jsx
│   └── socket.js
|
├── index.html
├── package-lock.json
├── package.json
├── server.js. //Backend
├── tailwind.config.js
├── vite.config.js
├── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, &lt;code&gt;src&lt;/code&gt; contains my &lt;code&gt;frontend&lt;/code&gt;, and &lt;code&gt;server.js&lt;/code&gt; contains my &lt;code&gt;backend&lt;/code&gt;. &lt;br&gt;
If I have to start my application locally to run (development) I would have to start my backend using &lt;strong&gt;node server.js&lt;/strong&gt; and frontend using &lt;strong&gt;npm run dev&lt;/strong&gt; (vite configuration). I will also have to install the dependencies required for both of them.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating Dockerfile
&lt;/h3&gt;

&lt;p&gt;Your Dockerfile contains the instructions for building a Docker image of your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use an official Node.js image, based on Alpine Linux, as the base image.
FROM node:18-alpine

# Set the working directory inside the container to /app.
WORKDIR /app

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

# Install the dependencies specified in package.json.
RUN npm install

# Copy all files from the current directory on the host machine to the working directory in the container.
COPY . .

# Build the application. This assumes there's a build script in package.json.
RUN npm run build

# Expose port 3000 on the container, so it can be accessed from the host.
EXPOSE 3000

# Define the command to run when the container starts. This runs the server in production mode.
CMD ["npm", "run", "server:prod"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go through the Dockerfile&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Base Image&lt;/strong&gt; - I have taken the &lt;strong&gt;node:18-alpine&lt;/strong&gt; image from the docker hub. It will download the node inside the container. &lt;code&gt;node:18-alpine&lt;/code&gt; is a lightweight Node.js image using Alpine Linux. You can find more about it on the docker hub and you can use the image required according to your project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WORKDIR&lt;/strong&gt; - Sets /app as the working directory inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;COPY package.json and package-lock.json&lt;/strong&gt; - Copies package.json and package-lock.json into the container. This helps in leveraging Docker's cache for dependencies. You can also use &lt;code&gt;package*.json ./&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RUN npm install&lt;/strong&gt; - Installs the Node.js dependencies inside the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;COPY . .&lt;/strong&gt; - It copies all files from the current directory on the host to the /app directory in the container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RUN npm run build&lt;/strong&gt; - This command builds our application. Typically used for building the front-end assets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;EXPOSE 3000&lt;/strong&gt; - it exposes port 3000, which is used by the application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CMD&lt;/strong&gt; - Specifies the command to run the application in production mode using npm run &lt;code&gt;server:prod&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Building &amp;amp; Running the Dockerfile
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t compile-x:v1 .
docker run -d -p 3000:3000 compile-x:v1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;build&lt;/strong&gt; - After creating Dockerfile we build the docker image for our application. Here I am creating a compile-x image and I have given it a tag v1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsx84s9tltamj9wksgej3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsx84s9tltamj9wksgej3.png" alt="docker build preview" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;run&lt;/strong&gt; - To run the image we use &lt;code&gt;docker run &amp;lt;image name&amp;gt;&lt;/code&gt;. I am running my docker image in detach mode and I have also given used port mapping. &lt;strong&gt;-d&lt;/strong&gt; for &lt;code&gt;detach mode&lt;/code&gt; &lt;strong&gt;-p&lt;/strong&gt; for &lt;code&gt;port mapping&lt;/code&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;To check the status of the container run &lt;code&gt;docker ps&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdynonulg5q4cwn3l97e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdynonulg5q4cwn3l97e.png" alt="docker image preview" width="746" height="45"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqgh8x52yn7ba3yn6ny9e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqgh8x52yn7ba3yn6ny9e.png" alt="docker run preview" width="800" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating docker-compose.yml
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Define the version of Docker Compose file format.
version: '3.8'

# Define the services (containers) to be run.
services:
  app:
    # Build the Docker image for the app service using the Dockerfile in the current directory.
    build:
      context: .
      dockerfile: Dockerfile
    # Map port 3000 of the container to port 3000 of the host machine.
    ports:
      - "3000:3000"
    # Mount the current directory (.) on the host machine to /app in the container.
    volumes:
      - .:/app
    # Set environment variables for the container.
    environment:
      - NODE_ENV=development 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Building &amp;amp; Running docker-compose.yml
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up --build
docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;build&lt;/strong&gt; - This command builds the Docker image and starts the container as defined in your docker-compose.yml file&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note&lt;/em&gt; - You only need to run it only one time&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpm5ti927vsm1h6wigdc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjpm5ti927vsm1h6wigdc.png" alt="docker-compose --build preview" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;run&lt;/strong&gt; - To run the docker-compose run &lt;code&gt;docker-compose up&lt;/code&gt; after building it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffs7busoo3ixivkptpco9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffs7busoo3ixivkptpco9.png" alt="Docker-compose run" width="727" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;access&lt;/strong&gt; - Once the containers are running, we can access our application at &lt;code&gt;http://localhost:3000&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By using Docker and Docker Compose, we have created an environment where our MERN stack application can run consistently across different systems. The &lt;strong&gt;Dockerfile&lt;/strong&gt; specifies how to build the image, while &lt;strong&gt;docker-compose.yml&lt;/strong&gt; defines how to configure and run the container. This setup is beneficial for both development and deployment, ensuring that your application behaves the same way regardless of where it's run.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>docker</category>
      <category>mern</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
