What is written in a Dockerfile? What is multi-stage build?
sobes.tech AI
Answer from AI
A Dockerfile describes a sequence of instructions for building a Docker image. Typically, it specifies a base image (FROM), copies files (COPY), installs dependencies (RUN), sets environment variables (ENV), exposes ports (EXPOSE), and defines the command to run the container (CMD or ENTRYPOINT).
Multi-stage build is an approach where multiple build stages are used within a single Dockerfile, each with its own base image. This allows separating the compilation and preparation stages of the application from the final minimal image in which the application will run. As a result, a compact and secure image without unnecessary development tools is produced.
Example of a multi-stage build for Go:
# Build stage
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Final image
FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/myapp .
CMD ["./myapp"]
Here, the binary is first built in a Go image, then only the ready executable is copied into a minimal Alpine image.