Follow

Follow

Containerizing Go-Gin application using docker!

Maheshwar Ligade's photo
Maheshwar Ligade
·May 17, 2023·

3 min read

Play this article

Table of contents

  • Introduction
  • Prerequisites
  • Building the Gin Framework Application
  • Creating a Dockerfile
  • Building a Docker Image
  • Running the Docker Container
  • Conclusion

Introduction

Containerization has become a popular way of packaging and deploying applications. Docker is one of the most popular containerization platforms, which enables developers to easily package their applications into a lightweight, portable container that can run anywhere. In this article, we will explore how to containerize a Gin Framework application using Docker.

Prerequisites

To follow along with this tutorial, you will need the following:

  • Go programming language installed on your machine

  • Docker installed on your machine

Building the Gin Framework Application

First, let's create a simple Gin Framework application that we will containerize using Docker. Here is an example of a simple Gin Framework application:

package main

import (
    "net/http"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    r.GET("/", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "Hello, world!",
        })
    })

    r.Run(":8080")
}

This application creates a simple HTTP server that responds to GET requests on the root URL with a JSON message.

Save this code in a file named main.go. Now, let's build this application.

go mod init example.com/hello
go mod tidy
go build

These commands will create a Go module for our application, download the required dependencies, and build the application. You should now have an executable file named hello.

Creating a Dockerfile

Now that we have built our Gin Framework application, let's create a Dockerfile that will allow us to build a Docker image for our application.

Create a new file named Dockerfile in the same directory as your main.go file, with the following content:

FROM golang:1.16-alpine AS build

WORKDIR /app
COPY . .

RUN go mod download
RUN go build -o /app/hello

FROM alpine:latest

WORKDIR /app
COPY --from=build /app/hello .

CMD ["./hello"]

This Dockerfile specifies a two-stage build process. In the first stage, we use the official Golang Docker image as a base image, and copy our application code to the /app directory. We then download the required dependencies and build our application. In the second stage, we use the official Alpine Linux image as a base image, and copy the executable file generated in the first stage to the /app directory. Finally, we specify the command to run our application when the container starts.

Building a Docker Image

To build a Docker image for our Gin Framework application, we need to run the following command:

docker build -t gin-example .

This command tells Docker to build a new image with the name gin-example using the Dockerfile in the current directory.

Running the Docker Container

Now that we have built a Docker image for our application, we can run it in a container using the following command:

docker run -p 8080:8080 gin-example

This command tells Docker to run a new container from the gin-example image, and map port 8080 in the container to port 8080 on the host machine.

If everything worked correctly, you should be able to access your Gin Framework application by navigating to http://localhost:8080 in your web browser.

Conclusion

In this article, we have explored how to containerize a Gin Framework application using Docker. We created a simple Gin Framework application and created a Dockerfile to build a Docker image for the application.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this