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.
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.docker compose up for single-line build and run of local
development server.--inspect by default in Docker Compose..vscode directory has the goods, thanks to @JPLemelin.COPY in package.json and run npm install
before COPY in your source code. This saves big on build time and keeps the container image lean.docker compose exec -w /opt/node_app node npm install --save <package name>HEALTHCHECK with /healthz route to
help Docker know if your container is running properly (example always returns 200, but you get the idea).NODE_ENV=production in Dockerfile and overrides to
development in docker-compose for local dev.NODE_ENV use means dev dependencies
won't be installed in the image by default. Using Docker Compose will build with them by default.node index.js rather than npm
for allowing graceful shutdown of node.
npm doesn't pass SIGTERM/SIGINT properly (you can't ctrl-c when running 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.node user, not root.docker-compose for local development only (docker-compose was never intended to be
a production deployment tool anyway).docker-compose.yml is not meant for docker stack deploy in Docker Swarm,
it's meant for happy local development. Use docker-stack.yml for Swarm.If this was your Node.js app, to start local development you would:
docker compose up is all you need. It will:NODE_ENV=development).nodemon to restart Node.js on file change in host pwd.up.docker compose build after changing package.json
or do what I do and always run docker compose up --build.docker compose down to cleanup after your done dev'ing.If you wanted to add a package while docker-compose was running your app:
docker compose exec -w /opt/node_app node npm install --save <package name>--save will add it to the package.json for next docker compose buildTo execute the unit-tests, you would:
docker compose exec node npm test, It will:npm test in the container.Docker Test (Attach 9230 --inspect),
It will: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.
—
$ claude mcp add node-docker-good-defaults \
-- python -m otcore.mcp_server <graph>