Skip to content

Commit c53555a

Browse files
authored
Merge pull request #137 from CommonsEngine/docker-x
Fix Docker Setup
2 parents 23798e0 + 0cf3b4c commit c53555a

File tree

6 files changed

+102
-70
lines changed

6 files changed

+102
-70
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
name: Docker Publish
1+
name: Docker Release
22

33
on:
44
push:
5-
tags: ["*"]
6-
release:
7-
types: [published]
5+
tags:
6+
- "v*"
87
workflow_dispatch:
98

109
env:
@@ -23,6 +22,9 @@ jobs:
2322
- name: Checkout repository
2423
uses: actions/checkout@v4
2524

25+
- name: Set up QEMU
26+
uses: docker/setup-qemu-action@v3
27+
2628
- name: Set up Docker Buildx
2729
uses: docker/setup-buildx-action@v3
2830

@@ -39,15 +41,23 @@ jobs:
3941
with:
4042
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
4143
tags: |
44+
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
45+
type=semver,pattern={{major}}.{{minor}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
46+
type=semver,pattern={{major}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
4247
type=ref,event=tag
43-
type=sha
44-
type=raw,value=latest,enable={{is_default_branch}}
48+
type=raw,value=latest
4549
4650
- name: Build and push image
4751
uses: docker/build-push-action@v6
4852
with:
4953
context: ${{ env.BUILD_CONTEXT }}
5054
file: ${{ env.DOCKERFILE }}
5155
push: true
56+
platforms: linux/amd64,linux/arm64
5257
tags: ${{ steps.meta.outputs.tags }}
5358
labels: ${{ steps.meta.outputs.labels }}
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max
61+
build-args: |
62+
BUILD_SHA=${{ github.sha }}
63+
BUILD_TAG=${{ github.ref_name }}

Dockerfile

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,86 @@
11
# syntax=docker/dockerfile:1.7
22

3-
# ---------- Base dependencies ----------
4-
FROM node:22-bookworm-slim AS base
3+
ARG NODE_IMAGE=node:22-bookworm-slim
4+
5+
# ---------- Base image with tooling ----------
6+
FROM ${NODE_IMAGE} AS base
57

6-
# set workdir and install common deps
78
WORKDIR /app
9+
810
RUN --mount=type=cache,target=/var/cache/apt \
911
apt-get update \
10-
&& apt-get install -y --no-install-recommends ca-certificates curl git tini openssl \
12+
&& apt-get install -y --no-install-recommends ca-certificates curl git openssh-client tini \
1113
&& rm -rf /var/lib/apt/lists/*
1214

1315
# Use Corepack to pin Yarn deterministically
1416
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
1517
RUN corepack enable && corepack prepare [email protected] --activate
1618

17-
# ---------- Dependencies layer ----------
18-
FROM base AS deps
19-
20-
# Copy package manager files
21-
COPY package.json yarn.lock ./
22-
23-
# IMPORTANT: skip lifecycle scripts here (prevents running prepare/build before sources exist)
24-
# RUN --mount=type=cache,target=/usr/local/share/.cache/yarn \
25-
# yarn install --frozen-lockfile --ignore-scripts
19+
# ---------- Builder ----------
20+
FROM base AS build
2621

27-
# Disable optional deps (argon2 has native build, still install)
28-
RUN yarn install --frozen-lockfile
22+
ARG BUILD_SHA=dev
23+
ARG BUILD_TAG=local
2924

30-
# ---------- Build layer ----------
31-
FROM deps AS build
32-
33-
# Reuse node_modules from deps
34-
COPY --from=deps /app/node_modules ./node_modules
35-
# Bring in source (exclude by .dockerignore later)
25+
# Install dependencies (cached). Force dev deps to be present even though we later build for production.
26+
COPY package.json yarn.lock ./
3627
COPY . .
28+
RUN --mount=type=cache,target=/usr/local/share/.cache/yarn \
29+
NODE_ENV=development yarn install --frozen-lockfile
3730

38-
# Generate prisma client & build sources
39-
RUN yarn prisma generate \
40-
&& yarn build
31+
# Prepare Prisma schema, migrations, seeds (writes sqlite DB under /app/data)
32+
ENV NODE_ENV=production
33+
ENV DATABASE_URL=file:/app/data/sovereign.db
34+
RUN mkdir -p /app/data \
35+
&& yarn prepare:all
4136

42-
# ---------- Production runtime ----------
43-
FROM node:22-bookworm-slim AS runtime
37+
# Build workspaces and manifest/openapi (prebuild hook regenerates manifest)
38+
RUN NODE_ENV=production yarn build
4439

40+
# ---------- Runtime ----------
41+
FROM ${NODE_IMAGE} AS runtime
42+
ARG BUILD_SHA=dev
43+
ARG BUILD_TAG=local
4544
WORKDIR /app
45+
4646
ENV NODE_ENV=production
47-
ENV PORT=5000
47+
ENV PORT=4000
4848
ENV DATABASE_URL="file:/app/data/sovereign.db"
4949

50-
# Corepack for Yarn in runtime too (optional if you don't use yarn here)
50+
# Corepack for runtime (keeps Yarn available for scripts)
5151
ENV COREPACK_ENABLE_DOWNLOAD_PROMPT=0
52-
RUN corepack enable && corepack prepare [email protected] --activate
53-
54-
# Bring built app and prisma **artifacts** (client + engine) from build
55-
# Copy the platform app since the runtime entry is /platform/index.cjs
56-
COPY --from=build /app/platform ./platform
57-
COPY --from=build /app/prisma ./prisma
58-
59-
# Copy Prisma client output generated during build (so we don't need prisma CLI now)
60-
COPY --from=build /app/node_modules/@prisma ./node_modules/@prisma
61-
COPY --from=build /app/node_modules/.prisma ./node_modules/.prisma
52+
RUN --mount=type=cache,target=/var/cache/apt \
53+
apt-get update \
54+
&& apt-get install -y --no-install-recommends ca-certificates tini \
55+
&& rm -rf /var/lib/apt/lists/* \
56+
&& corepack enable && corepack prepare [email protected] --activate
6257

63-
# Copy node_modules from build (matches built artifacts and avoids registry lookups)
64-
COPY --from=build /app/node_modules ./node_modules
58+
LABEL org.opencontainers.image.revision="${BUILD_SHA}" \
59+
org.opencontainers.image.version="${BUILD_TAG}"
6560

66-
# Copy build artifacts and required folders
61+
# Copy built artifacts and runtime assets
6762
COPY --from=build /app/package.json ./package.json
63+
COPY --from=build /app/yarn.lock ./yarn.lock
64+
COPY --from=build /app/node_modules ./node_modules
65+
COPY --from=build /app/platform ./platform
66+
COPY --from=build /app/plugins ./plugins
67+
COPY --from=build /app/tools ./tools
68+
COPY --from=build /app/data ./data
69+
COPY --from=build /app/platform/prisma ./prisma
70+
COPY --from=build /app/manifest.json ./manifest.json
71+
COPY --from=build /app/openapi.json ./openapi.json
6872

69-
# Prepare persistent data directory for sqlite
70-
RUN mkdir -p /app/data \
71-
&& chown node:node /app/data
73+
VOLUME ["/app/data"]
7274

73-
# Copy entrypoint script
75+
# Entry + runtime prep
7476
COPY docker/entrypoint.sh /entrypoint.sh
75-
RUN chmod +x /entrypoint.sh
77+
RUN chmod +x /entrypoint.sh \
78+
&& mkdir -p /app/data \
79+
&& chown -R node:node /app/data
7680

7781
USER node
7882

79-
EXPOSE 5000
83+
EXPOSE 4000
8084

8185
ENTRYPOINT ["/usr/bin/tini", "--", "/entrypoint.sh"]
8286
CMD ["node", "platform/index.cjs"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ IMAGE_NAME ?= sovereign
33
IMAGE_VERSION ?= local
44
DOCKERFILE ?= Dockerfile
55
BUILD_CONTEXT ?= .
6-
HOST_PORT ?= 5000
7-
CONTAINER_PORT ?= 5000
6+
HOST_PORT ?= 4000
7+
CONTAINER_PORT ?= 4000
88
DATA_DIR ?= $(CURDIR)/data
99
ENV_FILE ?= .env
1010
CONTAINER_NAME ?= sovereign

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,25 +387,28 @@ Environment variables come from your shell or an external manager (e.g., `/etc/p
387387

388388
#### Docker Setup
389389

390-
> ⚠️ Docker setup is being revamped to accomodate new architecture changes.
391-
392-
A multi-stage `Dockerfile` is provided to build and run Sovereign from a container. The image bundles the production build and Prisma client; SQLite data is stored under `/app/data`.
390+
A multi-stage `Dockerfile` is provided. The image runs `yarn prepare:all` during build (migrations + seeds + manifest/openapi) and ships a pre-seeded SQLite DB at `/app/data/sovereign.db`. Port 4000 is the default.
393391

394392
##### Build & run locally
395393

396394
```bash
397395
docker build -t sovereign:local .
398396
docker rm -f sovereign 2>/dev/null || true
399-
mkdir -p ./data
400-
# run with mounted volume for sqlite persistence
401-
docker run --rm \
397+
docker volume rm sovereign-data 2>/dev/null || true
398+
docker volume create sovereign-data
399+
400+
docker run -d --name sovereign \
402401
-p 4000:4000 \
403-
-v $(pwd)/data:/app/data \
404-
--env-file .env \
402+
-e PORT=4000 \
403+
-v sovereign-data:/app/data \
405404
sovereign:local
405+
406406
docker logs -f sovereign
407407
```
408408

409+
- First run populates the volume with the baked DB (includes non-dev test user `[email protected] / ffp@2025`).
410+
- If you bind-mount `./data`, ensure it exists and is writable; an empty mount hides the baked DB.
411+
409412
##### Publish to GHCR
410413

411414
Either you can directoy push to `ghcr` or you can simply tag version from the `main` branch, and it will automatically picked up by GitHub Actions and publish to `ghcr`.

docker/entrypoint.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
#!/usr/bin/env sh
22
set -eu
33

4-
# Ensure data directory exists (should be mounted volume)
5-
mkdir -p /app/data
4+
APP_DIR="/app"
5+
DATA_DIR="${APP_DIR}/data"
6+
PRISMA_BIN="${APP_DIR}/node_modules/.bin/prisma"
7+
SCHEMA_PATH="${APP_DIR}/prisma/schema.prisma"
68

7-
# Run prisma migrations (for sqlite this will just ensure schema); ignore failure to keep backwards compat
8-
yarn prisma db push --accept-data-loss >/tmp/prisma.log 2>&1 || {
9-
echo "[entrypoint] prisma db push failed; see /tmp/prisma.log" >&2
10-
}
9+
mkdir -p "$DATA_DIR"
10+
: "${DATABASE_URL:=file:/app/data/sovereign.db}"
11+
export DATABASE_URL
12+
13+
if [ ! -x "$PRISMA_BIN" ]; then
14+
echo "[entrypoint] Prisma CLI not found at ${PRISMA_BIN}" >&2
15+
exit 1
16+
fi
17+
18+
echo "[entrypoint] Applying migrations (DATABASE_URL=${DATABASE_URL})"
19+
if ! "$PRISMA_BIN" migrate deploy --schema "$SCHEMA_PATH" >/tmp/prisma.log 2>&1; then
20+
echo "[entrypoint] migrate deploy failed, falling back to db push (see /tmp/prisma.log)" >&2
21+
"$PRISMA_BIN" db push --accept-data-loss --schema "$SCHEMA_PATH" >/tmp/prisma.log 2>&1 || {
22+
echo "[entrypoint] prisma db push failed; see /tmp/prisma.log" >&2
23+
exit 1
24+
}
25+
fi
1126

1227
exec "$@"

docs/CLI.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Performs **first‑run detection**:
5555
sv serve --force
5656
5757
# Fast restart with a custom port probe
58-
sv serve --port 5000
58+
sv serve --port 4000
5959
```
6060

6161
### `sv serve rebuild [--no-health] [--port <n>] [--ecosystem <path>]`

0 commit comments

Comments
 (0)