sample node app for Docker examples
Last updated Jun 30, 2026
2.4k
Stars
487
Forks
11
Issues
0
Stars/day
Attention Score
93
Language breakdown
JavaScript 68.1%
Dockerfile 24.5%
Shell 7.4%
▸ Files
click to expand
README
Node.js + Docker for Showing Good Defaults in Using Node.js with Docker
This tries to be a "good defaults" example of starting to use Node.js in Docker for local development and shipping to production with basic bells, whistles, and best practices. Issues/PR welcome.
Note I have more advanced examples of Node.js Dockerfiles and Compose files in my DockerCon 2022 talk and repository. I also have more about everything Docker and Node.js in my 8 hour video course Docker for Node.js.
Also Note, I have other resources on Docker and Kubernetes here.
Local Development Features
- Dev as close to prod as you can.
- Prevent needing node/npm on host.
node_modules outside app root in the container image so local development won't run into a
problem of bind-mounting over it with local source code. This means it will run npm install
once on container build and you don't need to run npm on host or on each docker run.
It will re-run on build if you change package.json.
- One line startup. Uses
docker compose upfor single-line build and run of local
- Edit locally while code runs in container.
- Use nodemon in container. Docker Compose uses nodemon for development for auto-restarting
- Enable debug from host to container. Opens the inspect port 9229 for using host-based
--inspect by default in Docker Compose.
- Provides VSCode debug configs and tasks for tests. for Visual Studio Code fans,
.vscode directory has the goods, thanks to @JPLemelin.
- Small image and quick re-builds.
COPYinpackage.jsonand runnpm install
COPY in your source code. This saves big on build time and keeps the container image lean.
- Bind-mount package.json. This allows adding packages in realtime without rebuilding images. e.g.
docker compose exec -w /opt/node_app node npm install --save <package name>
Production-minded Features
- Use Docker built-in healthchecks. This uses Dockerfile
HEALTHCHECKwith/healthzroute to
- Proper NODEENV use. Defaults to
NODEENV=productionin Dockerfile and overrides to
development in docker-compose for local dev.
- Don't add dev dependencies into the production image. Proper
NODE_ENVuse means dev dependencies
- Enables proper SIGTERM/SIGINT for graceful exit. Defaults to
node index.jsrather than npm
docker run in foreground).
To get node index.js to graceful exit, extra signal-catching code is needed.
The Dockerfile and index.js document the options and links to known issues.
- Run Node.js in the container as
nodeuser, notroot. - Use docker-stack.yml example for Docker Swarm deployments.
Assumptions
- You have Docker and Docker Compose installed (Docker Desktop for Mac/Windows/Linux).
- You want to use Docker for local development (i.e. never need to install Node.js/npm on host)
- You don't want to lose fidelity in your dev workflow. You want a easy environment setup,
- You use
docker-composefor local development only (docker-compose was never intended to be
- The
docker-compose.ymlis not meant fordocker stack deployin Docker Swarm,
docker-stack.yml for Swarm.
Getting Started
If this was your Node.js app, to start local development you would:
- Running
docker compose upis all you need. It will: - Build custom local image enabled for development (nodemon,
NODE_ENV=development). - Start container from that image with ports 80 and 9229 open (on localhost).
- Starts with
nodemonto restart Node.js on file change in host pwd. - Mounts the pwd to the app dir in container.
- If you need other services like databases,
up.
- Compose won't rebuild automatically, so either run
docker compose buildafter changingpackage.json
docker compose up --build.
- Be sure to use
docker compose downto cleanup after your done dev'ing.
docker compose exec -w /opt/node_app node npm install --save <package name>- This installs it inside the running container.
- Nodemon will detect the change and restart.
--savewill add it to the package.json for nextdocker compose build
- Execute
docker compose exec node npm test, It will: - Run a process
npm testin the container. - You can use the vscode to debug unit-tests with config
Docker Test (Attach 9230 --inspect),
Ways to improve security
Run Node.js as Non-Root User
As mentioned in the official docker Node.js image docs, Docker runs the image as root. This can pose a potential security issue.
As a security best practice, it is recommended for Node.js apps to listen on non-privileged ports as mentioned here.
Other Resources
🔗 More in this category