Skip to content

Commit

Permalink
refactor(docker): multi-stage app image build
Browse files Browse the repository at this point in the history
fixes issue with version calculation from a dirty git index during
Docker build time / pip install time

there's no reason to have the .git directory in the app image, same with
all the extra files, tests, .devcontainer, etc.

copies the approach from eligibility-server
  • Loading branch information
thekaveman committed Sep 20, 2024
1 parent bfe2ad4 commit 6aa79e4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
11 changes: 7 additions & 4 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
.github/
.vscode/
**/__pycache__/
.flake8
.*ignore
node_modules/
static/
.coverage
*.db
*.egg-info
*.log
*.mo
.DS_Store
.env
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.env
*fixtures.json
!benefits/core/migrations/local_fixtures.json
*.log
*.mo
*.tfbackend
*.tmp
Expand Down
30 changes: 21 additions & 9 deletions appcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
FROM ghcr.io/cal-itp/docker-python-web:main
FROM ghcr.io/cal-itp/docker-python-web:main AS build_wheel

WORKDIR /build

# upgrade pip
RUN python -m pip install --upgrade pip
RUN python -m pip install --upgrade pip && \
pip install build

# copy source files
COPY . .
RUN git config --global --add safe.directory /build

# build package
RUN python -m build

FROM ghcr.io/cal-itp/docker-python-web:main AS appcontainer

# overwrite default nginx.conf
COPY appcontainer/nginx.conf /etc/nginx/nginx.conf
COPY appcontainer/proxy.conf /calitp/run/proxy.conf

# copy files needed for version metadata
COPY .git .git

# copy source files
# copy runtime files
COPY --from=build_wheel /build/dist /build/dist
COPY manage.py manage.py
COPY bin bin
COPY benefits benefits
COPY pyproject.toml pyproject.toml

RUN pip install -e .
RUN echo "$(find /build/dist -name benefits*.whl)"
# install source as a package
RUN pip install $(find /build/dist -name benefits*.whl)

# ensure $USER can compile messages in the locale directories
USER root
COPY LICENSE LICENSE
#ensure $USER can compile messages in the locale directories
RUN chmod -R 777 benefits/locale
USER $USER

Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Documentation = "https://docs.calitp.org/benefits"
Issues = "https://github.com/cal-itp/benefits/issues"

[build-system]
requires = ["setuptools>=65", "wheel", "setuptools-scm>=8"]
requires = ["setuptools>=65", "setuptools-scm>=8"]
build-backend = "setuptools.build_meta"

[tool.black]
Expand Down Expand Up @@ -78,8 +78,9 @@ markers = [
"request_path: use with session_request to initialize with the given path",
]

[tool.setuptools]
packages = ["benefits"]
[tool.setuptools.packages.find]
include = ["benefits*"]
namespaces = false

[tool.setuptools_scm]
# intentionally left blank, but we need the section header to activate the tool

0 comments on commit 6aa79e4

Please sign in to comment.