Dockerfile for Astro.build with node adapter
How to build and serve an Astro project using Docker and Nginx
Note: This article uses Node.js 14, which has reached end-of-life. Update the base image to a current LTS version (e.g.,
node:20-alpine) for new projects.
Here is a sample Dockerfile that uses a multi-stage build to containerize an Astro project with Nginx:
FROM node:14 as builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine as runner
COPY /app/build /usr/share/nginx/html This Dockerfile uses two stages to keep the final image small and production-ready:
- Builder stage — Installs dependencies and runs the Astro build. The
npm cicommand ensures a clean, reproducible install based on the lockfile. - Runner stage — Copies only the static build output into a lightweight Nginx Alpine image, discarding the Node.js runtime and
node_modulesfrom the final image.
To build and run the Docker image:
docker build -t astro-image .
docker run -p 80:80 astro-image This starts an Nginx container serving the built Astro project on port 80. For custom routing or caching rules, mount an Nginx configuration file into /etc/nginx/conf.d/.