Sobes.tech
Junior — Middle

Kuidas toimub Docker piltide koostamine Go keeles kirjutatud rakenduste jaoks?

sobes.tech AI

Vastus AI-lt

To build Docker images for Go applications, a common approach is to use a multi-stage build to create a minimal and optimized image.

Main steps:

  1. In the first stage (builder), an official Go SDK image is used to compile the application.
  2. In the second stage, a minimal image (e.g., based on scratch or alpine) 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

# Final minimal 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 sources.