<?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: ghassenyaa</title>
    <description>The latest articles on DEV Community by ghassenyaa (@ghassenyaa).</description>
    <link>https://dev.to/ghassenyaa</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%2F2220575%2Fc46eaa6a-f278-4510-9a43-1e4aca5c8405.png</url>
      <title>DEV Community: ghassenyaa</title>
      <link>https://dev.to/ghassenyaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ghassenyaa"/>
    <language>en</language>
    <item>
      <title>A Beginner's Guide to Dockerizing a Full-Stack Node.js and React Application with MongoDB Using Docker Compose</title>
      <dc:creator>ghassenyaa</dc:creator>
      <pubDate>Wed, 16 Oct 2024 16:17:29 +0000</pubDate>
      <link>https://dev.to/ghassenyaa/a-beginners-guide-to-dockerizing-a-full-stack-nodejs-and-react-application-with-mongodb-using-docker-compose-4fmh</link>
      <guid>https://dev.to/ghassenyaa/a-beginners-guide-to-dockerizing-a-full-stack-nodejs-and-react-application-with-mongodb-using-docker-compose-4fmh</guid>
      <description>&lt;p&gt;Docker is a powerful tool for containerizing applications, enabling developers to package their code and dependencies into a portable, lightweight unit. This guide is designed for beginners who want to containerize a simple Node.js backend, a React frontend, and MongoDB using Docker, along with Docker Compose to orchestrate all containers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we begin, ensure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Docker Compose&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 1: Setting Up Your Project Structure
&lt;/h2&gt;

&lt;p&gt;Let’s assume you have the following directory structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/my-app
│
├── backend (Node.js app)
│   ├── package.json
│   └── server.js
├── frontend (React app)
│   ├── package.json
│   └── src
├── docker-compose.yml
└── README.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will create a Dockerfile for both the Node.js and React applications, and then configure a docker-compose.yml file to manage the services, including MongoDB.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Dockerizing the Node.js App (Backend)
&lt;/h2&gt;

&lt;p&gt;In the /backend directory, create a file named Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official Node.js image
FROM node:18

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose port 5000 (or whichever port your Node.js app uses)
EXPOSE 5000

# Command to run your application
CMD ["node", "server.js"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Dockerfile defines:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A base Node.js image.&lt;/li&gt;
&lt;li&gt;Instructions to copy your Node.js app code and install dependencies.&lt;/li&gt;
&lt;li&gt;A command to start the app.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3: Dockerizing the React App (Frontend)
&lt;/h2&gt;

&lt;p&gt;In the /frontend directory, create another Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the official Node.js image
FROM node:18

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the React application for production
RUN npm run build

# Install an HTTP server to serve the static files
RUN npm install -g serve

# Expose port 3000
EXPOSE 3000

# Serve the React application
CMD ["serve", "-s", "build"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This Dockerfile does the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Uses the official Node.js image.&lt;/li&gt;
&lt;li&gt;Installs the app dependencies.&lt;/li&gt;
&lt;li&gt;Builds the React app and serves it on port 3000 using the serve package.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4: Setting Up MongoDB
&lt;/h2&gt;

&lt;p&gt;MongoDB is easy to run inside a Docker container. We don't need to create a separate Dockerfile for it. Instead, we will pull an official MongoDB image when configuring docker-compose.yml.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Creating the docker-compose.yml File
&lt;/h2&gt;

&lt;p&gt;The docker-compose.yml file orchestrates all the containers: backend, frontend, and MongoDB. Create this file at the root of your project (/my-app).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3'
services:
  backend:
    build: ./backend
    ports:
      - '5000:5000'
    volumes:
      - ./backend:/usr/src/app
    depends_on:
      - mongo
    environment:
      MONGO_URI: mongodb://mongo:27017/mydb
  frontend:
    build: ./frontend
    ports:
      - '3000:3000'
    volumes:
      - ./frontend:/usr/src/app
    depends_on:
      - backend
  mongo:
    image: mongo:5
    ports:
      - '27017:27017'
    volumes:
      - mongo-data:/data/db
volumes:
  mongo-data:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: Running the Application
&lt;/h2&gt;

&lt;p&gt;Once everything is set up, navigate to your project directory and run the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Build the Node.js and React Docker images.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pull the MongoDB image from Docker Hub.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start all services (backend, frontend, and MongoDB) together.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Accessing the Application:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The Node.js backend should be accessible at &lt;a href="http://localhost:5000" rel="noopener noreferrer"&gt;http://localhost:5000&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The React frontend should be accessible at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 7: Testing the Application
&lt;/h2&gt;

&lt;p&gt;You can now test your Node.js backend and React frontend together. For instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your backend could expose a /api route that stores and retrieves data from MongoDB.&lt;/li&gt;
&lt;li&gt;Your frontend could send requests to the backend API (e.g., using Axios or Fetch).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure the backend's API base URL is configured correctly in the React app (e.g., using environment variables).&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Stopping and Cleaning Up Containers
&lt;/h2&gt;

&lt;p&gt;When you're done testing or developing, you can stop and remove all the containers and associated resources with the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you wish to remove the persisted MongoDB data volume as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose down -v

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;You have now successfully Dockerized a Node.js backend, React frontend, and MongoDB using Docker Compose! This setup allows you to run all services in separate containers, ensuring a consistent and portable environment across different machines.&lt;/p&gt;

&lt;p&gt;With Docker, you can easily share your entire application stack, including its dependencies, with your team or deploy it in any Docker-supported environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resources
&lt;/h2&gt;

&lt;p&gt;To further enhance your Docker knowledge, you can explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/guides/" rel="noopener noreferrer"&gt;Official Docker Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/compose/" rel="noopener noreferrer"&gt;Docker Compose Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good luck with your Docker journey!&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>database</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
