From 316fa78ada4414919fc3b8590828c1512bc4d649 Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Tue, 30 Apr 2024 22:45:39 -0400 Subject: [PATCH] Refactor django container build with poetry dependency management --- django/Dockerfile | 23 +++++++++++++++-------- django/install-packages.sh | 14 ++++++++++++++ docker-compose.yml | 5 +++-- 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 django/install-packages.sh diff --git a/django/Dockerfile b/django/Dockerfile index ebac72eb1..7a5b23dc3 100644 --- a/django/Dockerfile +++ b/django/Dockerfile @@ -1,18 +1,25 @@ -# start from an official image +# Download and install python dependencies in a container +FROM python:3.9 as dependency-install-container +ARG PROJECT_ENVIRONMENT +COPY ./poetry.lock ./pyproject.toml ./django/install-packages.sh /code/ +WORKDIR /code +RUN chmod u+x /code/install-packages.sh && \ + /code/install-packages.sh $PROJECT_ENVIRONMENT + +# Copy our dependencies into the base image FROM python:3.9 +COPY --from=dependency-install-container /code/.venv /code/.venv ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 EXPOSE 8000 -WORKDIR /code/django/cantusdb_project - -COPY cantusdb_project/requirements.txt /code/django/cantusdb_project - -RUN pip3 install -r requirements.txt +WORKDIR /code/cantusdb_project -COPY cantusdb_project . +# Add our python environment binaries and package files to the path +ENV PATH="$PATH:/code/.venv/bin/" \ + PYTHONPATH="$PYTHONPATH:/code/.venv/lib/python3.9/site-packages/" # define the default command to run when starting the container -CMD ["gunicorn", "--chdir", "cantusdb", "--bind", ":8000", "cantusdb.wsgi:application", "--workers", "5"] \ No newline at end of file +CMD ["gunicorn", "--bind", ":8000", "cantusdb.wsgi:application", "--workers", "5"] \ No newline at end of file diff --git a/django/install-packages.sh b/django/install-packages.sh new file mode 100644 index 000000000..7c489363e --- /dev/null +++ b/django/install-packages.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +pip install poetry==1.8.2 + +poetry config virtualenvs.in-project true # Creates a virtualenv in the project directory +poetry config virtualenvs.options.always-copy true # Ensures no symlinking in the virtualenv +poetry config virtualenvs.options.no-pip true # Don't install pip in the virtualenv +poetry config virtualenvs.options.no-setuptools true # Don't install setuptools in the virtualenv + +if [ $1 = "DEVELOPMENT" ]; then + poetry install --no-cache --with debug,test +else + poetry install --no-cache +fi \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 1bbaa5810..511d8fe2a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,9 +3,10 @@ version: '3' services: django: build: - context: ./django + context: . + dockerfile: ./django/Dockerfile volumes: - - ./:/code/ + - ./django/cantusdb_project:/code/cantusdb_project - static_volume:/resources/static - media_volume:/resources/media - api_cache_volume:/resources/api_cache