هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Split build and runtime
Each FROM starts a new stage. COPY --from=STAGE takes only the files you need.
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/app /app
USER nonroot
EXPOSE 8080
ENTRYPOINT ["/app"]| Base image | Size | Has a shell / package manager |
|---|---|---|
ubuntu, debian | 70 – 120 MB | Yes |
*-slim | 30 – 80 MB | Yes, fewer packages |
alpine | about 8 MB | Yes (musl libc, can differ) |
| UBI minimal / micro (Red Hat) | 30 – 100 MB / about 25 MB | minimal: microdnf / micro: no |
| distroless | 2 – 20 MB | No — smallest attack surface |
Run as a non-root user
Root in the container is root on the host if the container escapes. Use a fixed, non-root UID.
RUN groupadd -r app -g 1001 && useradd -r -u 1001 -g app app
USER 1001:1001docker run --rm shop:dev id
uid=1001(app) gid=1001(app) groups=1001(app)تذكّر
Many orchestrators (and OpenShift) run containers with a random UID. Make the app folder readable by the group 0, and do not write outside /tmp and mounted volumes.
Pin the base and rebuild often
Pin the base image (tag or digest) so builds are repeatable — and rebuild regularly so you get the security fixes.
FROM python:3.12.7-slim@sha256:DIGESTفي العمل
Let Dependabot or Renovate open PRs when a new base image digest appears, and rebuild every week even without code changes.
Add metadata
Standard OCI labels say where an image came from.
LABEL org.opencontainers.image.source="https://github.com/s0sta/shop" \
org.opencontainers.image.revision="${GIT_SHA}" \
org.opencontainers.image.version="${VERSION}"Check an image before you ship it
Look at size, layers, user and vulnerabilities.