Building A Golang Docker Image

Rahul Kapoor
Geek Culture
Published in
3 min readJan 20, 2023
Image source https://www.docker.com/

I was learning docker where the course teacher was building, running and pushing python/java/nodejs docker images. My tech stack includes Golang rather than these other languages.

So I tried to build my Golang image. And you can too!

We will be writing a basic Go (also called Golang) program which will have a / route to display Hello World Go!

We will be using Gorilla mux to match the incoming requests to our defined routes and call the associated handled func to it.

Package gorilla/mux implements a request router and dispatcher for matching incoming requests to their respective handler.

Run go mod init. Then need to create an app.go file which will have the package main and func main defined in it:

package main

import (
"fmt"
"net/http"

"github.com/gorilla/mux"
)

func main() {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello World Go!")
})

fmt.Println("Serving requests on localhost:5000")
http.ListenAndServe(":5000", r)
}

Now dogo build app.gofor the first time, it will install all the packages with specific versions i.e which are the latest at that moment.

It will also create a go.sum file which maintains the checksum so when you rerun the project it will not install all packages again. But use the cache which is stored inside $GOPATH/pkg/mod directory (module cache directory).

go.sum is a generated file you don’t have to edit or modify this file.

Now you can locally test it with go run app.go

Let’s create our Dockerfile now:

FROM golang:latest
WORKDIR /app
COPY go.mod /app
COPY go.sum /app
RUN go mod download
COPY *.go /app
RUN go build app.go
EXPOSE 5000
CMD go run app.go
  1. Here the base image will be golang:latest (I have taken the latest official image)
  2. Then we set the working directory for any subsequent instructions
  3. we will copy our go.mod and go.sum files to this set working dir.
  4. go mod download command, which takes go. mod and go. sum files and downloads the dependencies from them instead of using the source code.
  5. Then copy our files with .go extension to our set working dir
  6. Run go build, expose the port, then run the command go run app.go

To build, run and push the docker image we can follow the below commands

  1. docker build <your_username>/<repository_name>:<tag_name> .

You will get an output something like this where you can even match the steps:

[+] Building 2.4s (13/13) FINISHED                                                                                                                                                                       
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 194B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/golang:latest 2.2s
=> [auth] library/golang:pull token for registry-1.docker.io 0.0s
=> [1/7] FROM docker.io/library/golang:latest@sha256:bb9811fad43a7d6fd2173248d8331b2dcf5ac9af20976b1937ecd214c5b8c383 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 79B 0.0s
=> CACHED [2/7] WORKDIR /app 0.0s
=> CACHED [3/7] COPY go.mod /app 0.0s
=> CACHED [4/7] COPY go.sum /app 0.0s
=> CACHED [5/7] RUN go mod download 0.0s
=> CACHED [6/7] COPY *.go /app 0.0s
=> CACHED [7/7] RUN go build app.go 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:964ca7332349ea7c4e4e909f8b3ddc97478824d8ecf56d0d586c85a2355c67c7
  1. docker run -p 5000:5000 <your_username>/<repository_name>:<tag_name>

Once you execute the docker run command you can test it by running localhost:5000 on your browser. You will get the output below:

Image source screenshot from my chrome browser
  1. docker push <your_username>/<repository_name>:<tag_name>

You can verify it by logging in to the Docker Hub

--

--

Responses (1)