Практическое задание 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"] ```
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.
Also asked at ylab
- Could you tell me about your overall role in this project? Were you fully responsible for it, or were you responsible for a specific part?
- Could you tell about a situation where you made a mistake at work?
- How can you find and analyze slow queries in PostgreSQL?
- 20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. 3. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false
Similar questions
- Tell me how to package and run a Python service in a Docker container.
- Write a Dockerfile for a Python application that does the following: - Uses the latest Python image - Sets the working directory to /app - Copies the current directory contents into the container - Installs the dependencies listed in requirements.txt - Exposes port 8000 - Runs the application using the command: python app.py
- Have you ever written a Dockerfile or `docker-compose.yml` yourself?
- Conduct a code review of a file with five FastAPI endpoints in a monologue format: point out issues and suggest fixes.
- What methods do you use to launch and configure FastAPI applications?