Do you struggle to begin with Docker? Would you like to be the one to dockerize a Golang app effectively? Are you having difficulty determining a basic tutorial for dockerizing a Golang app? Each of these queries and only one solution – yes, you've picked the best tutorial!
For beginners,this tutorial will be really useful. In this guide, we will design an app by showing some basic steps to dockerize the golang app. Without any more hesitation, let's begin our tutorial.
Dockerization of a Golang App Requirements
Once you start developing the app, assure that your system has docker and golang set up. If not, follow the instructions to install it.
⦿ Install Docker
⦿ Install Golang
Setup of the Project
First and foremost, let's make the main.go file using the command go mod init, create and restart the app.
The project structure will be as follows:
sample-dockerize-app
|– main.go
|– Dockerfile
Main.go will contain the code mentioned below.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
response := map[string]string{
"message": "Welcome to Dockerized app",
}
json.NewEncoder(rw).Encode(response)
})
router.HandleFunc("/{name}", func(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name := vars["name"]
var message string
if name == "" {
message = "Hello World"
} else {
message = "Hello " + name
}
response := map[string]string{
"message": message,
}
json.NewEncoder(rw).Encode(response)
})
log.Println("Server is running!")
fmt.Println(http.ListenAndServe(":8081", router))
}
Make a Docker Image.
According to the documentation, an image contains everything required to run an application, including the code, dependencies, and any other objects required.
An image defines your app that will be required for handling the app in the most basic terms.
You must write steps in the config file to create a Docker image. Dockerfile is the most common and preferred file name, but you can use any name you want. However, in my opinion, it is always preferable to adhere to standards.
Now, create a file named Dockerfile and write the following code.
# Build Stage
# First pull Golang image
FROM golang:1.17-alpine as build-env
# Set environment variable
ENV APP_NAME sample-dockerize-app
ENV CMD_PATH main.go
# Copy application data into image
COPY . $GOPATH/src/$APP_NAME
WORKDIR $GOPATH/src/$APP_NAME
# Budild application
RUN CGO_ENABLED=0 go build -v -o /$APP_NAME $GOPATH/src/$APP_NAME/$CMD_PATH
# Run Stage
FROM alpine:3.14
# Set environment variable
ENV APP_NAME sample-dockerize-app
# Copy only required data into this image
COPY --from=build-env /$APP_NAME .
# Expose application port
EXPOSE 8081
# Start app
CMD ./$APP_NAME
Run the code mentioned below and update the dependencies
go mod tidy
Build and Run Docker Image
To build the docker image Use the below code
docker build --rm -t golang-docker-example .
The output visible on your screen will look something like this
To run the image use the command mentioned below
docker run -p 8081:8081 golang-docker-example
To conclude, this was a beginner's tutorial about how to dockerize the Golang app with the help of an example of the "HELLO WORLD" application. If you want to learn in detail about this, you can refer to the link mentioned above, and if you are thinking of creating this kind of app, you should get in touch with the best Golang development company and hire golang developers from them.
About the Creator
Bacancy
A Leader in Agile and Lean Software Development
Comments
There are no comments for this story
Be the first to respond and start the conversation.