dangkhoa2016 commited on
Commit
e933369
Β·
1 Parent(s): 11f6f66

feat: add Docker support with multi-stage build

Browse files

- Add a multi-stage Dockerfile running as a non-root user
- Run migrate and seed automatically on container start
- Exclude .env files from the image

Files changed (5) hide show
  1. .dockerignore +18 -0
  2. Dockerfile +51 -0
  3. README.md +65 -0
  4. docker-entrypoint.sh +58 -0
  5. manual/inspect-docker-data.sh +16 -0
.dockerignore ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ node_modules
2
+ .git
3
+ .gitignore
4
+ .env
5
+ .env.*
6
+ coverage
7
+ temp
8
+ storage/*.db
9
+ storage/*.db-wal
10
+ storage/*.db-shm
11
+ tests
12
+ manual
13
+ .github
14
+ .circleci
15
+ .gitattributes
16
+ .DS_Store
17
+ docker-compose*.yml
18
+ *.md
Dockerfile ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================
2
+ # Builder
3
+ # =========================
4
+ FROM node:22-alpine AS builder
5
+
6
+ RUN apk add --no-cache python3 make g++
7
+
8
+ WORKDIR /app
9
+
10
+ COPY package.json yarn.lock ./
11
+
12
+ RUN yarn install --frozen-lockfile
13
+
14
+ # =========================
15
+ # Runtime
16
+ # =========================
17
+ FROM node:22-alpine
18
+
19
+ LABEL org.opencontainers.image.title="Nodejs JSON API Server"
20
+ LABEL org.opencontainers.image.description="Nodejs JSON API with SQLite, Redis caching, rate limiting, and admin endpoints"
21
+ LABEL org.opencontainers.image.source="https://github.com/anomalyco/Nodejs-JSON-API-Server"
22
+ LABEL org.opencontainers.image.licenses="MIT"
23
+
24
+ WORKDIR /app
25
+
26
+ # Create non-root user
27
+ RUN addgroup -S app && adduser -S app -G app
28
+
29
+ # Copy dependencies
30
+ COPY --from=builder /app/node_modules ./node_modules
31
+
32
+ # Copy application source
33
+ COPY --chown=app:app . .
34
+
35
+ # Prepare runtime directories
36
+ RUN mkdir -p /app/storage \
37
+ && chmod +x /app/docker-entrypoint.sh \
38
+ && chown -R app:app /app/storage
39
+
40
+ USER app
41
+
42
+ ENV NODE_ENV=production \
43
+ DB_PATH=/app/storage/data.db
44
+
45
+ EXPOSE 3000
46
+
47
+ VOLUME ["/app/storage"]
48
+
49
+ ENTRYPOINT ["/app/docker-entrypoint.sh"]
50
+
51
+ CMD ["node", "bin/start.js"]
README.md CHANGED
@@ -30,6 +30,67 @@ npm start # start server
30
  npm run dev
31
  ```
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  > **Note:** Node 22 may show a warning that `node:sqlite` is experimental. This is harmless.
34
 
35
  ---
@@ -325,6 +386,7 @@ json-api-server/
325
  β”‚ β”œβ”€β”€ health.sh # Health endpoint
326
  β”‚ β”œβ”€β”€ inspect-queries.sql # SQL queries for database inspection
327
  β”‚ β”œβ”€β”€ inspect.sh # Database inspection script
 
328
  β”‚ β”œβ”€β”€ photos.sh # Photos endpoints
329
  β”‚ β”œβ”€β”€ posts.sh # Posts endpoints
330
  β”‚ β”œβ”€β”€ todos.sh # Todos endpoints
@@ -332,6 +394,9 @@ json-api-server/
332
  β”œβ”€β”€ manual-test-coverage/
333
  β”‚ β”œβ”€β”€ README.md # Coverage verification documentation
334
  β”‚ └── verify-commit-coverage.sh # Coverage verification script
 
 
 
335
  β”œβ”€β”€ storage/ # SQLite database files (auto-created)
336
  β”œβ”€β”€ temp/ # Temporary files (gitignored)
337
  β”œβ”€β”€ .env # Base configuration (tried first in development β€” highest priority)
 
30
  npm run dev
31
  ```
32
 
33
+ ## Docker
34
+
35
+ ### Build
36
+
37
+ ```bash
38
+ docker build -t json-api-server .
39
+ ```
40
+
41
+ ### Run
42
+
43
+ ```bash
44
+ docker run -d -p 3000:3000 -v ./storage:/app/storage --name json-api-server json-api-server
45
+ ```
46
+
47
+ The entrypoint automatically runs `npm run db:setup` (migrate + seed) on container start.
48
+
49
+ ### Environment Variables
50
+
51
+ ```bash
52
+ docker run -d -p 3000:3000 \
53
+ -e PORT=3000 \
54
+ -e ADMIN_KEY=my-secret-key \
55
+ -e REDIS_HOST=redis \
56
+ -v ./storage:/app/storage \
57
+ --name json-api-server json-api-server
58
+ ```
59
+
60
+ ### Notes
61
+
62
+ - The container runs as a non-root `app` user.
63
+ - Database files persist in `/app/storage` (declared as a `VOLUME`).
64
+ - `NODE_ENV=production` is set by default β€” dotenv is **skipped**, so all config must come via environment variables (see below).
65
+ - `.env` and `.env.*` files are **excluded** by `.dockerignore` and are **not copied** into the image.
66
+ - The entrypoint runs `npm run db:setup` on container start.
67
+
68
+ ### Environment Configuration
69
+
70
+ The container runs with `NODE_ENV=production`, where `src/config/load-env.js` **skips dotenv entirely**. Combined with `.dockerignore` excluding all `.env*` files, you **must** pass configuration through Docker environment variables.
71
+
72
+ **Recommended**: pass variables explicitly:
73
+
74
+ ```bash
75
+ docker run -d -p 3000:3000 \
76
+ -e PORT=3000 \
77
+ -e ADMIN_KEY=my-secret-key \
78
+ -e REDIS_HOST=redis \
79
+ -e SEED_API_BASE_URL=https://jsonplaceholder.typicode.com \
80
+ -v ./storage:/app/storage \
81
+ --name json-api-server json-api-server
82
+ ```
83
+
84
+ **Alternative β€” mount an env file** (only works with `NODE_ENV=production-local`):
85
+
86
+ ```bash
87
+ docker run -d -p 3000:3000 \
88
+ -e NODE_ENV=production-local \
89
+ -v ./.env.prod:/app/.env.prod \
90
+ -v ./storage:/app/storage \
91
+ --name json-api-server json-api-server
92
+ ```
93
+
94
  > **Note:** Node 22 may show a warning that `node:sqlite` is experimental. This is harmless.
95
 
96
  ---
 
386
  β”‚ β”œβ”€β”€ health.sh # Health endpoint
387
  β”‚ β”œβ”€β”€ inspect-queries.sql # SQL queries for database inspection
388
  β”‚ β”œβ”€β”€ inspect.sh # Database inspection script
389
+ β”‚ β”œβ”€β”€ inspect-docker-data.sh # Docker data inspection script
390
  β”‚ β”œβ”€β”€ photos.sh # Photos endpoints
391
  β”‚ β”œβ”€β”€ posts.sh # Posts endpoints
392
  β”‚ β”œβ”€β”€ todos.sh # Todos endpoints
 
394
  β”œβ”€β”€ manual-test-coverage/
395
  β”‚ β”œβ”€β”€ README.md # Coverage verification documentation
396
  β”‚ └── verify-commit-coverage.sh # Coverage verification script
397
+ β”œβ”€β”€ Dockerfile # Docker image definition
398
+ β”œβ”€β”€ docker-entrypoint.sh # Container entrypoint script
399
+ β”œβ”€β”€ .dockerignore # Docker ignore rules
400
  β”œβ”€β”€ storage/ # SQLite database files (auto-created)
401
  β”œβ”€β”€ temp/ # Temporary files (gitignored)
402
  β”œβ”€β”€ .env # Base configuration (tried first in development β€” highest priority)
docker-entrypoint.sh ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ #
3
+ # Docker entrypoint for JSON API Server
4
+ #
5
+ # Runs database initialisation (migrate + seed) before handing off
6
+ # to the main application process. All configuration is sourced
7
+ # from environment variables β€” no .env files are loaded in Docker
8
+ # because NODE_ENV=production skips dotenv entirely.
9
+ #
10
+ # Behaviour can be controlled via:
11
+ # SKIP_DB_SETUP=true β€” skip migrate / seed on container start
12
+ # DEBUG_ENTRYPOINT β€” print diagnostic info for troubleshooting
13
+ #
14
+
15
+ set -eu
16
+
17
+ # ── Logging helpers ──────────────────────────────────────────────
18
+ log() { printf '[Entrypoint] %s\n' "$*"; }
19
+ warn() { printf '[Entrypoint] [WARN] %s\n' "$*" >&2; }
20
+ die() { printf '[Entrypoint] [FATAL] %s\n' "$*" >&2; exit 1; }
21
+
22
+ # ── Diagnostics ──────────────────────────────────────────────────
23
+ log "Node.js $(node --version)"
24
+
25
+ if [ "${DEBUG_ENTRYPOINT:-}" = "true" ]; then
26
+ log "NODE_ENV = ${NODE_ENV:-<unset>}"
27
+ log "DB_PATH = ${DB_PATH:-<unset>}"
28
+ log "ADMIN_KEY = $( [ -n "${ADMIN_KEY:-}" ] && echo 'set' || echo 'not set' )"
29
+ log "REDIS_URL = $( [ -n "${REDIS_URL:-}" ] && echo 'set' || echo 'not set' )"
30
+ fi
31
+
32
+ # ── Environment validation ───────────────────────────────────────
33
+ if [ -z "${DB_PATH:-}" ]; then
34
+ die "DB_PATH is required but was not set. Define it in the Dockerfile or pass it at runtime."
35
+ fi
36
+
37
+ if [ -z "${ADMIN_KEY:-}" ]; then
38
+ warn "ADMIN_KEY is not set β€” admin routes will be disabled at runtime."
39
+ fi
40
+
41
+ # ── Database initialisation ──────────────────────────────────────
42
+ if [ "${SKIP_DB_SETUP:-}" = "true" ]; then
43
+ log "SKIP_DB_SETUP is set β€” skipping database setup."
44
+ else
45
+ log "Running database setup (migrate + seed)..."
46
+
47
+ if ! npm run db:setup; then
48
+ die "Database setup failed. Check the logs above for details."
49
+ fi
50
+
51
+ log "Database setup completed."
52
+ fi
53
+
54
+ # ── Runtime β€” hand off to CMD ────────────────────────────────────
55
+ # exec replaces the shell so that signals (SIGTERM from docker stop)
56
+ # go directly to the Node process, enabling graceful shutdown.
57
+ log "Starting server..."
58
+ exec "$@"
manual/inspect-docker-data.sh ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -e
3
+
4
+ CONTAINER="${1:-json-api-server}"
5
+ DB="${2:-/app/storage/data.db}"
6
+
7
+ echo "[Inspect] Copying scripts to container '$CONTAINER'..."
8
+ docker cp "$(dirname "$0")/inspect.sh" "$CONTAINER:/tmp/inspect.sh"
9
+ docker cp "$(dirname "$0")/inspect-queries.sql" "$CONTAINER:/tmp/inspect-queries.sql"
10
+
11
+ echo "[Inspect] Installing sqlite3 CLI..."
12
+ docker exec -u 0 "$CONTAINER" sh -c "apk add --no-cache sqlite >/dev/null 2>&1"
13
+
14
+ echo "[Inspect] Running inspect.sh against $DB..."
15
+ echo ""
16
+ docker exec -i "$CONTAINER" sh -c "sh /tmp/inspect.sh '$DB'"