هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
FROM python:3.13-slim
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd --system --uid 10001 --no-create-home app
USER 10001
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"].git
.env
__pycache__/
*.pyc
tests/
DockerfileStep by step
- Start from a small, pinned base image.
slim,alpineordistrolessinstead of the full image; a fixed version tag (and in CI a digest):
FROM python:3.13-slim@sha256:<digest from docker buildx imagetools inspect>- Order for the cache: copy the dependency list first, install, then copy the code. A code change then does not reinstall all packages.
- **One
RUNfor install + cleanup** (files deleted in a later layer still take space):
RUN apt-get update && apt-get install -y --no-install-recommends libpq5 && rm -rf /var/lib/apt/lists/*- Multi-stage build — compile in a big image, ship only the result (Go example, final image a few MB):
FROM golang:1.24 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/api ./cmd/api
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/api /api
USER nonroot:nonroot
EXPOSE 8080
ENTRYPOINT ["/api"]- The review rules:
| Rule | Why |
|---|---|
USER with a non-root UID | A bug in the app does not give root in the container. |
| No secrets in the Dockerfile or image | Every layer can be read (docker history). Use build secrets (RUN --mount=type=secret) or runtime env. |
.dockerignore | Keeps .git, .env and junk out of the image and the build context. |
CMD/ENTRYPOINT in JSON form | The app gets signals (clean docker stop). |
COPY, not ADD | ADD can download URLs and unpack archives — surprises. |
| Labels | org.opencontainers.image.source, .version — know where an image came from. |
- Build, look at the size and the history:
Check that it worked
The image is small (compare with the old one in docker image ls), id in the container is not root, a code-only change rebuilds in seconds (cached layers), and a scan shows fewer findings (trivy image shop/api:1.4.2).
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Every build installs all packages again | Code is copied before the dependency file. | Copy requirements.txt / package*.json / go.mod first. |
permission denied after USER 10001 | Files belong to root, the app writes to them. | COPY --chown=10001:10001; write only to /tmp or a volume. |
| Alpine image: strange crashes or slow Python builds | musl libc instead of glibc; no wheels. | Use -slim (Debian) for Python and Java apps. |
| Distroless: no shell for debugging | By design. | Use the :debug variant (has busybox) in test only, or attach a tool container: docker run -it --rm --pid=container:api --network=container:api busybox:1.37. |
| The image is still big | Build tools or caches in the last stage. | Multi-stage; --no-cache-dir; rm -rf /var/lib/apt/lists/* in the same RUN. |
docker stop takes 10 seconds | Shell-form CMD — the app does not get SIGTERM. | JSON form CMD ["app"], or exec in an entry script. |
Go further
- Command pages: docker, trivy.
- Next how-tos: Scan a container image for vulnerabilities, Push an image to a private registry, Run an app with Docker Compose, Build images for ARM and x86.
- Learn it: Docker — Dockerfile instructions, Docker — Layer cache, Docker — Multi-stage, Docker — Non-root, Docker — Build secrets.
- Daemonless builds: Buildah and Skopeo.
- Practise: Lab: shrink an image from 900 MB to under 20 MB.
1Why copy the dependency file before the source code?
The install layer stays cached; a code change rebuilds only the last layers.
2Why is a secret deleted in a later RUN still unsafe?
It stays in the earlier layer; anyone with the image can read it.