You need to distribute a Go web service as a minimal, production-ready container image. The security policy says the image must contain only the statically-linked binary, be based on scratch, and run as the unprivileged UID 1001. Which Dockerfile snippet satisfies all of these requirements?
FROM golang:1.22-alpine
WORKDIR /app
COPY . .
RUN go build -o server
USER 1001
CMD ["./server"]
# Stage 1 - build
FROM golang:1.22-alpine AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server
# Stage 2 - minimal runtime
FROM scratch
COPY --from=build /app/server /server
USER 1001
ENTRYPOINT ["/server"]
# Stage 1 - build
FROM golang:1.22-alpine AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server
# Stage 2 - minimal runtime
FROM scratch
COPY --from=build /app/server /server
UID 1001
ENTRYPOINT ["/server"]
# Builder
FROM golang:1.22-alpine AS builder
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /server
# Runtime (still has OS)
FROM alpine:latest
COPY --from=builder /server /server
USER 1001
ENTRYPOINT ["/server"]
The first snippet uses a multi-stage build to compile the code inside a full Go toolchain image and then copies only the resulting binary into a second stage that starts FROM scratch. Setting CGO_ENABLED=0 GOOS=linux ensures the binary is fully static, so it can run in an empty scratch image without any C libraries. Finally, the USER 1001 instruction makes the process run as an unprivileged user.
The other snippets violate at least one requirement:
Keeps all build tools in the final image
Is not based on scratch.
There is no UID directive in Dockerfile syntax (UID is correct)
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is a multi-stage build in Docker?
Open an interactive chat with Bash
Why use `scratch` as the base image?
Open an interactive chat with Bash
What is the significance of the `USER` instruction in the Dockerfile?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Services and User Management
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access