Let’s dockerize my yt-comment-sentiment project which is a FastAPI app managed via UV.
Official Docs
- FastAPI docs for Dockerization
- UV docs for Dockerization
- UV-FastAPI example docs for Dockerization
:thinking: Then, Why this?
I also consider above docs to fulfill my requirements but this includes:
- Multistage build to reduce final image size.
- Best and Flexible practices to use
uvin Docker. - Solution of some problems which I have encountered.
Dockerfile
I am taking reference of my yt-comment-sentiment which I have developed and recently and
continuously improving it.
FROM python:3.11-slim AS builder
# See official docs at https://docs.astral.sh/uv/guides/integration/docker/#available-images
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/
# Install gcc for wordcloud
# Using `wordcloud` in backend and it require `gcc` package to build wheels to work in python.
RUN apt-get update && apt-get install -y gcc && apt-get clean
WORKDIR /app
ADD pyproject.toml uv.lock /app
# Install dependencies with `--extra=backend` dependencies
RUN uv sync --extra=backend --frozen --compile-bytecode --no-install-project
# Copy only necessary files/folders to reduce image size
COPY params.yaml /app
COPY backend /app/backend
COPY ml /app/ml
RUN uv sync --extra=backend --locked
# Final stage
# Use multistage build to reduce the image size.
FROM python:3.11-slim AS final
COPY --from=builder /app /app
WORKDIR /app
# Run backend using fastapi-cli
CMD [".venv/bin/fastapi", "run", "--host", "0.0.0.0", "--port", "8000", "backend/app.py"]If you get any other problem please refer to official docs.