Skip to main content
BLOG.siposdani87

Dockerfile for Astro.build with node adapter

How to build and serve an Astro project using Docker and Nginx

By Dániel Sipos
· · 1 min read

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 --from=builder /app/build /usr/share/nginx/html

This Dockerfile uses two stages to keep the final image small and production-ready:

  1. Builder stage — Installs dependencies and runs the Astro build. The npm ci command ensures a clean, reproducible install based on the lockfile.
  2. Runner stage — Copies only the static build output into a lightweight Nginx Alpine image, discarding the Node.js runtime and node_modules from 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/.

Share with your friends

Related posts