# NestJS Dockerfile Generator

URL: /dockerizer/nestjs

Generate a production Dockerfile for a NestJS API — compiled to dist/, dev dependencies pruned, running non-root.

## Default configuration

- `appName` (App name): nestjs — Used for the image tag, the compose service and the OCI labels.

- `port` (Port): 3000 — The port the app listens on inside the container. Keep it above 1024 so the process can bind it without root.

- `nodeVersion` (Node version): 24 — Node 24 is the active LTS line. Node 20 goes end-of-life in April 2026.

- `packageManager` (Package manager): npm — pnpm and Yarn are installed through Corepack, which reads the `packageManager` field in your package.json.

- `baseVariant` (Base image): alpine — Alpine is the smallest. Debian slim is safest if you compile native modules such as sharp, canvas or bcrypt.

- `database` (Database service): none — Adds the database to docker-compose.yml with a healthcheck, a named volume and a DATABASE_URL wired into the app.

- `redis` (Redis service): false — Adds Redis to docker-compose.yml and exposes REDIS_URL to the app.

- `cacheMounts` (BuildKit cache mounts): true — Persists the package manager store between builds. Repeat builds skip the download entirely.

- `multiArch` (Multi-architecture build): false — Adds BUILDPLATFORM/TARGETARCH so `docker buildx build --platform linux/amd64,linux/arm64` cross-compiles natively.

- `healthcheck` (Healthcheck): true — Adds a HEALTHCHECK so orchestrators can restart an unresponsive container.

- `tini` (tini init): false — Runs the app under tini so signals and zombie processes are handled properly.

- `ociLabels` (OCI labels): false — Adds org.opencontainers.image.* metadata to the final image.

- `buildSecret` (Build secret mount): false — Reads a private registry token via --mount=type=secret so it never lands in an image layer.

## Generated files

### Dockerfile

Multi-stage build: dependencies, compilation and the runtime image are separate, so only what the app needs at runtime ships.

```docker
# syntax=docker/dockerfile:1
# Generated by Easypanel Dockerizer — https://easypanel.io/dockerizer

# --- Base ---------------------------------------------
FROM node:24-alpine AS base
WORKDIR /app

# --- Dependencies -------------------------------------
FROM base AS deps

# Manifests first: source changes must not bust the dependency cache
COPY package.json package-lock.json* ./
RUN --mount=type=cache,id=npm-store,target=/root/.npm,sharing=locked \
    npm ci --include=dev

# --- Production dependencies --------------------------

# Strips dev dependencies from the tree the runtime image copies
FROM deps AS prod-deps
RUN npm prune --omit=dev

# --- Build --------------------------------------------
FROM base AS build
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NODE_ENV=production
RUN npm run build

# --- Runtime ------------------------------------------
FROM node:24-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
COPY --from=prod-deps --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY --from=build --chown=node:node /app/package.json ./package.json
USER node
EXPOSE 3000
STOPSIGNAL SIGTERM

# Lets Docker, Compose and Easypanel see when the app is wedged
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD ["node", "-e", "fetch('http://127.0.0.1:3000/').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
CMD ["node", "dist/main.js"]

```

### .dockerignore

Keeps the build context small and stops secrets and local dependencies from reaching an image layer.

```bash
# Version control
.git
.gitignore
.github

# Secrets — never bake these into an image layer
.env
.env.*
!.env.example
*.pem
*.key

# Editor and OS noise
.vscode
.idea
.DS_Store
Thumbs.db

# Docs and local tooling
README.md
LICENSE
docs
.editorconfig
docker-compose*.yml
Dockerfile*
.dockerignore

# Dependencies — reinstalled inside the image from the lockfile
node_modules
**/node_modules
.pnpm-store
.yarn/cache
.yarn/unplugged
npm-debug.log*
yarn-error.log*
pnpm-debug.log*

# Test and coverage output
coverage
.nyc_output
**/*.test.ts
**/*.spec.ts

```

### docker-compose.yml

Runs the image locally with its backing services, wired together and health-gated.

```yaml
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      PORT: 3000

```

## Frequently asked questions

### The healthcheck fails even though the app is up. Why?

Nest returns 404 on `/` unless you have a root route. Point the healthcheck at an endpoint that exists — `@nestjs/terminus` on `/health` is the usual choice.

### My app uses a global prefix — does that matter?

Only for the healthcheck path. `setGlobalPrefix('api')` moves every route, so the probe should target `/api/health` rather than `/`.

## Related generators

- [Node.js](/dockerizer/nodejs)

- [Express](/dockerizer/express)

- [Fastify](/dockerizer/fastify)

- [Next.js](/dockerizer/nextjs)