| # Stage 1: Build Stage (for dependencies) | |
| FROM python:3.11-slim as builder | |
| # Install build dependencies for qBittorrent client library | |
| RUN apt-get update && \ | |
| apt-get install -y --no-install-recommends \ | |
| build-essential \ | |
| libssl-dev \ | |
| libffi-dev \ | |
| python3-dev \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Set up working directory | |
| WORKDIR /app | |
| # Copy only the requirements file to leverage Docker cache | |
| COPY requirements.txt . | |
| # Install Python dependencies | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # Stage 2: Final Image | |
| # Use a base image that includes a modern Linux distribution for qBittorrent | |
| FROM ubuntu:22.04 | |
| # Set environment variables | |
| ENV DEBIAN_FRONTEND=noninteractive | |
| ENV QB_WEBUI_PORT=8080 | |
| ENV APP_PORT=8000 | |
| # Install qBittorrent-nox (daemon) and other necessary packages | |
| RUN apt-get update && \ | |
| apt-get install -y --no-install-recommends \ | |
| qbittorrent-nox \ | |
| python3 \ | |
| python3-pip \ | |
| # Install tini for proper signal handling and process management | |
| tini \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Create a non-root user for security | |
| RUN useradd -ms /bin/bash qbuser | |
| # Copy the installed Python packages from the builder stage | |
| COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3/dist-packages | |
| COPY --from=builder /usr/local/bin /usr/local/bin | |
| # Set up the application directory and copy files | |
| WORKDIR /home/qbuser/app | |
| COPY app/ ./app/ | |
| COPY run.sh . | |
| RUN chmod +x run.sh && \ | |
| chown -R qbuser:qbuser /home/qbuser/app | |
| # Set the non-root user | |
| USER qbuser | |
| # Expose the FastAPI port and the qBittorrent Web UI port | |
| EXPOSE 8000 | |
| EXPOSE 8080 | |
| # Use tini as the entrypoint to manage the main process | |
| ENTRYPOINT ["/usr/bin/tini", "--"] | |
| # Run the startup script | |
| CMD ["./run.sh"] | |