{"id":28874,"date":"2024-04-30T11:04:22","date_gmt":"2024-04-30T10:04:22","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/desarrollando-y-contenedorizando-microservicios-con-node-js-y-docker\/"},"modified":"2024-06-03T18:43:47","modified_gmt":"2024-06-03T17:43:47","slug":"desarrollando-y-contenedorizando-microservicios-con-node-js-y-docker","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/","title":{"rendered":"Developing and Containerizing Microservices with Node.js and Docker"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Introduccion_a_Microservicios_con_Nodejs_y_Docker\" >Introduction to Microservices with Node.js and Docker<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#%C2%BFPor_que_usar_Nodejs_y_Docker_para_Microservicios\" >Why use Node.js and Docker for Microservices?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Configuracion_del_Entorno_de_Desarrollo\" >Development Environment Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Creacion_de_un_Microservicio_Basico_con_Nodejs\" >Creating a Basic Microservice with Node.js<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_1_Inicializar_un_Nuevo_Proyecto_en_Nodejs\" >Step 1: Initialize a New Project in Node.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_2_Instalar_Expressjs\" >Step 2: Install Express.js<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_3_Crear_un_Servidor_HTTP_Basico\" >Step 3: Create a Basic HTTP Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_4_Probar_el_Microservicio\" >Step 4: Test the Microservice<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Contenedorizando_el_Microservicio_con_Docker\" >Containerizing the Microservice with Docker<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_1_Crear_un_Archivo_Dockerfile\" >Step 1: Create a Dockerfile<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_2_Construir_la_Imagen_de_Docker\" >Step 2: Build the Docker Image<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Paso_3_Ejecutar_el_Contenedor\" >Step 3: Run the Container<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/blog\/developing-and-containerizing-microservices-with-node-js-and-docker\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Introduccion_a_Microservicios_con_Nodejs_y_Docker\"><\/span>Introduction to Microservices with Node.js and Docker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Microservices have revolutionized the way developers build and deploy applications, enabling a more modular and scalable architecture. Thanks to technologies such as Node.js and Docker, implementing this type of architecture is more accessible and efficient. Node.js offers a lightweight, efficiency-focused runtime environment for JavaScript, while Docker enables applications to be encapsulated and deployed in lightweight, portable containers, ensuring consistency across different development and production environments.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFPor_que_usar_Nodejs_y_Docker_para_Microservicios\"><\/span>Why use Node.js and Docker for Microservices?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Node.js is ideal for developing microservices due to its asynchronous and non-blocking nature, making it especially suitable for handling I\/O (input and output) operations that are common in web services. Additionally, the Node.js ecosystem, with tools like npm, makes it easy to manage dependencies and package distribution.<\/p>\n<p>Docker, on the other hand, provides a standard approach to packaging applications with all their dependencies in a container. This simplifies configuration and deployment, and avoids the &quot;It works on my machine&quot; problem. Additionally, Docker integrates well with various container orchestration platforms, such as Kubernetes, which is crucial for handling large-scale microservices.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configuracion_del_Entorno_de_Desarrollo\"><\/span>Development Environment Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before you begin, make sure you have Node.js and Docker installed on your machine. You can download Node.js from <a href=\"https:\/\/nodejs.org\/\" rel=\"nofollow noopener\" target=\"_blank\">its official page<\/a> and Docker from <a href=\"https:\/\/hub.docker.com\/\" rel=\"nofollow noopener\" target=\"_blank\">DockerHub<\/a>. Once installed, you can verify that they are configured correctly by running <code>node -v<\/code> y <code>docker -v<\/code> in the terminal, which will show the installed versions of both.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Creacion_de_un_Microservicio_Basico_con_Nodejs\"><\/span>Creating a Basic Microservice with Node.js<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Paso_1_Inicializar_un_Nuevo_Proyecto_en_Nodejs\"><\/span>Step 1: Initialize a New Project in Node.js<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Create a new directory for your project and navigate to it in your terminal. Initialize a new Node.js project:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">mkdir my-microservice cd my-microservice npm init -y<\/code><\/pre>\n<p>This command will create a new file <code>package.json<\/code> which will manage the dependencies of your project.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Paso_2_Instalar_Expressjs\"><\/span>Step 2: Install Express.js<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Express.js is a minimalist and flexible framework for Node.js that allows you to easily handle HTTP requests. Install it with npm:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">npm install express<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_3_Crear_un_Servidor_HTTP_Basico\"><\/span>Step 3: Create a Basic HTTP Server<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Create a file called <code>server.js<\/code> and add the following code to define a basic HTTP server listening on port 3000:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">const express = require(&#039;express&#039;); const app = express(); app.get(&#039;\/&#039;, (req, res) =&gt; { res.send(&#039;Hello World!&#039;); }); app.listen(3000, () =&gt; { console.log(&#039;Microservice running on http:\/\/localhost:3000&#039;); });<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_4_Probar_el_Microservicio\"><\/span>Step 4: Test the Microservice<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Run your server with Node.js:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">node server.js<\/code><\/pre>\n<p>Navigate to <code>http:\/\/localhost:3000<\/code> in your browser and you should see the message &quot;Hello World!&quot;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Contenedorizando_el_Microservicio_con_Docker\"><\/span>Containerizing the Microservice with Docker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Paso_1_Crear_un_Archivo_Dockerfile\"><\/span>Step 1: Create a Dockerfile<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the root of your project, create a file called <code>Dockerfile<\/code>. This file describes the instructions for building the Docker image for your application. Add the following content to the Dockerfile:<\/p>\n<pre><code class=\"&quot;language-dockerfile&quot;\"># Set the base image FROM node:14 # Working directory in the WORKDIR \/app container # Copy the project files to the COPY container. . # Install project dependencies RUN npm install # Expose the port on which the EXPOSE 3000 application is running # Command to run the CMD application [&quot;node&quot;, &quot;server.js&quot;]<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_2_Construir_la_Imagen_de_Docker\"><\/span>Step 2: Build the Docker Image<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>From the terminal, in your project folder, run the following command to build the Docker image:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">docker build -t my-microservice .<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Paso_3_Ejecutar_el_Contenedor\"><\/span>Step 3: Run the Container<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Once the image is built, start the container:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">docker run -p 3000:3000 my-microservice<\/code><\/pre>\n<p>Now, your microservice should be running inside a Docker container, and will be accessible again in <code>http:\/\/localhost:3000<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The combination of Node.js with Docker offers a robust and efficient solution for the development and deployment of microservices. Node.js provides the runtime environment and libraries, while Docker encapsulates the entire service environment, ensuring it runs consistently on any system.<\/p>\n<p>For more details on Node.js, Docker or detailed development guides, visit <a href=\"https:\/\/nelkodev.com\/en\/\">nelkodev.com<\/a> or if you have questions, you can contact me directly at <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">https:\/\/nelkodev.com\/contacto<\/a>. I hope this resource is helpful for your microservices projects!<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction to Microservices with Node.js and Docker Microservices have revolutionized the way developers build and deploy applications, allowing for a more modular and scalable architecture. Thanks to technologies like Node.js and Docker, implementing this type of architecture is more accessible and efficient. Node.js offers a lightweight, microservice-centric runtime that is easy to deploy and deploy.<\/p>","protected":false},"author":1,"featured_media":28875,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2207],"tags":[1255,205,90,1991,889,1963,1961,1193,2208,213],"class_list":["post-28874","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-nodejs","tag-arquitectura","tag-blog","tag-con","tag-contenedorizando","tag-desarrollando","tag-docker","tag-microservicios","tag-node-js","tag-nodejs","tag-software"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28874"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28874\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28875"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}