11ARG PYTHON_VERSION=3.11-slim
22
3- FROM python:${ PYTHON_VERSION}
3+ FROM python:$PYTHON_VERSION
44
5- # Install virtualenv and create a virtual environment
6- RUN pip install --no-cache-dir -U virtualenv>=20.13.1 && virtualenv /env --python=python3.11
7- ENV PATH /env/bin:$PATH
5+ # Set shell with pipefail for safer pipe operations
6+ SHELL ["/bin/bash" , "-o" , "pipefail" , "-c" ]
87
9- # Install pip requirements
10- WORKDIR /app
11- COPY . .
12- RUN /env/bin/pip install --no-cache-dir . && rm nginx.conf
8+ # Define where Poetry virtual environments will be stored
9+ ARG POETRY_VIRTUALENVS_PATH=/opt/pypoetry/virtualenvs
10+
11+ # Ensure that the python output is sent straight to terminal (e.g. your container log)
12+ # without being first buffered and that you can see the output of your application (e.g. django logs)
13+ # in real time. Equivalent to python -u: https://docs.python.org/3/using/cmdline.html#cmdoption-u
14+ ENV PYTHONUNBUFFERED=1
15+
16+ # Prevent Python from writing .pyc files to disc
17+ # https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
18+ ENV PYTHONDONTWRITEBYTECODE=1
1319
1420# Install make, nginx and copy configuration
1521RUN apt-get update \
@@ -20,20 +26,26 @@ RUN apt-get update \
2026RUN apt-get update && apt-get install -y postgresql postgresql-contrib
2127COPY nginx.conf /etc/nginx/nginx.conf
2228
23- # Prevents Python from writing .pyc files to disc
24- # https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
25- ENV PYTHONDONTWRITEBYTECODE 1
29+ # Install Poetry and add it to PATH so its commands can be executed
30+ # from anywhere, without specifying the full path to its executable.
31+ RUN curl -sSL https://install.python-poetry.org | python3 - --version 2.1.3
32+ ENV PATH="/root/.local/bin:$PATH"
2633
27- # Ensures that the python output is sent straight to terminal (e.g. your container log)
28- # without being first buffered and that you can see the output of your application (e.g. django logs)
29- # in real time. Equivalent to python -u: https://docs.python.org/3/using/cmdline.html#cmdoption-u
30- ENV PYTHONUNBUFFERED 1
34+ # Create the folder where Poetry virtual environments will be stored and make it
35+ # accessible to all users. This is needed by the 'www-data' user during server startup
36+ RUN mkdir -p $POETRY_VIRTUALENVS_PATH && chmod 755 $POETRY_VIRTUALENVS_PATH
37+ ENV POETRY_VIRTUALENVS_PATH=$POETRY_VIRTUALENVS_PATH
38+
39+ # Copy and install project
40+ WORKDIR /app
41+ COPY . .
42+ RUN poetry install --only main && rm nginx.conf
3143
32- # Copy app, generate static and set permissions
33- RUN /env/bin/ python manage.py collectstatic --no-input --settings=backend.settings.base && \
44+ # Generate static and set permissions
45+ RUN poetry run python manage.py collectstatic --no-input --settings=backend.settings.base && \
3446 chown -R www-data:www-data /app
3547
3648# Expose and run app
3749EXPOSE 80
3850STOPSIGNAL SIGKILL
39- CMD ["/app/start-server.sh" ]
51+ CMD ["poetry" , "run" , " /app/start-server.sh" ]
0 commit comments