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,


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

Let's try using the multistage build,
Build the image,

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