A Record of Introducing Docker Compose to a Docusaurus Project
This article organizes the steps I took to introduce Docker Compose for the local development environment of a Docusaurus project.
Background:
To standardize the local development environment and simplify setup, I introduced Docker Compose to my Docusaurus (v3.8.0) project. Before this, I was running Node.js (v22.16.0) and pnpm (v10.11) by installing them directly on each developer's local machine.
1. Creating the Development Dockerfile (Dockerfile.dev)
To define a Docker image specifically for local development, I created Dockerfile.dev following these steps.
-
File Creation I created a file named
Dockerfile.devin the project root. -
Specify Base Image For consistency with the production
Dockerfile, I specifiednode:22.16.0-alpineas the base image. -
Enable pnpm and Install Dependencies After running
corepack enable pnpm, I copiedpackage.jsonandpnpm-lock.yamlinto the container and installed all packages, including development dependencies, withpnpm install --frozen-lockfile. -
Expose Port and Set Start Command I exposed Docusaurus's default development port
3000and set the startup command topnpm start --host 0.0.0.0 --no-open. This allows access from outside the container while suppressing the unnecessary automatic opening of a browser.# syntax=docker/dockerfile:1
FROM node:22.16.0-alpine AS development
WORKDIR /app
RUN corepack enable pnpm
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
EXPOSE 3000
CMD ["pnpm", "start", "--host", "0.0.0.0", "--no-open"]
2. Creating the Docker Compose Configuration File (docker-compose.yml)
To define and manage the development service, I created docker-compose.yml with the following steps.
-
File Creation I created a file named
docker-compose.ymlin the project root. -
Service Definition I defined a development service named
appand set the container name tohkdocs_dev_app. The build context was set to the project root (.), and the Dockerfile to use was specified asDockerfile.dev. -
Port Mapping Configuration I mapped the host's port
3000to the container's port3000. -
Volume Mount Configuration I set
.:/app:cachedfor source code synchronization, and/app/node_modulesand/app/.docusaurusas container-only volumes to isolate thenode_modulesand.docusaurusdirectories. -
Environment Variables and Other Settings I set
NODE_ENV=development. I preparedCHOKIDAR_USEPOLLING=true(commented out) for potential hot-reloading instability. For interactive execution, I settty: trueandstdin_open: true.version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile.dev
container_name: hkdocs_dev_app
ports:
- "3000:3000"
volumes:
- .:/app:cached
- /app/node_modules
- /app/.docusaurus
environment:
- NODE_ENV=development
# CHOKIDAR_USEPOLLING=true
tty: true
stdin_open: true
3. Updating the Docker Build Context Exclusion File (.dockerignore)
To optimize Docker image build efficiency and size, I updated the .dockerignore file. The main items to exclude are .git, node_modules, build, .docusaurus, the Docker files themselves, deployment scripts, environment-specific files, log files, OS-specific files, and IDE configuration files.
Note:
pnpm-lock.yamlis used by theCOPYcommand inDockerfile.dev, so it should not be included in the.dockerignorefile.
# Git
.git
.gitignore
# Node.js
node_modules
# Docusaurus build output and cache
build
.docusaurus
# Docker related files
Dockerfile
Dockerfile.dev
docker-compose.yml
docker-compose.*.yml
# Deployment scripts & environment specific files
deploy.sh
.env
*.local
# Logs
*.log
pnpm-debug.log*
# IDE/Editor specific
.DS_Store
.vscode/
.idea/
4. Using and Verifying the Development Environment
-
Starting the Development Environment From the terminal in the project root directory, run
docker-compose up --buildfor the first time or when configuration files have changed. For subsequent starts, usedocker-compose up. -
Verifying in the Browser Access
http://localhost:3000in a browser to confirm that the Docusaurus site is displayed. -
Testing Hot Reloading Edit and save a source code file in an editor on the host machine and confirm that the changes are immediately reflected in the browser (hot reload).
Note: If hot reloading is unstable, you can address it by uncommenting
CHOKIDAR_USEPOLLING=trueindocker-compose.ymlor by adding the--polloption to theCMDinDockerfile.devand restarting withdocker-compose up --build. -
Stopping the Development Environment Press
Ctrl + Cin the terminal where the development server is running to stop the container. To completely remove the container and its associated resources (like networks), run thedocker-compose downcommand.
