Skip to content

Commit fbe50d5

Browse files
authored
chore: optimize dockerfile (#61)
* chore: optimize dockerfile * fix: deployment stage missing user and project args * docs: improve docker layer docs
1 parent 9a8389c commit fbe50d5

File tree

2 files changed

+89
-20
lines changed

2 files changed

+89
-20
lines changed

.dockerignore

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,81 @@
1-
# repo and project folders
2-
.devcontainer
3-
.docs
1+
# Version control
42
.git
5-
.github
63
.gitignore
7-
.gitmodules
8-
.idea
9-
.vscode
10-
11-
# docker
12-
Dockerfile
13-
.dockerignore
14-
docker-compose.yaml
15-
16-
# python related
17-
.pyright_cache
18-
.pytest_cache
4+
.gitattributes
5+
6+
# Development containers
7+
.devcontainer/
8+
.vscode/
9+
10+
# Documentation
11+
docs/
12+
*.txt
13+
14+
# Python cache and temporary files
15+
__pycache__/
1916
*.pyc
2017
*.pyo
2118
*.pyd
19+
.Python
20+
.pytest_cache/
21+
.coverage
22+
.coverage.*
23+
.cache
24+
.mypy_cache/
25+
.ruff_cache/
26+
.dmypy.json
27+
dmypy.json
28+
29+
# Virtual environments
30+
.env
31+
.venv
32+
env/
33+
venv/
34+
ENV/
35+
env.bak/
36+
venv.bak/
37+
38+
# Build artifacts
39+
build/
40+
dist/
41+
*.egg-info/
42+
.eggs/
43+
44+
# IDE files
45+
.idea/
46+
.vscode/
47+
*.swp
48+
*.swo
49+
*~
50+
51+
# OS files
52+
.DS_Store
53+
.DS_Store?
54+
._*
55+
.Spotlight-V100
56+
.Trashes
57+
ehthumbs.db
58+
Thumbs.db
59+
60+
# Scripts (if not needed in container)
61+
scripts/
62+
63+
# Log files
64+
*.log
65+
logs/
66+
67+
# Temporary files
68+
tmp/
69+
temp/
70+
.tmp/
71+
.temp/
72+
73+
# Test artifacts
74+
.tox/
75+
.nox/
76+
htmlcov/
77+
.coverage
78+
coverage.xml
79+
*.cover
80+
*.py,cover
81+
.hypothesis/

Dockerfile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ ARG USER=appuser
77

88
ENV RUNTIME_PACKAGES=libpq-dev
99
# These packages will be deleted from the final image, after the application is packaged
10-
ENV BUILD_PACKAGES=gcc
10+
ENV BUILD_PACKAGES="gcc build-essential python3-dev"
1111

1212
RUN apt-get update \
13-
&& apt-get install -y ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \
14-
&& apt-get clean && rm -rf /var/lib/apt/lists/*
13+
&& apt-get install -y --no-install-recommends ${BUILD_PACKAGES} ${RUNTIME_PACKAGES} \
14+
&& apt-get clean \
15+
&& rm -rf /var/lib/apt/lists/* \
16+
&& rm -rf /tmp/* /var/tmp/*
1517

1618
RUN mkdir -p /opt/app/${PROJECT_NAME}
1719

@@ -32,15 +34,20 @@ RUN pip install --upgrade pip \
3234
&& pip install --user uv==${UV_VERSION}
3335

3436
WORKDIR /opt/app/${PROJECT_NAME}
35-
COPY --chown=${USER}:${USER} . .
3637

38+
# Only copy files needed to install dependencies so it can be cached
39+
# (changes to source files won't invalidate cache at this point)
40+
COPY --chown=${USER}:${USER} pyproject.toml uv.lock ./
3741
RUN uv sync --frozen --no-cache --no-install-project --no-default-groups
3842

43+
# Then copy the rest of the application
44+
COPY --chown=${USER}:${USER} . .
3945

4046
# ----
4147
# Devcontainer adds extra tools for development
4248
FROM base AS devcontainer
4349

50+
ARG USER=appuser
4451
USER root
4552

4653
# Add any other tool usefull during development to the following list, this won't be included
@@ -80,6 +87,8 @@ RUN uv build --wheel
8087
# Deployment stage to run in cloud environments. This must be the last stage, which is used to run the application by default
8188
FROM base AS deployment
8289

90+
ARG PROJECT_NAME=python-template
91+
8392
# root is needed to remove build dependencies
8493
USER root
8594
RUN apt-get purge -y ${BUILD_PACKAGES} \

0 commit comments

Comments
 (0)