From d2a4816454cb892f2b950040e28a044621b5aff9 Mon Sep 17 00:00:00 2001 From: Bilal Qamar <59555732+BilalQamar95@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:09:56 +0500 Subject: [PATCH] perf: updated Dockerfile to optimize requirements installation and dependency caching --- .../push-enterprise-access-image.yaml | 5 +++++ dockerfiles/enterprise-access.Dockerfile | 21 ++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/.github/workflows/push-enterprise-access-image.yaml b/.github/workflows/push-enterprise-access-image.yaml index 53a8ce5..27501ce 100644 --- a/.github/workflows/push-enterprise-access-image.yaml +++ b/.github/workflows/push-enterprise-access-image.yaml @@ -9,6 +9,11 @@ on: schedule: - cron: "0 4 * * 1-5" # UTC Time +# Added for testing purposes. Will remove once the PR is finalised + pull_request: + branches: + - '**' + jobs: build-and-push-image: runs-on: ubuntu-latest diff --git a/dockerfiles/enterprise-access.Dockerfile b/dockerfiles/enterprise-access.Dockerfile index ac5d5f5..74802d1 100644 --- a/dockerfiles/enterprise-access.Dockerfile +++ b/dockerfiles/enterprise-access.Dockerfile @@ -46,7 +46,7 @@ RUN apt-get update && apt-get -qy install --no-install-recommends \ pkg-config \ libmysqlclient-dev \ libssl-dev \ - git \ + git \ wget \ curl \ libffi-dev \ @@ -59,16 +59,14 @@ RUN apt-get update && apt-get -qy install --no-install-recommends \ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN pip install --upgrade pip setuptools -# delete apt package lists because we do not need them inflating our image + +# Remove package lists to reduce image size RUN rm -rf /var/lib/apt/lists/* -# need to use virtualenv pypi package with Python 3.12 +# Set up Python environment and install virtualenv RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} RUN pip install virtualenv -# cloning git repo -RUN curl -L https://github.com/openedx/enterprise-access/archive/refs/heads/main.tar.gz | tar -xz --strip-components=1 - # Create a virtualenv for sanity ENV VIRTUAL_ENV=/edx/venvs/enterprise-access RUN virtualenv -p python${PYTHON_VERSION} $VIRTUAL_ENV @@ -94,16 +92,18 @@ RUN useradd -m --shell /bin/false app WORKDIR /edx/app/enterprise-access -RUN pwd - +RUN curl -L -o /requirements/pip.txt https://raw.githubusercontent.com/openedx/enterprise-access/main/requirements/pip.txt +RUN curl -L -o /requirements/production.txt https://raw.githubusercontent.com/openedx/enterprise-access/main/requirements/production.txt # Dependencies are installed as root so they cannot be modified by the application user. RUN pip install -r /requirements/pip.txt RUN pip install -r /requirements/production.txt RUN mkdir -p /edx/var/log -# Code is owned by root so it cannot be modified by the application user. -# So we copy it before changing users. +# Clone the source code +RUN curl -L https://github.com/openedx/enterprise-access/archive/refs/heads/main.tar.gz | tar -xz --strip-components=1 + +# Change user to app USER app # Gunicorn 19 does not log to stdout or stderr by default. Once we are past gunicorn 19, the logging to STDOUT need not be specified. @@ -115,6 +115,7 @@ CMD newrelic-admin run-program gunicorn --workers=2 --name enterprise-access -c FROM app as devstack USER root +RUN curl -L -o /requirements/dev.txt https://raw.githubusercontent.com/openedx/enterprise-access/main/requirements/dev.txt RUN pip install -r /requirements/dev.txt USER app CMD gunicorn --workers=2 --name enterprise-access -c /edx/app/enterprise-access/enterprise_access/docker_gunicorn_configuration.py --log-file - --max-requests=1000 enterprise_access.wsgi:application