From 8c11dc5739be763d568525f8b556cc62c7f5293e Mon Sep 17 00:00:00 2001 From: Vikramjeet Date: Thu, 24 Oct 2024 20:42:04 +0530 Subject: [PATCH] Refactor Dockerfiles for serverless functions --- serverless/image-to-video/Dockerfile | 33 +++++++++++++--------------- serverless/inpainting/Dockerfile | 32 +++++++++++++-------------- serverless/outpainting/Dockerfile | 32 +++++++++++++-------------- serverless/text-to-image/Dockerfile | 33 +++++++++++++--------------- 4 files changed, 60 insertions(+), 70 deletions(-) diff --git a/serverless/image-to-video/Dockerfile b/serverless/image-to-video/Dockerfile index 3d65c38..04abc7a 100644 --- a/serverless/image-to-video/Dockerfile +++ b/serverless/image-to-video/Dockerfile @@ -3,8 +3,7 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/usr/local/bin:$PATH" + PYTHONPATH=/app # Set up working directory WORKDIR /app @@ -14,6 +13,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ python3.11 \ python3-pip \ python3.11-dev \ + python3.11-venv \ ffmpeg \ libsm6 \ libxext6 \ @@ -22,33 +22,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* -# Create symbolic link for python and pip -RUN ln -sf /usr/bin/python3.11 /usr/bin/python && \ - ln -sf /usr/bin/pip3 /usr/bin/pip - # Create user and set permissions RUN useradd -m -u 1000 user && \ chown -R user:user /app -# Install base packages as root -RUN pip install --no-cache-dir -U pip setuptools wheel +# Switch to user +USER user + +# Create and activate virtual environment +RUN python3.11 -m venv /app/venv +ENV PATH="/app/venv/bin:$PATH" -# Copy all required files and directories +# Copy necessary files maintaining structure COPY --chown=user:user api/ /app/api/ COPY --chown=user:user scripts/ /app/scripts/ COPY --chown=user:user configs/ /app/configs/ COPY --chown=user:user setup.py /app/ -# Install Python dependencies as root -RUN pip install --no-cache-dir -r api/requirements.txt && \ - pip install --no-cache-dir runpod==1.3.3 && \ +# Install dependencies in venv +RUN pip install --no-cache-dir -U pip setuptools wheel && \ + pip install --no-cache-dir -r api/requirements.txt && \ + pip install --no-cache-dir runpod && \ pip install --no-cache-dir -e . -# Switch to user after installations -USER user - +# Copy the service file COPY --chown=user:user serverless/image-to-video/run_image-to-video.py . - - -CMD ["/usr/bin/python3.11", "run_image-to-video.py"] \ No newline at end of file +CMD ["python", "run_image-to-video.py"] \ No newline at end of file diff --git a/serverless/inpainting/Dockerfile b/serverless/inpainting/Dockerfile index 2ab80fb..adb3603 100644 --- a/serverless/inpainting/Dockerfile +++ b/serverless/inpainting/Dockerfile @@ -3,8 +3,7 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/usr/local/bin:$PATH" + PYTHONPATH=/app # Set up working directory WORKDIR /app @@ -14,6 +13,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ python3.11 \ python3-pip \ python3.11-dev \ + python3.11-venv \ ffmpeg \ libsm6 \ libxext6 \ @@ -22,32 +22,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* -# Create symbolic link for python and pip -RUN ln -sf /usr/bin/python3.11 /usr/bin/python && \ - ln -sf /usr/bin/pip3 /usr/bin/pip - # Create user and set permissions RUN useradd -m -u 1000 user && \ chown -R user:user /app -# Install base packages as root -RUN pip install --no-cache-dir -U pip setuptools wheel +# Switch to user +USER user + +# Create and activate virtual environment +RUN python3.11 -m venv /app/venv +ENV PATH="/app/venv/bin:$PATH" -# Copy all required files and directories +# Copy necessary files maintaining structure COPY --chown=user:user api/ /app/api/ COPY --chown=user:user scripts/ /app/scripts/ COPY --chown=user:user configs/ /app/configs/ COPY --chown=user:user setup.py /app/ -# Install Python dependencies as root -RUN pip install --no-cache-dir -r api/requirements.txt && \ - pip install --no-cache-dir runpod==1.3.3 && \ +# Install dependencies in venv +RUN pip install --no-cache-dir -U pip setuptools wheel && \ + pip install --no-cache-dir -r api/requirements.txt && \ + pip install --no-cache-dir runpod && \ pip install --no-cache-dir -e . -# Switch to user after installations -USER user - +# Copy the service file COPY --chown=user:user serverless/inpainting/run_inpainting.py . - -CMD ["/usr/bin/python3.11", "run_inpainting.py"] \ No newline at end of file +CMD ["python", "run_inpainting.py"] \ No newline at end of file diff --git a/serverless/outpainting/Dockerfile b/serverless/outpainting/Dockerfile index 44914b4..a0f572a 100644 --- a/serverless/outpainting/Dockerfile +++ b/serverless/outpainting/Dockerfile @@ -3,8 +3,7 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/usr/local/bin:$PATH" + PYTHONPATH=/app # Set up working directory WORKDIR /app @@ -14,6 +13,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ python3.11 \ python3-pip \ python3.11-dev \ + python3.11-venv \ ffmpeg \ libsm6 \ libxext6 \ @@ -22,32 +22,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* -# Create symbolic link for python and pip -RUN ln -sf /usr/bin/python3.11 /usr/bin/python && \ - ln -sf /usr/bin/pip3 /usr/bin/pip - # Create user and set permissions RUN useradd -m -u 1000 user && \ chown -R user:user /app -# Install base packages as root -RUN pip install --no-cache-dir -U pip setuptools wheel +# Switch to user +USER user + +# Create and activate virtual environment +RUN python3.11 -m venv /app/venv +ENV PATH="/app/venv/bin:$PATH" -# Copy all required files and directories +# Copy necessary files maintaining structure COPY --chown=user:user api/ /app/api/ COPY --chown=user:user scripts/ /app/scripts/ COPY --chown=user:user configs/ /app/configs/ COPY --chown=user:user setup.py /app/ -# Install Python dependencies as root -RUN pip install --no-cache-dir -r api/requirements.txt && \ - pip install --no-cache-dir runpod==1.3.3 && \ +# Install dependencies in venv +RUN pip install --no-cache-dir -U pip setuptools wheel && \ + pip install --no-cache-dir -r api/requirements.txt && \ + pip install --no-cache-dir runpod && \ pip install --no-cache-dir -e . -# Switch to user after installations -USER user - +# Copy the service file COPY --chown=user:user serverless/outpainting/run_outpainting.py . - -CMD ["/usr/bin/python3.11", "run_outpainting.py"] \ No newline at end of file +CMD ["python", "run_outpainting.py"] \ No newline at end of file diff --git a/serverless/text-to-image/Dockerfile b/serverless/text-to-image/Dockerfile index 7db662d..328f0f3 100644 --- a/serverless/text-to-image/Dockerfile +++ b/serverless/text-to-image/Dockerfile @@ -3,8 +3,7 @@ FROM nvidia/cuda:12.2.2-cudnn8-runtime-ubuntu22.04 # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ - PYTHONPATH=/app \ - PATH="/usr/local/bin:$PATH" + PYTHONPATH=/app # Set up working directory WORKDIR /app @@ -14,6 +13,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ python3.11 \ python3-pip \ python3.11-dev \ + python3.11-venv \ ffmpeg \ libsm6 \ libxext6 \ @@ -22,33 +22,30 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* -# Create symbolic link for python and pip -RUN ln -sf /usr/bin/python3.11 /usr/bin/python && \ - ln -sf /usr/bin/pip3 /usr/bin/pip - # Create user and set permissions RUN useradd -m -u 1000 user && \ chown -R user:user /app -# Install base packages as root -RUN pip install --no-cache-dir -U pip setuptools wheel +# Switch to user +USER user + +# Create and activate virtual environment +RUN python3.11 -m venv /app/venv +ENV PATH="/app/venv/bin:$PATH" -# Copy all required files and directories +# Copy necessary files maintaining structure COPY --chown=user:user api/ /app/api/ COPY --chown=user:user scripts/ /app/scripts/ COPY --chown=user:user configs/ /app/configs/ COPY --chown=user:user setup.py /app/ -# Install Python dependencies as root -RUN pip install --no-cache-dir -r api/requirements.txt && \ - pip install --no-cache-dir runpod==1.3.3 && \ +# Install dependencies in venv +RUN pip install --no-cache-dir -U pip setuptools wheel && \ + pip install --no-cache-dir -r api/requirements.txt && \ + pip install --no-cache-dir runpod && \ pip install --no-cache-dir -e . -# Switch to user after installations -USER user - +# Copy the service file COPY --chown=user:user serverless/text-to-image/run_text-to-image.py . - - -CMD ["/usr/bin/python3.11", "run_text-to-image.py"] \ No newline at end of file +CMD ["python", "run_text-to-image.py"] \ No newline at end of file