1- # - Stage 1: Builder -----------------------------------------------------------
2-
1+ # ------------------- -----------------------------------------------------------
2+ # Stage 1: Builder
33# This stage builds the application and its dependencies.
4+ # ------------------------------------------------------------------------------
45FROM node:jod-alpine AS builder
56
67WORKDIR /app
@@ -24,9 +25,10 @@ RUN npm run build && \
2425 npm run swagger:docs && \
2526 npm prune --omit=dev
2627
27- # - Stage 2: Runtime -----------------------------------------------------------
28-
28+ # ------------------- -----------------------------------------------------------
29+ # Stage 2: Runtime
2930# This stage creates the final, minimal image to run the application.
31+ # ------------------------------------------------------------------------------
3032FROM node:jod-alpine AS runtime
3133
3234WORKDIR /app
@@ -37,23 +39,21 @@ LABEL org.opencontainers.image.description="Proof of Concept for a RESTful API m
3739LABEL org.opencontainers.image.licenses="MIT"
3840LABEL org.opencontainers.image.source="https://github.com/nanotaboada/ts-node-samples-express-restful"
3941
40- # Install the SQLite runtime libraries, add a non-root user for security
41- # hardening, and set the ownership of the /app directory to this user.
42- RUN apk add --no-cache sqlite-libs && \
43- adduser -D -g "" express && \
44- chown -R express:express /app
42+ # Copy README and assets (read-only): often displayed in container registries such as Docker Hub or GHCR
43+ COPY --chown=root:root --chmod=644 README.md ./
44+ COPY --chown=root:root --chmod=755 assets/ ./assets/
4545
46- # Copy transpiled JavaScript, pruned node_modules, SQLite database and Swagger JSON.
47- COPY --from=builder --chown=express:express /app/dist ./dist
48- COPY --from=builder --chown=express:express /app/node_modules ./dist/node_modules
49- COPY --from=builder --chown=express:express /app/src/data/players-sqlite3.db ./dist/data/players-sqlite3.db
50- COPY --from=builder --chown=express:express /app/dist/swagger.json ./dist/swagger.json
46+ # Install SQLite runtime libraries and create a system-like user (express) for running the app.
47+ RUN apk add --no-cache sqlite-libs && \
48+ adduser -D -g "" express
5149
52- # Copy README and assets to the root of the app.
53- # This is often displayed in registries like GitHub Container Registry.
54- COPY README.md ./
55- COPY assets/ ./assets/
50+ # Copy transpiled JavaScript, pruned node_modules, SQLite database and Swagger JSON, owned by express
51+ COPY --from=builder --chown=express:express --chmod=755 /app/dist ./dist
52+ COPY --from=builder --chown=express:express --chmod=755 /app/node_modules ./dist/node_modules
53+ COPY --from=builder --chown=express:express --chmod=644 /app/dist/swagger.json ./dist/swagger.json
54+ COPY --from=builder --chown=express:express --chmod=664 /app/src/data/players-sqlite3.db ./dist/data/players-sqlite3.db
5655
56+ # Drop privileges: run as express
5757USER express
5858
5959EXPOSE 9000
0 commit comments