{"id":28829,"date":"2024-04-11T13:07:34","date_gmt":"2024-04-11T12:07:34","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/desarrollo-y-despliegue-eficiente-de-microservicios-en-go-con-docker\/"},"modified":"2024-06-03T17:42:46","modified_gmt":"2024-06-03T16:42:46","slug":"desarrollo-y-despliegue-eficiente-de-microservicios-en-go-con-docker","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/","title":{"rendered":"Efficient Development and Deployment of Microservices in Go with Docker"},"content":{"rendered":"<p>The world of software development is constantly evolving, and with it, the technologies and methodologies we use. One of the approaches that has gained a lot of popularity in recent years is the microservices architecture, especially due to its great flexibility and efficiency for applications in production environments. Go, for its part, is a programming language that stands out for its simplicity and efficiency, especially when it comes to creating back-end applications and concurrent systems. But how can you take advantage of both to create robust and easily scalable applications?<\/p>\n<p>In this tutorial, we&#039;ll explore step by step how you can develop a microservice using Go and how to deploy it using Docker containers, two tremendously powerful tools when it comes to building and deploying applications efficiently and securely.<\/p>\n<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\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#%C2%BFPor_Que_Elegir_Go_para_Tu_Microservicio\" >Why Choose Go for Your Microservice?<\/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\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Desarrollando_Nuestro_Primer_Microservicio_en_Go\" >Developing Our First Microservice in Go<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Estructura_Inicial_del_Proyecto\" >Initial Project Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Escribiendo_el_Servidor_HTTP\" >Writing the HTTP Server<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Compilando_y_Probando_el_Microservicio\" >Compiling and Testing 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-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Contenerizando_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-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Creando_un_Dockerfile\" >Creating a Dockerfile<\/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\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Construyendo_y_Ejecutando_el_Contenedor\" >Building and Running 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-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/efficient-development-and-deployment-of-microservices-in-go-with-docker\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFPor_Que_Elegir_Go_para_Tu_Microservicio\"><\/span>Why Choose Go for Your Microservice?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Go, also known as Golang, is a programming language developed by Google that combines execution efficiency, concise coding, and support for native concurrency. This makes it ideal for microservices development because:<\/p>\n<ul>\n<li><strong>Efficiency and Performance:<\/strong> Compiled into machine code, Go is fast and uses very few resources compared to other high-level languages.<\/li>\n<li><strong>Concurrence:<\/strong> Go includes concurrency features such as goroutines and channels, which allow multiple processes to run simultaneously and efficiently.<\/li>\n<li><strong>Simplicity:<\/strong> Its syntax is clear and its static typing prevents many common errors at runtime.<\/li>\n<li><strong>Extensive Ecosystem:<\/strong> Go comes with an extensive standard library and ecosystem of tools that facilitate everything from dependency management to communication between services.<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Desarrollando_Nuestro_Primer_Microservicio_en_Go\"><\/span>Developing Our First Microservice in Go<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before we dive into the code, make sure you have Go and Docker installed on your system. You can find installation instructions on the official sites for each technology.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Estructura_Inicial_del_Proyecto\"><\/span>Initial Project Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Create a new directory for your project and within it a basic file structure:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">mkdir my-microservice cd my-microservice mkdir -p cmd\/api touch cmd\/api\/main.go<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Escribiendo_el_Servidor_HTTP\"><\/span>Writing the HTTP Server<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Edit the file <code>cmd\/api\/main.go<\/code> To configure a basic HTTP server:<\/p>\n<pre><code class=\"&quot;language-go&quot;\">package main import ( &quot;net\/http&quot; ) func main() { http.HandleFunc(&quot;\/&quot;, func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(&quot;Hello, Microservice !&quot;)) }) http.ListenAndServe(&quot;:8080&quot;, nil) }<\/code><\/pre>\n<p>This code sets up a server that listens on port 8080 and responds a simple message to any request.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Compilando_y_Probando_el_Microservicio\"><\/span>Compiling and Testing the Microservice<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To test your microservice locally, navigate to the project folder and run:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">go run .\/cmd\/api<\/code><\/pre>\n<p>Open your browser and visit <code>http:\/\/localhost:8080<\/code>. You should see the message &quot;Hello, Microservice!&quot;.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Contenerizando_el_Microservicio_con_Docker\"><\/span>Containerizing the Microservice with Docker<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Containerization helps us ensure that our microservice runs the same way in any deployment environment. Docker is one of the most popular tools for this task.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Creando_un_Dockerfile\"><\/span>Creating a Dockerfile<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the root of the project, create a file called <code>Dockerfile<\/code>:<\/p>\n<pre><code class=\"&quot;language-dockerfile&quot;\"># We use a lightweight Go base image FROM golang:alpine as builder # We set the working directory WORKDIR \/app # We copy the source code files to the COPY container. . # We compile the binary RUN go build -o microservice cmd\/api\/main.go # We use alpine to keep our container as light as possible FROM alpine:latest WORKDIR \/root\/ # We copy the compiled binary from the first step COPY --from= builder \/app\/microservice . # We expose the port on which we are listening EXPOSE 8080 # We execute the CMD microservice [&quot;.\/microservice&quot;]<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Construyendo_y_Ejecutando_el_Contenedor\"><\/span>Building and Running the Container<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To build the Docker image and run the container, use the following commands:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">docker build -t my-microservice . docker run -p 8080:8080 --name my-service my-microservice<\/code><\/pre>\n<p>Visit again <code>http:\/\/localhost:8080<\/code> in your browser. Your microservice is now running inside a Docker container.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In this tutorial, we have covered the fundamentals of developing and deploying a microservice using Go and Docker. These tools not only make it easy to create secure and efficient applications but also simplify their deployment and scaling. Ready to take your development skills to new horizons? Remember that you can always find more resources and guides at <a href=\"https:\/\/nelkodev.com\/en\/\">my blog<\/a>.<\/p>\n<p>If you have questions or need help, feel free to contact me via <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">my contact page<\/a>. Happy coding!<\/p>","protected":false},"excerpt":{"rendered":"<p>The world of software development is constantly evolving, and with it, the technologies and methodologies we use. One of the approaches that has gained a lot of popularity in recent years is microservices architecture, especially for its great flexibility and efficiency for applications in production environments. Go, on the other hand, is a language that is [\u2026]<\/p>","protected":false},"author":1,"featured_media":28830,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,1901],"tags":[205,90,211,1962,1963,628,1961,627,1008],"class_list":["post-28829","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-pruebas-de-proyecto","tag-blog","tag-con","tag-desarrollo","tag-despliegue","tag-docker","tag-eficiente","tag-microservicios","tag-proyecto","tag-pruebas"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28829","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=28829"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28829\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28830"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}