Docker + Node.js
Containerize a Node.js application with a production-ready multi-stage Dockerfile and Docker Compose for local development.
Create a .dockerignore File
Exclude files that should not be copied into the image — node_modules, build output, and secrets. This dramatically reduces image size and build time.
Write a Multi-Stage Dockerfile
Multi-stage builds keep the final image lean — the builder stage installs all dev dependencies and compiles TypeScript, while the runner stage copies only the compiled output and production deps.
Build and Run the Image
Build the Docker image and run a container locally to confirm everything works before setting up Compose.
docker build -t my-node-app . && docker run -p 3000:3000 --env-file .env my-node-appCreate docker-compose.yml for Local Development
Docker Compose wires up your app, a database, and any other services. Using volumes to mount source code enables hot-reload without rebuilding the image on every change.
Start Services with Docker Compose
Spin up all services defined in docker-compose.yml. The -d flag runs them in detached (background) mode.
docker compose up -dCreate a Production docker-compose.prod.yml
Override the dev compose file for production deployments. This targets the lean runner stage and removes volume mounts.
Add Useful npm Scripts
Add convenience scripts to package.json so the team doesn't have to remember long Docker commands.
Verify the Running Container
Check that your container is up, inspect its logs, and exec into it for debugging if needed.
docker compose ps && curl http://localhost:3000Found this guide helpful? Check out more tools and guides.
Browse All GuidesPrerequisites
- Docker Desktop installed
- Node.js 18+ installed
- Existing Node.js / Express app