NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior86 views

Практическое задание AI подготовил Dockerfile для FastAPI. Проведи ревью и предложи исправленную версию: сделай сборку воспроизводимой, не запускай приложение с отладочными настройками и учти запуск контейнера без привилегий. ``` FROM python:3.12 WORKDIR /app COPY . RUN pip install -r requirements.txt ENV DEBUG=true EXPOSE 8000 CMD ["uvicorn", "app:app", "--host", "0.0.0.0"] ```

Practice

Asked at

AI answer

In the original version, COPY . does not specify a destination, dependencies are not pinned, the base image is floating, and DEBUG=true enables a debug setting. The container also runs as root. The version below assumes that requirements.txt contains pinned dependency versions; for a reproducible build, use a verified digest for the base image and, preferably, a lock file with hashes.

# Replace the digest with the current verified digest for the required image.
FROM python:3.12-slim@sha256:<verified-digest>

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1

WORKDIR /app

RUN groupadd --gid 10001 app \
    && useradd --uid 10001 --gid app --create-home app

COPY requirements.txt ./
RUN python -m pip install --no-cache-dir -r requirements.txt

COPY --chown=app:app . .

USER 10001:10001
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Also add a .dockerignore file (for example, to exclude .git, the virtual environment, and local secrets) so they are not included in the image. In production, enable debug mode only when necessary through environment configuration; it should be off by default.