Created
September 22, 2025 10:13
-
-
Save initcron/938192266dbf789fce0ea647393db34d to your computer and use it in GitHub Desktop.
Dockerfile with Buildkit features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # syntax=docker/dockerfile:1 | |
| # BuildKit optimized Dockerfile with advanced caching | |
| # Stage 1: Dependencies stage with cache mounts | |
| FROM python:3.11-slim AS dependencies | |
| WORKDIR /app | |
| # Use cache mount for apt packages | |
| RUN --mount=type=cache,target=/var/cache/apt \ | |
| --mount=type=cache,target=/var/lib/apt \ | |
| apt-get update && apt-get install -y \ | |
| gcc \ | |
| python3-dev | |
| # Use cache mount for pip packages | |
| COPY requirements.txt . | |
| RUN --mount=type=cache,target=/root/.cache/pip \ | |
| pip install --user -r requirements.txt | |
| # Stage 2: Model training stage | |
| FROM dependencies AS trainer | |
| COPY train.py . | |
| # Train the model with cache mount for any downloads | |
| RUN --mount=type=cache,target=/tmp \ | |
| python train.py | |
| # Stage 3: Final production stage | |
| FROM python:3.11-slim AS production | |
| # Create non-root user | |
| RUN useradd --create-home --shell /bin/bash mluser | |
| WORKDIR /app | |
| # Copy Python packages from dependencies stage | |
| COPY --from=dependencies /root/.local /home/mluser/.local | |
| # Copy application files | |
| COPY app.py . | |
| COPY requirements.txt . | |
| # Copy trained models from trainer stage | |
| COPY --from=trainer /app/*.pkl ./ | |
| # Set proper ownership | |
| RUN chown -R mluser:mluser /app | |
| USER mluser | |
| # Update PATH for user-installed packages | |
| ENV PATH=/home/mluser/.local/bin:$PATH | |
| # Health check with improved reliability | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=15s --retries=3 \ | |
| CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860', timeout=5)" || exit 1 | |
| EXPOSE 7860 | |
| CMD ["python", "app.py"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment