[Python] Copying / Syncing files from a pip package that requires additional install to a Prod...

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am on this article (https://www.docker.com/blog/how-to-dockerize-django-app/) to Dockerize a Django App

    I have playwright==1.50.0 in my requirements.txt

    post pip install one has to do playwright install which can follow after

    # Install Python dependencies
    RUN pip install --no-cache-dir -r requirements.txt


    But how do I know this alone is enough to have the browser(s) copied to the Stage 2 (production) build?

    Code under "Make improvements to the Dockerfile"

    # Copy the Python dependencies from the builder stage
    COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
    COPY --from=builder /usr/local/bin/ /usr/local/bin/


    Where do the browsers get installed to on performing playwright install ?

    EDIT (included the Dockerfile code) :

    # Stage 1: Base build stage
    FROM python:3.13 AS builder

    # Create the app directory
    RUN mkdir /app

    # Set the working directory
    WORKDIR /app

    # Set environment variables to optimize Python
    ENV PYTHONDONTWRITEBYTECODE=1
    ENV PYTHONUNBUFFERED=1

    # Upgrade pip and install dependencies
    RUN pip install --upgrade pip

    # Copy the requirements file first (better caching)
    COPY requirements.txt /app/

    # Install Python dependencies
    RUN pip install --no-cache-dir -r requirements.txt
    RUN playwright install

    # Stage 2: Production stage
    FROM python:3.13

    RUN useradd -m -r appuser && \
    mkdir /app && \
    chown -R appuser /app

    # Copy the Python dependencies from the builder stage
    COPY --from=builder /usr/local/lib/python3.13/site-packages/ /usr/local/lib/python3.13/site-packages/
    COPY --from=builder /usr/local/bin/ /usr/local/bin/

    # Set the working directory
    WORKDIR /app

    # Copy application code
    COPY --chown=appuser:appuser . .

    # Set environment variables to optimize Python
    ENV PYTHONDONTWRITEBYTECODE=1
    ENV PYTHONUNBUFFERED=1

    # Switch to non-root user
    USER appuser

    # Expose the application port
    EXPOSE 8000

    # Start the application using Gunicorn
    CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "nerul.wsgi:application"]
    # CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]


    I have RUN playwright install right after RUN pip install.

    Continue reading...

Compartilhe esta Página