composebuilder

Docker Compose: Nuxt + PostgreSQL

Run a Nuxt 3 app built with Nitro and PostgreSQL 16 in Docker Compose, with a Node 22 Dockerfile, a database healthcheck, persistent data and a checklist.

When to use this stack

This stack runs a Nuxt 3 application, which uses Vue for the interface and Nitro for the server, with a PostgreSQL 16 database for persistent data. Nuxt’s server routes can read and write the database, so the same project can serve pages, forms and a JSON API. Compose starts the database first, waits for it to answer, then starts the built Nuxt server with the connection string from the environment. The result is a self-contained deployment that behaves the same on a laptop and on a VPS.

Choose it when your team uses Vue, you want server-side rendering or a hybrid rendering mode, and you need a relational database for accounts, orders or content. It suits business websites with a login area, booking tools, dashboards and small SaaS products. Do not choose it when the whole site is static and can be generated once, because a static host is cheaper and simpler. It is also not ideal for very high traffic without a cache layer in front. Nuxt does not ship a database client, so you must add one to the project and write the queries yourself.

The Dockerfile uses node:22-alpine. It installs all dependencies so that the build can run, then produces the Nitro output in .output. The runtime command is node .output/server/index.mjs, which needs only the built folder. Keep the Nuxt project in ./app with its package.json and package-lock.json, and add a .dockerignore file with node_modules, .nuxt, .output and .env.

docker-compose.yml

services:
  app:
    build: ./app
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "3000:3000"
    environment:
      NODE_ENV: production
      NITRO_HOST: 0.0.0.0
      NITRO_PORT: 3000
      DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:

Dockerfile (app/Dockerfile)

Place this file in ./app next to package.json. The whole folder is the build context.

Dockerfile
FROM node:22-alpine
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . .
RUN npm run build

ENV NODE_ENV=production \
    NITRO_HOST=0.0.0.0 \
    NITRO_PORT=3000
EXPOSE 3000
CMD ["node", ".output/server/index.mjs"]

.env

.env
# PostgreSQL database, user and password
POSTGRES_DB=nuxt
POSTGRES_USER=nuxt
POSTGRES_PASSWORD=CHANGE_ME_postgres_password

Start and verify

Shell
docker compose config --quiet

# Build the Nuxt output and start both services
docker compose up -d --build

# db must be healthy, app must be running
docker compose ps

# The log should show the Nitro server listening on port 3000
docker compose logs --tail=50 app

# The home page should return 200 (or a redirect for locale routing)
curl -I http://localhost:3000

# Check that PostgreSQL accepts connections with the app user
docker compose exec db pg_isready -U nuxt -d nuxt

Production checklist

  • Put Nuxt behind a reverse proxy with HTTPS, and set the public host there, not in the app.
  • Keep DATABASE_URL on the server only. Never place it in runtimeConfig.public or in a value that the browser can read.
  • Store secrets with the NUXT_ prefix override pattern or in the environment, and keep .env out of Git and out of the image.
  • Use a database driver with a connection pool, and cap the pool size so PostgreSQL’s max_connections is not exceeded.
  • Pin node:22-alpine and postgres:16-alpine, and rebuild the image after each dependency update.
  • Back up postgres_data with pg_dump on a schedule, and test a restore into a new database.
  • Enable response caching for public pages in Nitro route rules, to reduce database load.
  • Add a memory limit to the app container, because Node will use more memory under load.

Common problems

  • Symptom: The site is unreachable from the host even though the container runs. Cause: The Nitro server listens on localhost inside the container. Fix: Keep NITRO_HOST set to 0.0.0.0 in the environment and recreate the app container.
  • Symptom: The build fails with “Cannot find module” for a database client. Cause: The driver is not listed in package.json. Fix: Run npm install pg (or your chosen client) in ./app, commit package-lock.json and rebuild.
  • Symptom: The app shows the wrong port in logs or in redirects. Cause: PORT and NITRO_PORT disagree with the published mapping. Fix: Keep the container port 3000 on both sides of the mapping and in NITRO_PORT.
  • Symptom: DATABASE_URL is undefined at runtime. Cause: The variable was added to .env but not to the app environment section. Fix: Add it to compose under app, then run docker compose up -d app.
  • Symptom: The first request after a deploy is slow. Cause: Connections to PostgreSQL are created on demand and the cache is empty. Fix: Warm the pool with a health route, and cache heavy queries.

Variations

  • Nuxt with Drizzle ORM: Add drizzle-orm and drizzle-kit to the project, write the schema in server/db, and run migrations with a one-off container before you start the app.
  • Preview environments: Copy the compose file, change the project name with -p, and use a separate published port for each branch.
  • Static generation for public pages: Run npm run generate for the public marketing pages and serve them from Nginx, while keeping the Nitro app for dynamic routes. This reduces load on the database.

Questions

Which folder must contain my Nuxt project?

The app service builds from ./app, so package.json, package-lock.json and the nuxt.config file must be in that folder. The image runs the output of npm run build, which is self-contained.

Why does my Nuxt server listen on localhost only?

The Nitro server binds to the host set by NITRO_HOST. This file sets NITRO_HOST to 0.0.0.0 so the published port works. Without it, the container is unreachable from the host.

Does Nuxt connect to PostgreSQL by itself?

No. Nuxt does not include a database driver. Add a client such as pg or drizzle-orm to your project, and read DATABASE_URL on the server side only.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Laravel + PostgreSQL + Redis

    Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Next.js + PostgreSQL + Redis

    Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Spring Boot + MariaDB

    Run a Spring Boot 3 API on Java 21 with MariaDB 11.4 in Docker Compose, with a JRE image, datasource variables, a database healthcheck and a checklist.

    • app
    • db
    Difficulty: Intermediate
  • Strapi + PostgreSQL

    Run a Strapi 5 headless CMS with PostgreSQL 16 in Docker Compose, using a Node 22 image, secret variables, persistent uploads and a production checklist.

    • app
    • db
    Difficulty: Intermediate

Browse more Web & Frameworks stacks or the full library.