Skip to content

Commit 29f24ee

Browse files
gdubickiamimas
authored andcommitted
feat: optimize docker image (#960)
* build: faster building and smaller image locally (if you would ever do this) thanks to not adding to it any files that are not really needed, but may exist on a development machine * build: ~10MB (~11%) smaller image (numbers for ARM64) thanks to not creating pip cache (really) and not installing seemingly unneeded APK packages * build: we only need types packages for development * style: re-arrange dev dependencies in alphabetical order * fix: restore copy command so that docker build works * feat: use multi-stage dockerfile to reduce image size The change also updates dockerignore file ignoring more files from build context --------- Co-authored-by: amimas <aver.mimas@gmail.com>
1 parent c760636 commit 29f24ee

3 files changed

Lines changed: 75 additions & 26 deletions

File tree

.dockerignore

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
1-
/.git
2-
/.github
3-
/.idea
4-
/.overrides
5-
/dev
6-
# only gitlabform directory is needed
7-
/docs
8-
/.gitlabform.egg-info
9-
/tests
10-
/venv
11-
.dockerignore
1+
# Git
2+
.git
123
.gitignore
4+
.git-blame-ignore-revs
5+
6+
# Docker
7+
.dockerignore
8+
Dockerfile
9+
10+
# Python cache and artifacts
11+
*.pyc
12+
__pycache__/
13+
*.egg-info
14+
.mypy_cache/
15+
.pytest_cache/
16+
17+
# Test, dev, and local environments
18+
tests/
19+
dev/
20+
venv/
21+
.venv/
22+
23+
# Documentation
24+
docs/
25+
.overrides/
26+
mkdocs.yml
27+
*.txt
28+
*.md
29+
30+
# CI/CD and project config
31+
.github/
32+
.pre-commit-config.yaml
33+
.coveragerc
34+
codecov.yml
35+
.lgtm.yml
36+
.mypy.ini
37+
coverage.xml
38+
tbump.toml
39+
40+
# Editor/IDE config
41+
.idea/
42+
.vscode/
43+
.cursor/

Dockerfile

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
FROM python:3.12-alpine
2-
RUN apk add --no-cache \
3-
gmp \
4-
lua5.3 \
5-
lua5.3-lpeg
6-
RUN mkdir gitlabform
7-
COPY . /gitlabform
8-
9-
RUN cd gitlabform \
10-
&& apk add --no-cache build-base \
11-
&& pip install -e . \
12-
&& apk --purge del build-base
1+
# ---- Builder Stage ----
2+
FROM python:3.12-alpine AS builder
133

14-
WORKDIR /config
4+
# Set the working directory
5+
WORKDIR /app
6+
7+
# Install build-time dependencies
8+
RUN apk add --no-cache build-base
9+
10+
# Copy dependency definitions to leverage Docker's layer cache
11+
COPY pyproject.toml ./
12+
13+
# Install dependencies. This layer will be cached as long as pyproject.toml does not change.
14+
RUN pip install --no-cache-dir --prefix=/install .
15+
16+
# Now copy the application source code and install the application itself
17+
COPY gitlabform ./gitlabform
18+
# Install the application code itself. Dependencies are already in the cached layer above,
19+
# so we use --no-deps to make it faster and the intent clear.
20+
RUN pip install --no-deps --no-cache-dir --prefix=/install .
1521

22+
# ---- Final Stage ----
23+
FROM python:3.12-alpine AS final
24+
25+
# Create a non-root user for security
1626
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
27+
28+
# Copy the installed artifacts from the builder stage
29+
COPY --from=builder /install /usr/local
30+
31+
# Set the user to the non-root user
1732
USER appuser
33+
34+
# Set the working directory for the application to run
35+
WORKDIR /config

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ dependencies = [
3636
"python-gitlab[graphql]==7.1.0",
3737
"requests==2.32.5",
3838
"ruamel.yaml==0.17.21",
39-
"types-requests==2.32.4.20260107",
40-
"types-setuptools==81.0.0.20260209",
4139
"yamlpath==3.8.2",
4240
]
4341

@@ -62,6 +60,8 @@ test = [
6260
"pytest==9.0.2",
6361
"pytest-cov==7.0.0",
6462
"pytest-rerunfailures==16.1",
63+
"types-requests==2.32.4.20260107",
64+
"types-setuptools==81.0.0.20260209",
6565
"xkcdpass==1.30.0",
6666
]
6767
docs = ["mkdocs", "mkdocs-material"]

0 commit comments

Comments
 (0)