Blog

2 min read

How to Deploy a Nuxt 4 App with Docker

Build a Nuxt 4 app with pnpm, copy Nitro’s .output into a slim Docker image, and run the server as a non-root user.

Ask my AI assistant
Michael Hoffmann

Michael Hoffmann

@mokkapps

How to Deploy a Nuxt 4 App with Docker Image

The Dockerfile in my portfolio website builds Nuxt in one stage and copies the .output directory into a separate Node.js image. That keeps pnpm and the compiler in the build stage. I use the same pattern here for a smaller Nuxt 4 app.

Build the Nitro Server

This example uses Node.js 24 and pnpm. Commit your pnpm-lock.yaml and pin the pnpm version in the packageManager field of package.json. The install step also reads pnpm-workspace.yaml, where pnpm records your decisions about dependency build scripts. My test app uses the following setting for esbuild.

pnpm-workspace.yaml
allowBuilds:
  esbuild: false

Add this Dockerfile to your project root.

Dockerfile
ARG NODE_VERSION=24.22.1

FROM node:${NODE_VERSION}-bookworm-slim AS build
WORKDIR /app

RUN corepack enable
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile

COPY . .
ENV NITRO_PRESET=node-server
RUN pnpm run build

FROM node:${NODE_VERSION}-bookworm-slim AS runtime
WORKDIR /app

ENV NODE_ENV=production \
    HOST=0.0.0.0 \
    PORT=3000

COPY --from=build --chown=node:node /app/.output ./.output

USER node
EXPOSE 3000
CMD ["node", ".output/server/index.mjs"]

The Nitro Node server preset puts the server, public assets and traced runtime dependencies in .output. Copy the whole directory so requests for both pages and assets work.

My portfolio uses Nuxt Content with better-sqlite3. Its Dockerfile has a couple of extra steps. The build stage installs Python, Make and a C++ compiler in case the SQLite module needs to compile. The runtime stage creates a writable /app/.data directory for the node user. Both stages use the same Debian base so the native module runs in the environment it was built for.

Add a .dockerignore to keep local installs and environment files out of the build context.

.git
.nuxt
.output
.data
node_modules
.env
.env.*

Build and Run the Image

Build the image, publish port 3000 on your machine, and make a request to the Nuxt server.

Terminal
docker build -t my-nuxt-app .
docker run --rm -p 127.0.0.1:3000:3000 my-nuxt-app

Open http://localhost:3000 or run curl -fsS http://localhost:3000/ from a second terminal. Test a route that uses your server code too. For this site I check an article, the RSS feed and llms.txt, which exercise the content database.

Run With Docker Compose

If your app runs alongside other services, put the same build and port settings in compose.yaml.

compose.yaml
services:
  web:
    build: .
    ports:
      - "127.0.0.1:3000:3000"

Start it with docker compose up --build. Pass production settings to the running container through your deployment platform. For example, set PORT to match the internal port your reverse proxy targets.

If you're deciding where to run the container, I wrote about self-hosting my Nuxt apps with Coolify. You can also subscribe to my weekly Vue and Nuxt newsletter or follow me on X.

If you found this article helpful.You will love these ones as well.
A Comprehensive Guide to Data Fetching in Nuxt 3 Image

A Comprehensive Guide to Data Fetching in Nuxt 3

Analyze Memory Leaks in Your Nuxt App Image

Analyze Memory Leaks in Your Nuxt App

Building a Polite Newsletter Popup With Nuxt 3 Image

Building a Polite Newsletter Popup With Nuxt 3

Connecting a MySQL Database in Nuxt with Drizzle ORM Image

Connecting a MySQL Database in Nuxt with Drizzle ORM