DEV Community

Cover image for How To Create Docker Image For Golang Application
luthfisauqi17
luthfisauqi17

Posted on

How To Create Docker Image For Golang Application

Hi all, in this blog I will show you how to build a docker image for your golang application. There are three sections you should follow to gain a comprehensive understanding about this topic:

  1. Create a simple golang application
  2. Create a Dockerfile
  3. Build the image

Let’s start from the first section…


1. Create a simple golang application

Create a directory for your golang application, lets call it simple-go-app. Use the following command to perform the operation:

mkdir simple-go-app
Enter fullscreen mode Exit fullscreen mode

Inside the directory, lets initialize a golang application module and call it simple-go-app using this command:

cd simple-go-app
go mod init simple-go-app
Enter fullscreen mode Exit fullscreen mode

Create a file with the name main.go and copy the following code into that file:

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")
}
Enter fullscreen mode Exit fullscreen mode

Run the following command:

go run main.go
Enter fullscreen mode Exit fullscreen mode

Then you will get the following output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

2. Create a Dockerfile

You already made a simple go application, next step is to define the Dockerfile in order to generate its docker image.

First step is to create a file with the name Dockerfile inside the root folder of your simple golang application project.

We will define several code inside the file, and I will explain it one-by-one:

Instead of defining the each required tools & software needed for golang application, you could use the existing golang image as the base of your image as it has installed the requirements.

FROM golang:1.21
Enter fullscreen mode Exit fullscreen mode

For the golang version, you could refer to your local development environment.

Next, lets define the work directory for our application inside the image on the /app.

WORKDIR /app
Enter fullscreen mode Exit fullscreen mode

Copy go.mod file into the image.

COPY go.mod ./
Enter fullscreen mode Exit fullscreen mode

Download the required dependencies by adding this code.

RUN go mod download
Enter fullscreen mode Exit fullscreen mode

Copy golang files into the image.

COPY *.go ./
Enter fullscreen mode Exit fullscreen mode

Build the golang application.

RUN CGO_ENABLED=0 GOOS=linux go build -o /simple-go-app
Enter fullscreen mode Exit fullscreen mode

Expose port for the application.

EXPOSE 8080
Enter fullscreen mode Exit fullscreen mode

Run the built golang application as the image command.

CMD ["/simple-go-app"]
Enter fullscreen mode Exit fullscreen mode

Below are the full code of Dockerfile file:

FROM golang:1.21

WORKDIR /app

COPY go.mod ./

RUN go mod download

COPY *.go ./

RUN CGO_ENABLED=0 GOOS=linux go build -o /simple-go-app

EXPOSE 8080

CMD ["/simple-go-app"]
Enter fullscreen mode Exit fullscreen mode

3. Build the image

Run the following command inside the directory that contains the Dockerfile file:

docker build --tag simple-go-app .
Enter fullscreen mode Exit fullscreen mode

After the operation is done, run docker images to all docker image in your machine.

If the image building process is successful, you should see this output (your image id and size could be different):

REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
simple-go-app                    latest    7f153fbcc0a8   2 minutes ago   1.11GB
...
Enter fullscreen mode Exit fullscreen mode

There you go, that is how you can create image of a golang application. Thank you for reading this article and have a nice day!

Source:

Cover image:
https://fastly.picsum.photos/id/655/1920/1080.jpg?hmac=aKxkq2OlSgFaw0pcf_uve5cFi267Y7c92cP9UjDti8s

Top comments (0)