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 2019 talk and repo. 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_modulesoutside app root in container so local development won't run into a problem of bind-mounting over it with local source code. This means it will run
npm installonce 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
docker-compose upfor single-line build and run of local development server.
--inspectby default in docker-compose.
.vscodedirectory has the goods, thanks to @JPLemelin.
COPYin your source code. This saves big on build time and keep container lean.
dce node npm install --save <package name>(dosn't work on all systems)
/healthzroute to help Docker know if your container is running properly (example always returns 200, but you get the idea).
NODE_ENV=productionin Dockerfile and overrides to
developmentin docker-compose for local dev.
NODE_ENVuse means dev dependencies won't be installed in container by default. Using docker-compose will build with them by default.
node index.jsrather then npm for allowing graceful shutdown of node. npm doesn't pass SIGTERM/SIGINT properly (you can't ctrl-c when running
docker runin foreground). To get
node index.jsto graceful exit, extra signal-catching code is needed. The
index.jsdocument the options and links to known issues.
docker-composefor local development only (docker-compose was never intended to be a production deployment tool anyway).
docker-compose.ymlis not meant for
docker stack deployin Docker Swarm, it's meant for happy local development. Use
If this was your Node.js app, to start local development you would:
docker-compose upis all you need. It will:
nodemonto restart node on file change in host pwd.
docker-compose buildworks for manually building.
docker-compose downto 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>
--savewill add it to the package.json for next
To execute the unit-tests, you would:
docker-compose exec node npm test, It will:
npm testin the container node.
Docker Test (Attach 9230 --inspect), It will:
As mentioned in the official docker node 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 apps to listen on non-privileged ports as mentioned here:
Copyright (c) 2015-2019 Bret Fisher
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.