From d99f63cf36e5defecc640c15d6f9faca89fa51ba Mon Sep 17 00:00:00 2001 From: nghionpoint Date: Thu, 9 Apr 2026 14:21:30 +0700 Subject: [PATCH 1/3] Add Docker support and improve Dockerfile configuration - Added `DOCKER.md` documentation to guide users on using Docker with the project. - Migrated Dockerfile to use `oven/bun:1-alpine` for performance improvements. - Refined build process and permissions in the container for better compatibility. - Excluded `.idea/` files in `.gitignore`. - Enhanced `.npmignore` to clean redundant blank lines. --- .gitignore | 1 + .npmignore | 29 --------------------- DOCKER.md | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 25 +++++++++++------- 4 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 DOCKER.md diff --git a/.gitignore b/.gitignore index e5d4f0d4..c6310e25 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ product # production /build +.idea/ # misc .DS_Store diff --git a/.npmignore b/.npmignore index 9bec212d..24e8f32f 100644 --- a/.npmignore +++ b/.npmignore @@ -29,32 +29,3 @@ tsconfig.json .next/cache/ .next/standalone/data/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/DOCKER.md b/DOCKER.md new file mode 100644 index 00000000..f96f3ca0 --- /dev/null +++ b/DOCKER.md @@ -0,0 +1,76 @@ +# Docker + +This project ships with a `Dockerfile` for building and running 9Router in a container. + +## Build image + +```bash +docker build -t 9router . +``` + +## Start container + +```bash +docker run --rm \ + -p 20128:20128 \ + -v "$HOME/.9router:/app/data" \ + --name 9router \ + 9router +``` + +The app listens on port `20128` in the container. + +## What the volume does + +```bash +-v "$HOME/.9router:/app/data" +``` + +This keeps your data outside the container so it survives restarts and image rebuilds. + +## Stop container + +```bash +docker stop 9router +``` + +## Run in background + +```bash +docker run -d \ + -p 20128:20128 \ + -v "$HOME/.9router:/app/data" \ + --name 9router \ + 9router +``` + +## View logs + +```bash +docker logs -f 9router +``` + +## Optional environment variables + +You can override runtime env vars with `-e`. + +Example: + +```bash +docker run --rm \ + -p 20128:20128 \ + -v "$HOME/.9router:/app/data" \ + -e PORT=20128 \ + -e HOSTNAME=0.0.0.0 \ + -e DEBUG=true \ + --name 9router \ + 9router +``` + +## Rebuild after code changes + +```bash +docker build -t 9router . +``` + +Then restart the container. diff --git a/Dockerfile b/Dockerfile index 31923ce1..e060dfc1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,20 @@ -FROM node:20-alpine AS builder +# syntax=docker/dockerfile:1.7 +FROM oven/bun:1-alpine AS base WORKDIR /app -COPY package*.json ./ -RUN if [ -f package-lock.json ]; then npm ci --no-audit --no-fund; else npm install --no-audit --no-fund; fi +FROM base AS builder + +RUN apk add --no-cache nodejs npm python3 make g++ linux-headers + +COPY package.json ./ +RUN --mount=type=cache,target=/root/.npm \ + npm install COPY . ./ ENV NEXT_TELEMETRY_DISABLED=1 -RUN npm run build +RUN bun run build:bun -FROM node:20-alpine AS runner +FROM oven/bun:1-alpine AS runner WORKDIR /app LABEL org.opencontainers.image.title="9router" @@ -27,13 +33,14 @@ COPY --from=builder /app/src/mitm ./src/mitm # Standalone node_modules may omit deps only required by the MITM child process. COPY --from=builder /app/node_modules/node-forge ./node_modules/node-forge -RUN mkdir -p /app/data +RUN mkdir -p /app/data && chown -R bun:bun /app # Fix permissions at runtime (handles mounted volumes) -RUN printf '#!/bin/sh\nchown -R node:node /app/data 2>/dev/null; exec su-exec node "$@"\n' > /entrypoint.sh && chmod +x /entrypoint.sh -RUN apk add --no-cache su-exec +RUN apk add --no-cache su-exec && \ + printf '#!/bin/sh\nchown -R bun:bun /app/data 2>/dev/null\nexec su-exec bun "$@"\n' > /entrypoint.sh && \ + chmod +x /entrypoint.sh EXPOSE 20128 ENTRYPOINT ["/entrypoint.sh"] -CMD ["node", "server.js"] +CMD ["bun", "server.js"] From 5d3780cfd25a97a0edd4bc0cdcd1782632e70c30 Mon Sep 17 00:00:00 2001 From: nghionpoint Date: Fri, 10 Apr 2026 10:14:34 +0700 Subject: [PATCH 2/3] Update Docker build process and documentation - Replaced `bun run build:bun` with `npm run build` in Dockerfile for consistency. - Enhanced `DOCKER.md` to include `DATA_DIR` environment variable usage for database persistence. - Clarified paths for container and host data storage. --- DOCKER.md | 21 +++++++++++++++++++-- Dockerfile | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/DOCKER.md b/DOCKER.md index f96f3ca0..2ef89dac 100644 --- a/DOCKER.md +++ b/DOCKER.md @@ -14,6 +14,7 @@ docker build -t 9router . docker run --rm \ -p 20128:20128 \ -v "$HOME/.9router:/app/data" \ + -e DATA_DIR=/app/data \ --name 9router \ 9router ``` @@ -23,10 +24,24 @@ The app listens on port `20128` in the container. ## What the volume does ```bash --v "$HOME/.9router:/app/data" +-v "$HOME/.9router:/app/data" \ +-e DATA_DIR=/app/data ``` -This keeps your data outside the container so it survives restarts and image rebuilds. +`9router` stores its database at `path.join(DATA_DIR, "db.json")`. +Without `DATA_DIR`, the app falls back to the current user's home directory (for example `~/.9router/db.json` on macOS/Linux). In the container, set `DATA_DIR=/app/data` so the bind mount is actually used. + +With the example above, the database file is: + +```text +/app/data/db.json +``` + +and it is persisted on the host at: + +```text +$HOME/.9router/db.json +``` ## Stop container @@ -40,6 +55,7 @@ docker stop 9router docker run -d \ -p 20128:20128 \ -v "$HOME/.9router:/app/data" \ + -e DATA_DIR=/app/data \ --name 9router \ 9router ``` @@ -60,6 +76,7 @@ Example: docker run --rm \ -p 20128:20128 \ -v "$HOME/.9router:/app/data" \ + -e DATA_DIR=/app/data \ -e PORT=20128 \ -e HOSTNAME=0.0.0.0 \ -e DEBUG=true \ diff --git a/Dockerfile b/Dockerfile index e060dfc1..1bb6925c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ RUN --mount=type=cache,target=/root/.npm \ COPY . ./ ENV NEXT_TELEMETRY_DISABLED=1 -RUN bun run build:bun +RUN npm run build FROM oven/bun:1-alpine AS runner WORKDIR /app From 7887f4fd32d55d97102de3fdb34d57b6a6ff516f Mon Sep 17 00:00:00 2001 From: nghionpoint Date: Fri, 10 Apr 2026 10:47:37 +0700 Subject: [PATCH 3/3] Parameterize Bun image and improve package management in Dockerfile - Introduced `BUN_IMAGE` build argument for flexibility in specifying the Bun image version. - Added `apk --no-cache upgrade` for ensuring up-to-date packages. - Streamlined apk package additions for improved dependency management. --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1bb6925c..e5d72cb5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,11 @@ # syntax=docker/dockerfile:1.7 -FROM oven/bun:1-alpine AS base +ARG BUN_IMAGE=oven/bun:1.3.2-alpine +FROM ${BUN_IMAGE} AS base WORKDIR /app FROM base AS builder -RUN apk add --no-cache nodejs npm python3 make g++ linux-headers +RUN apk --no-cache upgrade && apk --no-cache add nodejs npm python3 make g++ linux-headers COPY package.json ./ RUN --mount=type=cache,target=/root/.npm \ @@ -14,7 +15,7 @@ COPY . ./ ENV NEXT_TELEMETRY_DISABLED=1 RUN npm run build -FROM oven/bun:1-alpine AS runner +FROM ${BUN_IMAGE} AS runner WORKDIR /app LABEL org.opencontainers.image.title="9router" @@ -36,7 +37,7 @@ COPY --from=builder /app/node_modules/node-forge ./node_modules/node-forge RUN mkdir -p /app/data && chown -R bun:bun /app # Fix permissions at runtime (handles mounted volumes) -RUN apk add --no-cache su-exec && \ +RUN apk --no-cache upgrade && apk --no-cache add su-exec && \ printf '#!/bin/sh\nchown -R bun:bun /app/data 2>/dev/null\nexec su-exec bun "$@"\n' > /entrypoint.sh && \ chmod +x /entrypoint.sh