Skip to content

Commit c569549

Browse files
authored
Refactor: Multi-stage Docker build for app image (#2392)
2 parents 1b92352 + 34ecd8a commit c569549

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ USER root
66
RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
77
USER $USER
88

9+
COPY . .
910
# install devcontainer requirements
1011
RUN pip install -e .[dev,test]
1112

1213
# docs requirements are in a separate file for the GitHub Action
13-
COPY docs/requirements.txt docs/requirements.txt
1414
RUN pip install -r docs/requirements.txt

.dockerignore

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
.github/
2-
.vscode/
31
**/__pycache__/
4-
.flake8
5-
.*ignore
2+
node_modules/
3+
static/
4+
.coverage
65
*.db
76
*.egg-info
7+
*.log
8+
*.mo
9+
.DS_Store
10+
.env

appcontainer/Dockerfile

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1-
FROM ghcr.io/cal-itp/docker-python-web:main
1+
# multi-stage build
2+
#
3+
# stage 1: builds the benefits package from source
4+
# using the git metadata for version info
5+
FROM ghcr.io/cal-itp/docker-python-web:main AS build_wheel
6+
7+
WORKDIR /build
28

39
# upgrade pip
4-
RUN python -m pip install --upgrade pip
10+
RUN python -m pip install --upgrade pip && \
11+
pip install build
12+
13+
# copy source files
14+
COPY . .
15+
RUN git config --global --add safe.directory /build
16+
17+
# build package
18+
RUN python -m build
19+
20+
# multi-stage build
21+
#
22+
# stage 2: installs the benefits package in a fresh base container
23+
# using the pre-built package, and copying only needed source
24+
FROM ghcr.io/cal-itp/docker-python-web:main AS appcontainer
525

626
# overwrite default nginx.conf
727
COPY appcontainer/nginx.conf /etc/nginx/nginx.conf
828
COPY appcontainer/proxy.conf /calitp/run/proxy.conf
929

10-
# copy files needed for version metadata
11-
COPY .git .git
12-
13-
# copy source files
30+
# copy runtime files
31+
COPY --from=build_wheel /build/dist /build/dist
1432
COPY manage.py manage.py
1533
COPY bin bin
1634
COPY benefits benefits
17-
COPY pyproject.toml pyproject.toml
1835

19-
RUN pip install -e .
36+
# install source as a package
37+
RUN pip install $(find /build/dist -name benefits*.whl)
2038

21-
# ensure $USER can compile messages in the locale directories
2239
USER root
40+
COPY LICENSE LICENSE
41+
#ensure $USER can compile messages in the locale directories
2342
RUN chmod -R 777 benefits/locale
2443
USER $USER
2544

pyproject.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ readme = "README.md"
66
license = { file = "LICENSE" }
77
classifiers = ["Programming Language :: Python :: 3 :: Only"]
88
requires-python = ">=3.9"
9+
maintainers = [
10+
{ name = "Compiler LLC", email = "[email protected]" }
11+
]
912
dependencies = [
1013
"Authlib==1.3.2",
1114
"azure-keyvault-secrets==4.8.0",
@@ -39,12 +42,14 @@ test = [
3942
]
4043

4144
[project.urls]
45+
Changelog = "https://github.com/cal-itp/benefits/releases"
4246
Code = "https://github.com/cal-itp/benefits"
4347
Documentation = "https://docs.calitp.org/benefits"
48+
Homepage = "https://www.calitp.org/#initiatives-benefits"
4449
Issues = "https://github.com/cal-itp/benefits/issues"
4550

4651
[build-system]
47-
requires = ["setuptools>=65", "wheel", "setuptools-scm>=8"]
52+
requires = ["setuptools>=65", "setuptools-scm>=8"]
4853
build-backend = "setuptools.build_meta"
4954

5055
[tool.black]
@@ -79,8 +84,9 @@ markers = [
7984
"request_path: use with session_request to initialize with the given path",
8085
]
8186

82-
[tool.setuptools]
83-
packages = ["benefits"]
87+
[tool.setuptools.packages.find]
88+
include = ["benefits*"]
89+
namespaces = false
8490

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

0 commit comments

Comments
 (0)