🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Deployment••By Komal Nakrani

Astro Docker Setup for Static and SSR Builds

Run Astro in Docker: a multi-stage Dockerfile that serves the static build with Nginx, a Node.js image for SSR with @astrojs/node, plus .dockerignore and env tips.

#Astro#Docker#Deployment

To run Astro in Docker, use a multi-stage Dockerfile: build the site in a node:22 image, then copy the output into a small runtime image. For a static site, the runtime image is Nginx serving dist/. For on-demand rendering, it is Node.js running dist/server/entry.mjs from the @astrojs/node adapter in standalone mode.

Key Takeaways

  • Build stage: node:22-alpine, npm ci, npm run build. Current Astro needs Node.js 22.23.4 or newer.
  • Static sites ship only dist/ in an Nginx image; no Node.js at runtime.
  • SSR sites need @astrojs/node with mode: 'standalone' and HOST=0.0.0.0.
  • A .dockerignore keeps node_modules and dist out of the build context.

.dockerignore first

# .dockerignore
node_modules
dist
.astro
.git
.env

Without this, Docker copies your local node_modules into the build context, which is slow and can bring in binaries built for the wrong platform. Keeping .env out stops secrets landing in an image layer.

Static site: build, then Nginx

# Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine AS runtime
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 8080
# nginx.conf
server {
  listen 8080;
  root /usr/share/nginx/html;
  index index.html;

  location /_astro/ {
    add_header Cache-Control "public, max-age=31536000, immutable";
  }

  location / {
    try_files $uri $uri/ $uri.html =404;
  }

  error_page 404 /404.html;
}

Copying package.json and the lock file before the rest of the source means Docker reuses the npm ci layer until dependencies change. The node:22-alpine tag tracks the latest 22.x release, which meets the 22.23.4 minimum; pin a full version if you need repeatable builds. Our Nginx tip explains each directive.

# Terminal
docker build -t my-astro-site .
docker run --rm -p 8080:8080 my-astro-site

SSR: Node.js standalone server

Add the Node adapter:

# Terminal
npx astro add node
// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';

export default defineConfig({
  site: 'https://www.example.com',
  adapter: node({ mode: 'standalone' }),
});
# Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS runtime
WORKDIR /app
ENV NODE_ENV=production HOST=0.0.0.0 PORT=4321
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 4321
CMD ["node", "./dist/server/entry.mjs"]

HOST=0.0.0.0 is the line people miss: without it the server listens on localhost inside the container and nothing outside can reach it. The runtime stage installs production dependencies only, and USER node avoids running as root. Our adapters explainer covers when you need SSR at all.

Environment variables

Variables used at build time (anything read through import.meta.env in prerendered pages) must exist during npm run build, so pass them as build arguments. Runtime secrets for SSR should be passed with docker run -e or your platform’s secret store, never baked into the image. Astro’s astro:env schema can mark variables as server-only secrets; see our environment variables guide.

Where to run the image

The same image runs on Fly.io, Railway, Render, a VPS or Kubernetes. Our tips for Fly.io and Railway show platform specifics. For a purely static site, a static host is usually simpler and cheaper than a container; the static deploy docs compare them. Themes from our Astro theme catalog are static by default, so they use the first Dockerfile.

FAQ

What Node.js version should an Astro Docker image use?

Current Astro releases need Node.js 22.23.4 or newer. node:22-alpine installs the latest 22.x release; pin a specific version tag if you want identical builds.

Why can’t I reach my Astro container on localhost?

Inside a container, the server must listen on all interfaces. Set HOST=0.0.0.0, expose the port, and map it with -p 4321:4321.

Does a static Astro site need Node.js in production?

No. Node.js is only needed to build. The runtime image can be Nginx or any web server serving dist/.

How do I keep Astro Docker builds fast?

Copy package.json and the lock file before the source so the npm ci layer is cached, use a .dockerignore, and use BuildKit cache mounts for the npm cache in CI.