Create a Dockerfile for a Go application that uses multi-stage builds to reduce the final image size
Question
Create a Dockerfile for a Go application that uses multi-stage builds to reduce the final image size. The application should print “Hello Docker”. Sample Go code.
app.go
package main
import "fmt"
func main() {
fmt.Println("Hello Docker")
}
Solution
Dockerfile
FROM golang:1.21.0-alpine
WORKDIR /app
COPY app.go /app
CMD ["go", "run", "app.go"]
Build the image,
docker build -f Dockerfile -t go_image:v1.1.0 .

docker run go_image:v1.1.0

This is a normal image, When we dive into it we can see its 221MB

Let's try using the multistage build,
FROM golang:1.21.0-alpine AS build
WORKDIR /app
COPY app.go .
RUN go build -o myapp app.go
FROM alpine:3.18
WORKDIR /app
COPY --from=build /app/myapp .
CMD ["./myapp"]
Build the image,
docker build -f Dockerfile -t go_image_multi_stage_build:v1.1.0 .

Let's dive into the image,

Now we can see its been reduced from 221MB to 9.2MB.
PreviousRun a container from the ubuntu image and start an interactive shell session inside it.NextCreate a Docker volume and use it to persist data for a MySQL container.
Last updated