Junior — Middle
How is the Docker image build process for applications written in Go language carried out?
sobes.tech AI
Answer from AI
To build Docker images of Go applications, a multi-stage build is usually used to obtain a minimal and optimized image.
Main steps:
- In the first stage (builder), an official image with Go SDK is used, where the application is compiled.
- In the second stage, a minimal image (for example, based on
scratchoralpine) is created, into which only the compiled binary is copied.
Example Dockerfile:
# Build stage
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Minimal final image
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/myapp /usr/local/bin/myapp
ENTRYPOINT ["/usr/local/bin/myapp"]
This approach reduces the size of the final image and excludes unnecessary tools and source code.