Skip to content

Commit

Permalink
Refactor django container build with poetry dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
dchiller committed May 1, 2024
1 parent dc2f6d1 commit 316fa78
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
23 changes: 15 additions & 8 deletions django/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
CMD ["gunicorn", "--bind", ":8000", "cantusdb.wsgi:application", "--workers", "5"]
14 changes: 14 additions & 0 deletions django/install-packages.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 316fa78

Please sign in to comment.