You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guides/nodejs/containerize.md
+158Lines changed: 158 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -312,6 +312,161 @@ These updates help ensure your app is easy to deploy, fast to load, and producti
312
312
313
313
### Step 2: Configure the Dockerfile file
314
314
315
+
Before creating a Dockerfile, you need to choose a base image. You can either use the [Node.js Official Image](https://hub.docker.com/_/node) or a Docker Hardened Image (DHI) from the [Hardened Image catalog](https://hub.docker.com/hardened-images/catalog).
316
+
317
+
Choosing DHI offers the advantage of a production-ready image that is lightweight and secure. For more information, see [Docker Hardened Images](https://docs.docker.com/dhi/).
318
+
319
+
{{< tabs >}}
320
+
{{< tab name="Using Docker Hardened Images" >}}
321
+
Docker Hardened Images (DHIs) are available for Node.js on [Docker Hub](https://hub.docker.com/hardened-images/catalog/dhi/node). Unlike using the Docker Official Image, you must first mirror the Node.js image into your organization and then use it as your base image. Follow the instructions in the [DHI quickstart](/dhi/get-started/) to create a mirrored repository for Node.js.
322
+
323
+
Mirrored repositories must start with `dhi-`, for example: `FROM <your-namespace>/dhi-node:<tag>`. In the following Dockerfile, the `FROM` instruction uses `<your-namespace>/dhi-node:22` as the base image.
324
+
325
+
```dockerfile
326
+
# ========================================
327
+
# Optimized Multi-Stage Dockerfile
328
+
# Node.js TypeScript Application (Using DHI)
329
+
# ========================================
330
+
331
+
FROM <your-namespace>/dhi-node:22 AS base
332
+
333
+
# Set working directory
334
+
WORKDIR /app
335
+
336
+
# Create non-root user for security
337
+
RUN addgroup -g 1001 -S nodejs && \
338
+
adduser -S nodejs -u 1001 -G nodejs && \
339
+
chown -R nodejs:nodejs /app
340
+
341
+
# ========================================
342
+
# Dependencies Stage
343
+
# ========================================
344
+
FROM base AS deps
345
+
346
+
# Copy package files
347
+
COPY package*.json ./
348
+
349
+
# Install production dependencies
350
+
RUN --mount=type=cache,target=/root/.npm,sharing=locked \
351
+
npm ci --omit=dev && \
352
+
npm cache clean --force
353
+
354
+
# Set proper ownership
355
+
RUN chown -R nodejs:nodejs /app
356
+
357
+
# ========================================
358
+
# Build Dependencies Stage
359
+
# ========================================
360
+
FROM base AS build-deps
361
+
362
+
# Copy package files
363
+
COPY package*.json ./
364
+
365
+
# Install all dependencies with build optimizations
366
+
RUN --mount=type=cache,target=/root/.npm,sharing=locked \
367
+
npm ci --no-audit --no-fund && \
368
+
npm cache clean --force
369
+
370
+
# Create necessary directories and set permissions
371
+
RUN mkdir -p /app/node_modules/.vite && \
372
+
chown -R nodejs:nodejs /app
373
+
374
+
# ========================================
375
+
# Build Stage
376
+
# ========================================
377
+
FROM build-deps AS build
378
+
379
+
# Copy only necessary files for building (respects .dockerignore)
0 commit comments