From 1f21998f0c2115a76b1c53ae2b89ea581d2fd106 Mon Sep 17 00:00:00 2001 From: sunnypranay Date: Thu, 13 Apr 2023 21:47:28 -0500 Subject: [PATCH] Improve Dockerfile with best practices and optimizations --- Dockerfile | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4d264c88c8b5..e776664e8483 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,23 @@ +# Use an official Python base image from the Docker Hub FROM python:3.11-slim -ENV PIP_NO_CACHE_DIR=yes -WORKDIR /app -COPY requirements.txt . -RUN pip install -r requirements.txt -COPY scripts/ . -ENTRYPOINT ["python", "main.py"] + +# Set environment variables +ENV PIP_NO_CACHE_DIR=yes \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +# Create a non-root user and set permissions +RUN useradd --create-home appuser +WORKDIR /home/appuser +RUN chown appuser:appuser /home/appuser +USER appuser + +# Copy the requirements.txt file and install the requirements +COPY --chown=appuser:appuser requirements.txt . +RUN pip install --no-cache-dir --user -r requirements.txt + +# Copy the application files +COPY --chown=appuser:appuser scripts/ . + +# Set the entrypoint +ENTRYPOINT ["python", "main.py"] \ No newline at end of file