Dockerfile for Docusaurus
Serve static documentation Docusaurus website via Nginx
Note: This article uses Node.js 16, which has reached end-of-life. Update the base image to a current LTS version for new projects.
Docusaurus is a popular static site generator for documentation websites. This guide shows how to containerize a Docusaurus project using a multi-stage Docker build with Nginx.
Nginx - default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
location / {
try_files $uri $uri.html /index.html =404;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
} Dockerfile
FROM node:16 as builder
RUN npm install -g npm@8.18.0
# Install dependencies
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
# Copy all local files into the image
COPY . .
RUN npm run build
FROM nginx:1.23-alpine as runner
ENV INSTALL_PATH /usr/share/nginx/html
WORKDIR $INSTALL_PATH
COPY default.conf /etc/nginx/conf.d
COPY /app/build/ $INSTALL_PATH/
RUN set -x ;
addgroup -g 82 -S www-data ;
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
RUN chown -R www-data:www-data $INSTALL_PATH/*
RUN chmod -R 0755 $INSTALL_PATH/*
Conclusion
This multi-stage build keeps the final image small by discarding the Node.js build environment and serving only the static output through Nginx.