<?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: lizahakem</title>
    <description>The latest articles on DEV Community by lizahakem (@liza_____).</description>
    <link>https://dev.to/liza_____</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%2F1115259%2F310c0165-b3b3-42c1-81a8-f8f617c93e82.png</url>
      <title>DEV Community: lizahakem</title>
      <link>https://dev.to/liza_____</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/liza_____"/>
    <language>en</language>
    <item>
      <title>Step-by-step tutorial on how to dockerize a Node.js API with a MongoDB database</title>
      <dc:creator>lizahakem</dc:creator>
      <pubDate>Fri, 07 Jul 2023 13:22:33 +0000</pubDate>
      <link>https://dev.to/liza_____/step-by-step-tutorial-on-how-to-dockerize-a-nodejs-api-with-a-mongodb-database-27f0</link>
      <guid>https://dev.to/liza_____/step-by-step-tutorial-on-how-to-dockerize-a-nodejs-api-with-a-mongodb-database-27f0</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Step 1: Set up the Node.js API&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new directory for your Node.js API project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialize a new Node.js project using npm init and follow the prompts to set up the project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the required dependencies for your Node.js API using npm install. This may include libraries such as Express, MongoDB client, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write your Node.js API code, including the necessary routes, controllers, and MongoDB connection logic. Make sure your API is working correctly by testing it locally.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Step 2: Set up Docker&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install Docker on your machine by following the official Docker installation guide for your specific operating system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify that Docker is installed correctly by running docker --version in your terminal. It should display the Docker version information.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Step 3: Create a Dockerfile&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a new file in the root directory of your Node.js project called Dockerfile.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the Dockerfile and add the following content:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

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

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

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the API port
EXPOSE 3000

# Start the Node.js application
CMD [ "npm", "start" ]

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Save the Dockerfile.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Step 4: Build the Docker image&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open a terminal and navigate to the root directory of your Node.js project where the Dockerfile is located.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the following command to build the Docker image:&lt;br&gt;
&lt;code&gt;docker build -t node-api .&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
The -t flag specifies the name and optionally a tag for the Docker image.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Step 5: Set up a MongoDB container&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;In your terminal, run the following command to pull the MongoDB image from the Docker Hub:&lt;br&gt;
&lt;code&gt;docker pull mongo&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a new directory for the MongoDB data to persist outside the container, e.g., data/db.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a new MongoDB container using the following command:&lt;br&gt;
&lt;code&gt;docker run -d -p 27017:27017 -v /path/to/data/db:/data/db --name mongodb mongo&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The -d flag runs the container in the background, the -p flag maps the container port 27017 to the host port 27017, and the -v flag mounts the data directory to the container's /data/db directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Step 6: Run the Docker containers&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start the MongoDB container by running:
&lt;code&gt;docker start mongodb&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Start the Node.js API container by running:
&lt;code&gt;docker run -p 3000:3000 --name node-api --link mongodb:mongodb -d node-api&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
    </item>
  </channel>
</rss>
