Containerize the go language REST API using Docker.

Create a docker file and build a docker image for go REST API applications.

In this article, we will be learning how to build a simple "Hello World" REST API using Go language and then we will containerize it with Docker. We will also look at how to run and test our Docker container.

Prerequisites:

Before we proceed, you should have the following tools installed:

  • Go language

  • Docker

Building the "Hello World" REST API:

Let's start by creating a simple "Hello World" REST API in Go. Open your favorite text editor and create a new file named main.go. Copy and paste the following code:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })

    log.Fatal(http.ListenAndServe(":8080", nil))
}

This code creates an HTTP server that listens on port 8080 and returns a "Hello, World!" message when the root URL is requested.

Now, let's build and test our REST API. Open your terminal and navigate to the directory where you saved the main.go file. Run the following command:

go run main.go

This will start the HTTP server. Open your web browser and navigate to http://localhost:8080. You should see the "Hello, World!" message displayed.

Containerizing the REST API with Docker:

Now that we have our "Hello World" REST API working, let's containerize it with Docker. Create a new file named Dockerfile in the same directory as main.go. Copy and paste the following code:

FROM golang:alpine

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

RUN go build -o main .

EXPOSE 8080

CMD ["./main"]

This Dockerfile specifies that we will be using the Alpine Linux distribution as the base image for our container. We then set the working directory to /app and copy the go.mod and go.sum files into it. We run go mod download to download the dependencies specified in the go.mod file.

Next, we copy the entire directory into the container and run go build to build the binary executable named main. We expose port 8080, which is the port that our HTTP server is listening on. Finally, we set the command to run our executable.

To build the Docker image, run the following command in the terminal:

docker build -t hello-world-rest-api .

This will build the Docker image with the tag hello-world-rest-api.

Running and testing the Docker container:

Now that we have built the Docker image, let's run it and test it. Run the following command in the terminal:

docker run -p 8080:8080 hello-world-rest-api

This will start the Docker container and map port 8080 on the host to port 8080 in the container.

Open your web browser and navigate to http://localhost:8080. You should see the "Hello, World!" message displayed.

Conclusion:

In this article, we learned how to build a simple "Hello World" REST API using Go language and then containerize it with Docker. We also learned how to run and test our Docker container. Containerizing your applications with Docker allows you to easily deploy and run them on any platform without worrying about dependencies or configurations.

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!