<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Binumon Jacob on Medium]]></title>
        <description><![CDATA[Stories by Binumon Jacob on Medium]]></description>
        <link>https://medium.com/@binumonjacob?source=rss-15b3ebe695b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zA7KyXZ9tAEZtTyGR9mqKQ.png</url>
            <title>Stories by Binumon Jacob on Medium</title>
            <link>https://medium.com/@binumonjacob?source=rss-15b3ebe695b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:49:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@binumonjacob/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Debugging Node.js Application in Docker with Hot Reloading]]></title>
            <link>https://medium.com/@binumonjacob/debugging-node-js-in-docker-with-hot-reloading-a1b6adc047c4?source=rss-15b3ebe695b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1b6adc047c4</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[node-js-tutorial]]></category>
            <category><![CDATA[debug]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[Binumon Jacob]]></dc:creator>
            <pubDate>Sat, 13 May 2023 18:23:53 GMT</pubDate>
            <atom:updated>2023-05-18T04:21:11.689Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cBryZbZ5-sJdV2YluMtByA.png" /></figure><p>In the previous article <a href="https://medium.com/@binumonjacob/how-to-containerize-a-node-js-application-using-docker-bd9d89e24442">How to Containerize a Node.js Application using Docker</a>, we have covered the basic steps required to set up a Dockerfile and containerize a Node.js application. However, debugging is an essential aspect of application development that cannot be overlooked. Debugging helps developers identify and fix errors in their code, leading to more robust and stable applications.</p><p>Therefore, in this article, we will add a crucial element to our Docker setup: debugging. We will explore how to set up debugging for a Node.js application running inside a Docker container, and how to connect to it from an external IDE.</p><p>By the end of this article, you will have a fully functional Docker setup that includes debugging, enabling you to debug your Node.js application inside a container.</p><p>In this article, we will cover the following essential steps for enabling debugging in a Docker container running a Node.js application:</p><ol><li>Updating package.json with a debug script: We will add a debug script to our package.json file that enables the Node.js application to run in debug mode.</li><li>Creating a debug version of our docker-compose.yml file: We will create a debug version of our docker-compose.yml file that exposes the debugging port of our container to the host machine.</li><li>Attaching a debugger to the container: We will connect an external debugger to the container running our Node.js application, allowing us to debug the code effectively.</li></ol><p>By following these steps, we can set up a robust debugging environment that makes it easier to identify and fix issues in our Node.js application running inside a Docker container.</p><p>Before proceeding with this article, it is recommended that you read the <a href="https://medium.com/@binumonjacob/how-to-containerize-a-node-js-application-using-docker-bd9d89e24442">previous article</a> in this series or clone the <a href="https://github.com/Binumon31/dockerApp.git">example repository</a> available on GitHub. The previous article provides an introduction to debugging in Docker containers and explains the basic setup required to enable debugging.</p><h3>Creating the debug script</h3><p>To add a new script called start:debug, open up your package.json file and add the following line to the scripts section:</p><blockquote>“start:debug”: “nodemon — inspect=0.0.0.0 -L ./app.js”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*24ZGMn4QT0ePAU8k6-xCXA.png" /><figcaption>Figure1: package.json</figcaption></figure><p>Let’s go through each component of this command:</p><ul><li>nodemon: This is a utility that monitors changes in your source code and automatically restarts the Node.js application when changes are detected. To install nodemon, run the following command:</li></ul><blockquote>npm install nodemon</blockquote><ul><li>— inspect=0.0.0.0: This option enables the Node.js debugger and tells it to listen for incoming debugger connections on all network interfaces (0.0.0.0).</li><li>-L: This flag tells nodemon to follow symlinks when watching for changes in the application files. This is useful when using Docker because it allows the container to detect changes made to the application files on the host machine.</li><li>app.js: This is the entry point of our Node.js application.</li></ul><p>So, when we run the start: debug script, nodemon starts our Node.js application with the debugger enabled and listens for incoming connections on all network interfaces. This allows us to connect to the debugger from an external IDE and debug the application running inside a Docker container.</p><h3>Creating a debug Docker Compose file</h3><p>To enable debugging inside the container, we need to modify our Dockerfile and docker-compose.yml file to expose the necessary debugging ports and map them to the corresponding ports on our host machine.</p><p>Create the Docker file in the root directory with the following content:</p><pre>FROM node:14-alpine AS development<br>ENV NODE_ENV development<br># Add a work directory<br>WORKDIR /app<br># Cache and Install dependencies<br>COPY package.json .<br>RUN npm install nodemon<br>RUN npm install<br># Copy app files<br>COPY . .<br># Expose port<br>EXPOSE 3000<br># Start the application with nodemon in debug mode<br>CMD [&quot;npm&quot;, &quot;run&quot;, &quot;start:debug&quot;]</pre><p>Here&#39;s how to create a debug Docker Compose file:</p><pre>services:<br>  app:<br>    image: myapp<br>    build: <br>      context: .<br>      dockerfile: Dockerfile<br>    ports:<br>      - 3000:3000<br>      - 9229:9229<br>    restart: on-failure<br>    volumes:<br>      - .:/app</pre><p>First, I added a ports section to the app service, which maps the host port 9229 to container port 9229. This allows us to connect to the debugger running inside the container from our host machine.</p><p>Here the volumes section maps a host directory to a directory inside the container, creating a shared volume between the host and container.</p><p>In this specific case, we’re mapping the current directory (represented by .) on the host machine to the /app the directory inside the container. This means that any changes we make to the files in the current directory on the host machine will be reflected immediately inside the container.</p><p>This is particularly useful for hot reloading. With hot reloading, the Node.js application is automatically restarted when a file is changed, allowing developers to see the changes without manually restarting the application.</p><p>By mapping the host directory to the container directory using volumes, we can achieve hot reloading in our Dockerized Node.js application. When we make changes to the files on the host machine, Nodemon (which is watching for changes) will detect the changes and restart the Node.js application automatically inside the container. This allows us to see the changes in real time without having to manually restart the application.</p><p>Once you have the docker-compose.yml file defined, you can use the following command to start the containers:</p><blockquote><em>docker-compose .\docker-compose.yml up -d</em></blockquote><h3><strong>Setting up the Debugger in Visual Studio Code</strong></h3><p>I will be using Visual Studio Code as our debugger. From VS Code click cmd+shift+p (ctrl+shift+p) to open the command palette and select an option called Debug: Add Configuration.... From the list, select Node.js add the following content in launch.json :</p><pre>{<br>      &quot;address&quot;: &quot;localhost&quot;,<br>      &quot;localRoot&quot;: &quot;${workspaceFolder}&quot;,<br>      &quot;name&quot;: &quot;Docker: API&quot;,<br>      &quot;port&quot;: 9229,<br>      &quot;remoteRoot&quot;: &quot;/app&quot;,<br>      &quot;request&quot;: &quot;attach&quot;,<br>      &quot;type&quot;: &quot;node&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*r_Iqdt2tnh6G1bBr0U7O5Q.png" /><figcaption>Figure2: launch.json</figcaption></figure><p>This launch.json file specifies the configuration for our VS Code debugger. The type field specifies that we&#39;re using a Node.js debugger and the name field specifies a name for this configuration.</p><p>In this case, we’re attaching to a remote program running on the Docker container. The address field specifies the IP address of the remote program (in this case, localhost), and the port field specifies the port number on which the debugger is running (in this case, 9229).</p><p>The localRoot and remoteRoot fields specify the local and remote file paths respectively. In this case, we&#39;re specifying that the local root is the root of our project directory and the remote root is /app inside the container.</p><p>Once the configuration is set, you can start the debugger by clicking on the Debug icon on the left-hand side menu in VS Code and selecting “Docker: API” from the dropdown. If everything is configured correctly, VSC should connect to the remote process and show an orange bar at the bottom of the window.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/435/1*_vzRNGy14YjHNQNmxE7Mtg.png" /><figcaption>FIgure3: VS Code Run and Debug</figcaption></figure><p>To test the debugger, you can set breakpoints in your code by clicking on the left-hand gutter next to the line number in app.js. When you make a request to your server, the debugger should pause at the breakpoint and allow you to inspect variables and step through the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*Z0ZCT2TKMWOieCTlGHrJvQ.png" /><figcaption>Figure4: VS code</figcaption></figure><p>After completing the steps mentioned above, you can now test if the debugger is working by loading up <a href="http://localhost:3000/">http://localhost:3000/</a> in your web browser. If everything is set up correctly, the debugger should pause at the breakpoints you set in the request handlers of app.js. This means that you have successfully attached a debugger to a Docker container. Congratulations on achieving this milestone!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kJZzSZ6ApXID36h9D5tJNg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1b6adc047c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Containerize a Node.js Application using Docker]]></title>
            <link>https://medium.com/@binumonjacob/how-to-containerize-a-node-js-application-using-docker-bd9d89e24442?source=rss-15b3ebe695b------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd9d89e24442</guid>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[node-js-tutorial]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Binumon Jacob]]></dc:creator>
            <pubDate>Wed, 10 May 2023 10:03:40 GMT</pubDate>
            <atom:updated>2023-05-13T04:28:54.202Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*355iwD22FAzxT93atygGzA.png" /></figure><p>Docker has indeed transformed the software development and deployment process, enabling developers to easily package and distribute their applications. With Docker, it has become much easier to build, test, and deploy applications across different environments, making it a go-to tool for many emerging startups and enterprises.</p><p>In this tutorial, I will show you how to containerize a Node.js application using Docker, which will allow you to package your application and its dependencies into a single container that can be easily distributed and run on any machine with Docker installed. I will walk you through the process of creating a simple Node js application, and then show you how to package it in a Docker container.</p><p>By the end of this tutorial, you will have a good understanding of how Docker works and how you can use it to simplify your application deployment process. Let’s get started!</p><h3>Pre-requisites:</h3><ul><li>Download and install Docker from the <a href="https://docs.docker.com/get-docker/">official website</a>.</li><li>Install Node.js from the official website <a href="https://nodejs.org/en/">here</a>.</li></ul><h3>Creating a Node.js Application</h3><p>Let’s start by creating a simple Node.js application that we will later containerize using Docker.</p><p>First, create a directory to store your application. You can name it anything you like. For this tutorial, we will name it ‘myapp’.</p><blockquote>mkdir myapp</blockquote><p>Navigate into the directory by running:</p><blockquote>cd myapp</blockquote><p>Next, initialize your application by running the following command:</p><blockquote>npm init -y</blockquote><p>This command creates a package.json file in your directory, which contains metadata about your application, including its name, version, and dependencies. The -y flag is used to accept all the default settings.</p><pre>{<br>  &quot;name&quot;: &quot;myapp&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;app.js&quot;,<br>  &quot;type&quot;:&quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;keywords&quot;: [],<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;<br>}</pre><p>Now, let’s add the Express framework as a dependency to our application. We will do this by running the following command in the project’s root directory:</p><blockquote>npm install express — save</blockquote><p>This command installs the latest version of Express and adds it as a dependency to our application. Once the installation is complete, you can check the package.json file to see that Express has been added to the list of dependencies.</p><pre>{<br>  &quot;name&quot;: &quot;test&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;,<br>  &quot;description&quot;: &quot;&quot;,<br>  &quot;main&quot;: &quot;app.js&quot;,<br>  &quot;type&quot;:&quot;module&quot;,<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;<br>  },<br>  &quot;keywords&quot;: [],<br>  &quot;author&quot;: &quot;&quot;,<br>  &quot;license&quot;: &quot;ISC&quot;,<br>  &quot;dependencies&quot;: {<br>    &quot;express&quot;: &quot;^4.18.2&quot;<br>  }<br>}</pre><p>Let’s create a file named app.js and add the following code:</p><pre>import express from &#39;express&#39;;<br><br>const app = express();<br>const PORT = process.env.PORT || 3000;<br><br>app.get(&#39;/&#39;, (request, response) =&gt; {<br>  response.status(200).json({<br>    message: &#39;Hello Docker!&#39;,<br>  });<br>});<br><br>app.listen(PORT, () =&gt; {<br>  console.log(`Server is up on http://localhost:${PORT}`);<br>});</pre><p>In the above code, we first import the express module and create an instance of the app by calling the express() function. We then set the port number to 3000 or to the value of the PORT environment variable, if it is set.</p><p>We define a single route for the root URL (/) using the get() method of the app object. When a GET request is made to the root URL, the app object responds with a JSON object containing the message &quot;Hello Docker!&quot; and sets the HTTP status code to 200 (OK).</p><p>Finally, we start the server by calling the listen() method of the app object and passing it the PORT value and a callback function that logs a message to the console indicating that the server is up and running.</p><p>Our simple application is now ready to be tested. We can run it by executing the command node app.js in the terminal. If everything is working correctly, you should see the message &quot;Server is up on localhost:3000&quot; printed to the console. You can then access the application by navigating to http://localhost:3000/ in your web browser and should see the message &quot;Hello Docker!&quot; displayed on the page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*jmQonLM_A_Id1axjJIUmRw.png" /><figcaption>Figure 1: Browser Image</figcaption></figure><h3><strong>Setting up Docker</strong></h3><p>To create a Dockerfile for our application, we need to go to the root directory of the application and create a file named Dockerfile. Here is the code that we need to add to the Dockerfile:</p><pre>FROM node:14-alpine AS development<br>ENV NODE_ENV development<br># Add a work directory<br>WORKDIR /app<br># Cache and Install dependencies<br>COPY package.json .<br>RUN npm install<br># Copy app files<br>COPY . .<br># Expose port<br>EXPOSE 3000<br># Start the app<br>CMD [ &quot;node&quot;, &quot;app.js&quot; ]</pre><p>This Dockerfile will create a Docker image for our Node.js application. Let’s go through each of the commands used in the Dockerfile:</p><ul><li>FROM node:14-alpine AS development: This command specifies the base image for our Docker image. We are using the official Node.js 14-alpine image, which is a lightweight image based on Alpine Linux.</li><li>ENV NODE_ENV development: This command sets an environment variable named NODE_ENV to development. This is not strictly necessary, but it can be useful for setting environment-specific configuration options.</li><li>WORKDIR /app: This command sets the working directory inside the container to /app. This is where we will store our application code and dependencies.</li><li>COPY package.json .: This command copies the package.json file from our host machine to the /app directory inside the container.</li><li>RUN npm install: This command installs the dependencies specified in the package.json file inside the container.</li><li>COPY . .: This command copies all the files from our host machine to the /app directory inside the container.</li><li>EXPOSE 3000: This command exposes port 3000 for our application to run on.</li><li>CMD [ &quot;node&quot;, &quot;app.js&quot; ]: This command specifies how our application will start. In this case, we are running the node command app.js as the argument.</li></ul><p>To prevent your local modules from being copied onto your Docker image, you can create a .dockerignore file in your project’s root directory. Add the following line to the file:</p><ul><li>node_modules</li></ul><p>Next, we need to create the docker-compose</p><p>The docker-compose.yml will enable us to run containers to build our app. So let&#39;s make a docker-compose.yml file.</p><p>/docker-compose.yml</p><pre>services:<br>  app:<br>    image: myapp<br>    build: <br>      context: .<br>      dockerfile: Dockerfile<br>    ports:<br>      - &quot;3000:3000&quot;<br>    restart: on-failure<br>    volumes:<br>      - .:/app</pre><p>Let’s go through each of the commands used in the docker-compose file:</p><ul><li>build: This specifies how to build the Docker image for the service. In this case, it&#39;s using the current directory as the build context (.) and the Dockerfile as the Dockerfile to use for building the image.</li><li>image: This specifies the name of the image that will be created for this service.</li><li>restart: This specifies the restart policy for the container. In this case, it will restart the container if it fails.</li><li>ports: This specifies the port mapping between the container and the host machine. In this case, it maps port 3000 on the container to port 3000 on the host.</li><li>volumes: This specifies the volume mapping between the container and the host machine. In this case, it maps the current directory (.) on the host to the /app directory on the container, allowing changes to the code to be reflected in the container without having to rebuild the image.</li></ul><p>Once you have the docker-compose.yml file defined, you can use the following command to start the containers:</p><blockquote>docker-compose .\docker-compose.yml up -d</blockquote><p>You can verify the new image pushed to your Docker hub and confirm that your app container is running by going to your Docker hub dashboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zh5PrWAnnENXxGNvLKPLwQ.png" /><figcaption>Figure2: Docker Desktop Image dashboard</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ouy4YmL3X_bebaDuFDhZWw.png" /><figcaption>Figure3: Docker Desktop Container dashboard</figcaption></figure><p>You should see the application running on the specified port with the following output inside the container:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aJnYDPGCQYftYD1NEAL4Ow.png" /><figcaption>Figure4: Logs inside docker container</figcaption></figure><p>Check your application by going to your <a href="http://localhost:3002/">http://localhost:3000/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*jmQonLM_A_Id1axjJIUmRw.png" /><figcaption>Figure 5: Browser Image</figcaption></figure><p>To conclude, containerizing a Node.js application using Docker is a great way to simplify the deployment process and ensure consistency across different environments. With Docker, you can package your application and all its dependencies into a container image that can be easily deployed to any machine that supports Docker.</p><p>The process involves creating a Dockerfile that defines the image and its dependencies, building the image, and pushing it to a container registry such as Docker Hub. You can then use Docker Compose to orchestrate multiple containers for your application.</p><p>By containerizing your Node.js application, you gain several benefits such as reproducibility, portability, and scalability. You can easily replicate your development environment on any machine and ensure that your application runs the same way across different environments. Additionally, Docker’s scalability features enable you to easily scale your application up or down based on demand.</p><p>Overall, containerization is an essential tool for modern application development and deployment, and Docker is one of the most popular containerization technologies available today. By following the steps outlined in this article, you can easily containerize your Node.js application and take advantage of all the benefits that containerization has to offer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd9d89e24442" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>