How To Create Docker Image For Golang Application
Tags: go, docker, devops, softwaredevelopment
Published: 2023-12-12 13:46:02

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:

```bash
mkdir simple-go-app
```

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

```bash
cd simple-go-app
go mod init simple-go-app
```

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

```go
package main

import "fmt"

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

Run the following command:

```bash
go run main.go
```

Then you will get the following output:

```bash
Hello World!
```

## 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.

```docker
FROM golang:1.21
```

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`.

```docker
WORKDIR /app
```

Copy `go.mod` file into the image.

```docker
COPY go.mod ./
```

Download the required dependencies by adding this code.

```docker
RUN go mod download
```

Copy golang files into the image.

```docker
COPY *.go ./
```

Build the golang application.

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

Expose port for the application.

```docker
EXPOSE 8080
```

Run the built golang application as the image command.

```docker
CMD ["/simple-go-app"]
```

Below are the full code of `Dockerfile` file:

```docker
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"]
```

## 3. Build the image

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

```docker
docker build --tag simple-go-app .
```

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):

```bash
REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
simple-go-app                    latest    7f153fbcc0a8   2 minutes ago   1.11GB
...
```

---

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:

- [https://dasarpemrogramangolang.novalagung.com/A-hello-world.html](https://dasarpemrogramangolang.novalagung.com/A-hello-world.html)
- [https://hub.docker.com/_/golang](https://hub.docker.com/_/golang)
- [https://docs.docker.com/language/golang/build-images/](https://docs.docker.com/language/golang/build-images/)

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