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:
- Create a simple golang application
- Create a Dockerfile
- 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
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
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!")
}
Run the following command:
go run main.go
Then you will get the following output:
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.
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
.
WORKDIR /app
Copy go.mod
file into the image.
COPY go.mod ./
Download the required dependencies by adding this code.
RUN go mod download
Copy golang files into the image.
COPY *.go ./
Build the golang application.
RUN CGO_ENABLED=0 GOOS=linux go build -o /simple-go-app
Expose port for the application.
EXPOSE 8080
Run the built golang application as the image command.
CMD ["/simple-go-app"]
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"]
3. Build the image
Run the following command inside the directory that contains the Dockerfile
file:
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):
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://hub.docker.com/_/golang
- https://docs.docker.com/language/golang/build-images/
Cover image:
https://fastly.picsum.photos/id/655/1920/1080.jpg?hmac=aKxkq2OlSgFaw0pcf_uve5cFi267Y7c92cP9UjDti8s
Top comments (0)