Skip to content

Commit

Permalink
Use multi-stage build with pdm for production
Browse files Browse the repository at this point in the history
  • Loading branch information
mrharpo committed Nov 13, 2023
1 parent 00e4d76 commit 3a2e4cb
Show file tree
Hide file tree
Showing 3 changed files with 549 additions and 1,599 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.pdm.toml
.pdm.toml
__pycache__
25 changes: 18 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
###########################
# 'base' build stage, common to all build stages
###########################
FROM python:3.11 as base
FROM python:3.11-slim as base

# Set working dir to /app, where all Chowda code lives.
WORKDIR /app
RUN pip install -U pip
RUN pip install -U pip pdm

# Copy app code to container
COPY pyproject.toml pdm.lock README.md ./
Expand All @@ -20,8 +20,6 @@ COPY migrations migrations
# 'dev' build stage
###########################
FROM base as dev
# Install PDM dependency manager
RUN pip install pdm
# Configure pdm to instal dependencies into ./__pypyackages__/
RUN pdm config python.use_venv false
# Configure python to use pep582 with local __pypyackages__
Expand Down Expand Up @@ -59,11 +57,24 @@ CMD poetry run locust
###########################
# 'production' build stage
############################
FROM base as production
RUN pip install .[production]
FROM base as build
# build-essential libpq-dev python3-dev
RUN apt update && apt install -y gcc libpq-dev git
RUN pdm install -G production

COPY static static
FROM python:3.11-slim as production
WORKDIR /app

RUN apt update && apt install -y libpq-dev
RUN apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
COPY --from=build /app/ /app/
COPY templates templates
COPY static static

ENV CHOWDA_ENV=production
ENV PATH=$PATH:/app/.venv/bin/

EXPOSE 8000
CMD gunicorn chowda.app:app -b 0.0.0.0:8000 -w 2 --worker-class uvicorn.workers.UvicornWorker
Loading

0 comments on commit 3a2e4cb

Please sign in to comment.