Skip to content

Commit

Permalink
Optimized production build
Browse files Browse the repository at this point in the history
  • Loading branch information
scheb committed Mar 17, 2024
1 parent 529cc1d commit 4d76fb5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
46 changes: 35 additions & 11 deletions build/production/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
FROM node:20-bookworm-slim as static_build
FROM node:20-bookworm-slim as static-builder

WORKDIR /src
COPY . ./
RUN yarn install && \
yarn build-prod

# define an alias for the specific python version used in this file.
FROM python:3.11-slim-bookworm as python

FROM python:3.11-slim-bookworm
FROM python as python-build-stage

ARG BUILD_ENVIRONMENT=production
ARG APP_HOME=/app
ARG POETRY_VERSION="1.8"

ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV DJANGO_SETTINGS_MODULE=config.settings_prod
ENV PATH="${PATH}:${POETRY_VENV}/bin"
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=1 \
POETRY_CACHE_DIR=/tmp/poetry_cache \
PATH="/opt/venv/bin:${POETRY_VENV}/bin:${PATH}"

# Dependencies
RUN apt-get update && \
apt-get -y install default-libmysqlclient-dev pkg-config build-essential git && \
apt-get -y install default-libmysqlclient-dev pkg-config build-essential && \
pip install poetry==${POETRY_VERSION}

WORKDIR ${APP_HOME}

# Install Python dependencies
COPY poetry.lock pyproject.toml ./
RUN poetry config virtualenvs.create false && \
poetry install
RUN poetry install --without dev && \
rm -rf $POETRY_CACHE_DIR

FROM python as python-run-stage

ARG APP_HOME=/app

ENV VIRTUAL_ENV=/app/.venv \
PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
DJANGO_SETTINGS_MODULE=config.settings_prod

# Dependencies
RUN apt-get update && \
apt-get -y install default-libmysqlclient-dev git && \
# cleaning up unused files
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -rf /var/lib/apt/lists/*

# Copy python dependency wheels from python-build-stage
COPY --from=python-build-stage ${VIRTUAL_ENV} ${VIRTUAL_ENV}

WORKDIR ${APP_HOME}

# Copy application code to WORKDIR
COPY ./bin/ ./bin/
Expand All @@ -37,7 +61,7 @@ COPY ./web_app/ ./web_app/
COPY ./manage.py ./

# Get the generated static assets
COPY --from=static_build /src/bundles/ ./bundles/
COPY --from=static-builder /src/bundles/ ./bundles/

COPY --chmod=755 ./build/production/entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion build/production/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ set -o nounset
# Collect all static images
python manage.py collectstatic --no-input

exec /usr/local/bin/gunicorn config.asgi --bind 0.0.0.0:5000 --chdir=/app -k uvicorn.workers.UvicornWorker
exec gunicorn config.asgi --bind 0.0.0.0:5000 --chdir=/app -k uvicorn.workers.UvicornWorker
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "beer-analytics"
version = "1.0"
description = "Beer Analytics website project"
authors = ["Christian Scheb <me@christianscheb.de>"]
package-mode = false

[tool.poetry.dependencies]
python = "~3.11"
Expand Down

0 comments on commit 4d76fb5

Please sign in to comment.