متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 27Containers · Docker

How to write a small and safe Dockerfile

A quick Dockerfile works — and gives you a 1 GB image that runs as root, has build tools and old packages inside, and rebuilds everything on every small change. A good one is small, fast to build, runs as a normal user, and has fewer CVEs to fix. This how-to shows a production-ready Dockerfile for a Python app and a multi-stage one for Go, and explains each rule you see in reviews.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

27.1

The quick answer

Dockerfile
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"]
.dockerignore
.git
.env
__pycache__/
*.pyc
tests/
Dockerfile

27.2

Step by step

  1. Start from a small, pinned base image. slim, alpine or distroless instead of the full image; a fixed version tag (and in CI a digest):
Dockerfile
FROM python:3.13-slim@sha256:<digest from docker buildx imagetools inspect>
  1. Order for the cache: copy the dependency list first, install, then copy the code. A code change then does not reinstall all packages.
  2. **One RUN for install + cleanup** (files deleted in a later layer still take space):
Dockerfile
RUN apt-get update && apt-get install -y --no-install-recommends libpq5 && rm -rf /var/lib/apt/lists/*
  1. Multi-stage build — compile in a big image, ship only the result (Go example, final image a few MB):
Dockerfile
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"]
  1. The review rules:
RuleWhy
USER with a non-root UIDA bug in the app does not give root in the container.
No secrets in the Dockerfile or imageEvery layer can be read (docker history). Use build secrets (RUN --mount=type=secret) or runtime env.
.dockerignoreKeeps .git, .env and junk out of the image and the build context.
CMD/ENTRYPOINT in JSON formThe app gets signals (clean docker stop).
COPY, not ADDADD can download URLs and unpack archives — surprises.
Labelsorg.opencontainers.image.source, .version — know where an image came from.
  1. Build, look at the size and the history:
docker build -t shop/api:1.4.2 .
docker image ls shop/api
docker history shop/api:1.4.2
docker run --rm shop/api:1.4.2 id
uid=10001(app) gid=10001(app) groups=10001(app)

الأوامر في هذا الدرس

27.3

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).

27.4

Common problems

ProblemCauseFix
Every build installs all packages againCode is copied before the dependency file.Copy requirements.txt / package*.json / go.mod first.
permission denied after USER 10001Files 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 buildsmusl libc instead of glibc; no wheels.Use -slim (Debian) for Python and Java apps.
Distroless: no shell for debuggingBy 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 bigBuild 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 secondsShell-form CMD — the app does not get SIGTERM.JSON form CMD ["app"], or exec in an entry script.

الأوامر في هذا الدرس

27.5

Go further

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.

كل الأوامر في هذا الفصل