GOLANG & DOCKER

GO: Dockerize Go Project

Nishee Jaiswal
3 min readJul 3, 2021

The prerequisites and the steps to dockerize your Go project is given below.

Prerequisites

  • You will need Docker installed on your development machine along with Go.

The operating system which we will be working on is Linux and all the directory structure and folder path descriptions will be based on Ubuntu 18.04 which is a Linux distribution. This environment setup will be similar for other versions of Ubuntu as well.

Go Project

Now, we will create a simple helloworld project inside our Go workspace.

nishee@ubuntu:~$ pwd 
/home/nishee
nishee@ubuntu:~$ cd go/src
nishee@ubuntu:~/go/src$ mkdir helloworld
nishee@ubuntu:~/go/src$ cd helloworld
nishee@ubuntu:~/go/src/helloworld$ touch main.go
nishee@ubuntu:~/go/src/helloworld$ code .

Now, write the below code inside your main.go file.

package mainimport "fmt"func main(){
fmt.Println("Hello World!")
}

Execute the below command after saving the main.go file.

nishee@ubuntu:~/go/src/helloworld$ go run main.go

Below is the output of the above go source code file on the terminal.

Hello World!

If you are writing your Go code using Visual Studio Code text editor, then install the Go extension inside it.
To execute code .from the terminal, you need to install it using the following command on the terminal sudo apt install code.

You can also open your helloworld project directly from Visual Studio Code by going inside the File Manager of your system instead of code . .

What is a Dockerfile?

So, our first question is simply what is a Dockerfile? When you run the Docker run command and specify WordPress, Docker uses this file to build the image itself. The Dockerfile is essentially the build instructions to build the image.

The advantage of a Dockerfile over just storing the binary image (or a snapshot/template in other virtualization systems) is that the automatic builds will ensure you have the latest version available. This is a good thing from a security perspective, as you want to ensure you’re not installing any vulnerable software.

Dockerfile

Now, create a Dockerfile with the name Dockerfile only where we will write a Dockerfile inside the same helloworld project folder where the main.go file resides.

# Start from golang:1.15-alpine base image.
FROM golang:1.15-alpine
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy the code into the container
COPY . .
# Build the Go app
RUN go build -o main
# Command to run when starting the container
CMD ["./main"]

Execute the below command to build the docker image using the above Dockerfile.

nishee@ubuntu:~/go/src/helloworld$ pwd
/home/nishee/go/src/helloworld
nishee@ubuntu:~/go/src/helloworld$ docker build -t helloworld .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM golang:1.15-alpine
---> 9c252852c824
Step 2/4 : COPY . .
---> Using cache
---> 1f7b8266f717
Step 3/4 : RUN go build
---> Using cache
---> 7fd8cc7a808d
Step 4/4 : CMD ["./main"]
---> Using cache
---> a1573630bad9
Successfully built a1573630bad9
Successfully tagged helloworld:latest
nishee@ubuntu:~/go/src/helloworld$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld latest a1573630bad9 6 minutes ago 302MB
nishee@ubuntu:~/go/src/helloworld$ docker run helloworld
Hello World!
nishee@ubuntu:~/go/src/helloworld$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e00384630b34 helloworld "./main" 8 minutes ago Exited (0) 8 minutes ago exciting_hypatia

The output of executing docker run helloworldis given below. Here, helloworld is the image name, the image that we created using the Dockerfile.

Hello World!

Since the Go project which we are containerizing is not a running server application, the container will immediately stop after executing the main()function of the helloworld project.

While executing docker ps you will be unable to list down the container which you have created now as it only lists the active containers. But executing the docker ps -a command will list down there as it lists all the stopped containers.

You have now successfully dockerized your Go project!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Nishee Jaiswal
Nishee Jaiswal

Written by Nishee Jaiswal

Software Engineer at Siemens, Pune.

Responses (1)

Write a response