Skip to content

Commit 8d137a9

Browse files
committed
docker reduction
1 parent 8155f12 commit 8d137a9

8 files changed

Lines changed: 66 additions & 55 deletions

File tree

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
app.conf
22
.git
33
.github
4+
tests/
5+
docs/
6+
coverage.xml
7+
sonar-project.properties
8+
LICENSES/
9+
app.test.conf

.github/workflows/ci.yaml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,10 @@ jobs:
210210
username: ${{ github.actor }}
211211
password: ${{ secrets.GITHUB_TOKEN }}
212212

213-
- name: Create version.json
213+
- name: Set release version
214214
run: |
215215
RELEASE_VERSION="${GITHUB_REF#refs/*/}"
216216
echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV
217-
echo "{ \"version\": \"${RELEASE_VERSION}\", \"git_ref\": \"$GITHUB_SHA\"}" > version.json
218217
219218
- name: Extract metadata (tags, labels) for Docker
220219
id: meta
@@ -238,6 +237,8 @@ jobs:
238237
NEW_UID=${{ env.NEW_UID }}
239238
NEW_GID=${{ env.NEW_GID }}
240239
standalone=true
240+
RELEASE_VERSION=${{ env.RELEASE_VERSION }}
241+
GIT_SHA=${{ github.sha }}
241242
242243
- name: Run Trivy vulnerability scanner
243244
uses: aquasecurity/trivy-action@0.35.0
@@ -268,18 +269,6 @@ jobs:
268269
skip-setup-trivy: true
269270

270271
- name: Push Docker image
271-
uses: docker/build-push-action@v7
272272
if: github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
273-
env:
274-
NEW_UID: 1000
275-
NEW_GID: 1000
276-
with:
277-
context: .
278-
file: docker/Dockerfile
279-
push: true
280-
tags: ${{ steps.meta.outputs.tags }}
281-
labels: ${{ steps.meta.outputs.labels }}
282-
build-args: |
283-
NEW_UID=${{ env.NEW_UID }}
284-
NEW_GID=${{ env.NEW_GID }}
285-
standalone=true
273+
run: |
274+
echo "${{ steps.meta.outputs.tags }}" | xargs -I{} docker push {}

.github/workflows/release-package-venv.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
python_version: ['3.11', '3.14']
13-
runs-on: ubuntu-22.04
13+
runs-on: ubuntu-24.04
1414
steps:
1515
- name: Checkout repository
1616
uses: actions/checkout@v6

.github/workflows/release-package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
build:
10-
runs-on: ubuntu-22.04
10+
runs-on: ubuntu-24.04
1111
steps:
1212
- name: Set env
1313
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV &&

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ NEW_UID = 1000
22
NEW_GID = 1000
33

44
ifdef DOCKER
5-
RUN_PREFIX := docker compose run --rm app
5+
RUN_PREFIX := docker compose run --rm app
6+
else ifdef POETRY
7+
RUN_PREFIX := poetry run
68
else
7-
RUN_PREFIX :=
9+
RUN_PREFIX :=
810
endif
911

1012
.SILENT: help
1113
all: help
1214

15+
container-build: ## Build the container
16+
docker compose build --build-arg="NEW_UID=${NEW_UID}" --build-arg="NEW_GID=${NEW_GID}"
17+
18+
container-build-sa: ## Build the container for standalone mode
19+
docker compose build --build-arg="NEW_UID=${NEW_UID}" --build-arg="NEW_GID=${NEW_GID}" --build-arg="standalone=true"
20+
1321
up: ## Start the container
1422
docker compose up
1523

docker/Dockerfile

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# syntax=docker/dockerfile:1
2+
# syntax directive is used to enable Docker BuildKit
23

34
ARG PYTHON_VERSION=3.14
45

@@ -7,8 +8,8 @@ FROM python:${PYTHON_VERSION}-slim AS base
78
ARG PROJECT_DIR="/src"
89
ARG APP_USER="app"
910
ARG APP_GROUP="app"
10-
ARG NEW_UID=1000
11-
ARG NEW_GID=1000
11+
ARG NEW_UID
12+
ARG NEW_GID
1213

1314
ENV PYTHONDONTWRITEBYTECODE=1 \
1415
PYTHONUNBUFFERED=1 \
@@ -23,6 +24,17 @@ RUN groupadd --system ${APP_GROUP} --gid=${NEW_GID} && \
2324
--gid ${NEW_GID} \
2425
${APP_USER}
2526

27+
# Runtime-only system dependencies
28+
RUN apt-get update && \
29+
apt-get install -y --no-install-recommends \
30+
openssl \
31+
libssl3 \
32+
libsodium23 \
33+
postgresql-client \
34+
&& rm -rf /var/lib/apt/lists/*
35+
36+
WORKDIR ${PROJECT_DIR}
37+
2638
# Builder stage for compilation and dependencies
2739
FROM base AS builder
2840

@@ -31,7 +43,6 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
3143
apt-get update && \
3244
apt-get install -y --no-install-recommends \
3345
git \
34-
curl \
3546
ca-certificates \
3647
pkg-config \
3748
libssl-dev \
@@ -51,48 +62,40 @@ RUN git clone --depth 1 --branch v0.9.3 https://github.com/stef/liboprf.git . &&
5162
cd src && \
5263
make
5364

54-
# Install Python dependencies
65+
# Install Python dependencies into a virtualenv
5566
WORKDIR ${PROJECT_DIR}
5667
COPY ./pyproject.toml ./poetry.lock ./
5768

58-
ENV POETRY_CACHE_DIR=/root/.cache/poetry \
59-
POETRY_VIRTUALENVS_CREATE=false
69+
ENV POETRY_CACHE_DIR=/tmp/poetry-cache \
70+
POETRY_VIRTUALENVS_CREATE=true \
71+
POETRY_VIRTUALENVS_IN_PROJECT=true
6072

61-
RUN --mount=type=cache,target=/root/.cache/poetry \
73+
RUN --mount=type=cache,target=${POETRY_CACHE_DIR} \
6274
poetry install --no-root --no-interaction --only main
6375

6476
# Final stage
6577
FROM base AS final
6678

67-
ARG PYTHON_VERSION
68-
69-
# Install only runtime dependencies
70-
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
71-
--mount=type=cache,target=/var/lib/apt,sharing=locked \
72-
apt-get update && \
73-
apt-get install -y --no-install-recommends \
74-
openssl \
75-
curl \
76-
libssl3 \
77-
libsodium23 \
78-
postgresql-client \
79-
gettext-base \
80-
&& rm -rf /var/lib/apt/lists/*
81-
82-
WORKDIR ${PROJECT_DIR}
83-
8479
# Copy compiled libraries
85-
COPY --from=builder --chown=${APP_USER}:${APP_GROUP} /liboprf/src/noise_xk/lib*.so /usr/local/lib/
86-
COPY --from=builder --chown=${APP_USER}:${APP_GROUP} /liboprf/src/lib*.so /usr/local/lib/
80+
COPY --from=builder /liboprf/src/noise_xk/lib*.so /usr/local/lib/
81+
COPY --from=builder /liboprf/src/lib*.so /usr/local/lib/
8782
RUN ldconfig
8883

89-
# Copy Python packages
90-
COPY --chown=${APP_USER}:${APP_GROUP} --from=builder /usr/local/lib/python${PYTHON_VERSION}/site-packages /usr/local/lib/python${PYTHON_VERSION}/site-packages
84+
# Copy only the virtualenv — no pip, no poetry, no build tools
85+
COPY --from=builder --chown=${APP_USER}:${APP_GROUP} /src/.venv /src/.venv
9186

92-
# Copy application
93-
COPY --chown=${APP_USER}:${APP_GROUP} . ${PROJECT_DIR}
87+
# Copy only what the app needs at runtime (no tests, docs, CI artifacts, etc.)
88+
COPY --chown=${APP_USER}:${APP_GROUP} app/ /src/app/
89+
COPY --chown=${APP_USER}:${APP_GROUP} sql/ /src/sql/
90+
COPY --chown=${APP_USER}:${APP_GROUP} tools/ /src/tools/
91+
COPY --chown=${APP_USER}:${APP_GROUP} app.conf.example /src/
92+
93+
# version.json is generated from build args so CI workspace artifacts never leak in
94+
ARG RELEASE_VERSION=dev
95+
ARG GIT_SHA=unknown
96+
RUN echo "{\"version\": \"${RELEASE_VERSION}\", \"git_ref\": \"${GIT_SHA}\"}" > /src/version.json \
97+
&& chown ${APP_USER}:${APP_GROUP} /src/version.json
9498

95-
# Copy and set permissions on entrypoint scripts
9699
COPY --chmod=755 ./docker/init-standalone.sh /usr/local/bin/
97100
COPY --chmod=755 ./docker/init.sh /usr/local/bin/
98101

@@ -106,6 +109,11 @@ RUN if [ "$standalone" = "true" ] ; then \
106109

107110
USER ${APP_USER}
108111

109-
ENV PYTHONPATH=${PROJECT_DIR}
112+
EXPOSE 6502
113+
WORKDIR ${PROJECT_DIR}
114+
115+
ENV PYTHONPATH=${PROJECT_DIR} \
116+
VIRTUAL_ENV=/src/.venv \
117+
PATH="/src/.venv/bin:$PATH"
110118

111119
ENTRYPOINT ["/docker-entrypoint.sh"]

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ python = "^3.11"
1515
uvicorn = "^0.40.0"
1616
fastapi = "^0.129"
1717
async-timeout = "5.0.1"
18-
mypy = "^1.19.1"
1918
pydantic-settings = "^2.13.0"
2019
inject = "^5.3.0"
2120
pycryptodome = "^3.23.0"
@@ -32,11 +31,12 @@ pyoprf = "^0.9.3"
3231
jwcrypto = "^1.5.6"
3332
sqlalchemy = "^2.0.46"
3433
psycopg = "^3.3.2"
35-
testcontainers = "^4.14.1"
3634
puzi = { git = "https://github.com/minvws/puzi-python" }
3735
pyjwt = "^2.11.0"
3836

3937
[tool.poetry.group.dev.dependencies]
38+
mypy = "^1.19.1"
39+
testcontainers = "^4.14.1"
4040
pytest = "^9.0.2"
4141
pytest-cov = "^7.0.0"
4242
pytest-mock = "^3.15.1"

0 commit comments

Comments
 (0)