Project Icon

node-docker-good-defaults

Docker化Node.js开发部署的最佳实践指南

node-docker-good-defaults项目展示了Docker环境中Node.js应用的最佳实践。该项目优化了本地开发流程,同时注重生产环境的安全性和效率。它集成了热重载、调试支持和优化构建等开发功能,以及健康检查、优雅关闭和非root用户运行等生产特性。这个开源项目为开发者提供了一个平衡本地开发便利性和生产部署要求的Docker化Node.js环境模板。

Node.js + Docker for Showing Good Defaults in Using Node.js with Docker

Lint Code Base Docker Build

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. Docker Compose builds a local development image that is just like the production image except for the below dev-only features needed in the image. The goal is to have dev environment be as close to test and prod as possible while still giving all the nice tools to make you a happy dev.
  • Prevent needing node/npm on host. This installs 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 up for single-line build and run of local development server.
  • Edit locally while code runs in container. Docker Compose uses proper bind-mounts of host source code into container so you can edit locally while running code in Linux container.
  • Use nodemon in container. Docker Compose uses nodemon for development for auto-restarting Node.js in container when you change files on host.
  • Enable debug from host to container. Opens the inspect port 9229 for using host-based debugging like chrome tools or VS Code. Nodemon enables --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. 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.
  • 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 HEALTHCHECK with /healthz route to help Docker know if your container is running properly (example always returns 200, but you get the idea).
  • Proper NODE_ENV use. Defaults to NODE_ENV=production in Dockerfile and overrides to development in docker-compose for local dev.
  • Don't add dev dependencies into the production image. Proper NODE_ENV use means dev dependencies won't be installed in the image by default. Using Docker Compose will build with them by default.
  • Enables proper SIGTERM/SIGINT for graceful exit. Defaults to 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.
  • Run Node.js in the container as node user, not root.
  • 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) and have dev and prod Docker images be as close as possible.
  • You don't want to lose fidelity in your dev workflow. You want a easy environment setup, using local editors, Node.js debug/inspect, local code repository, while Node.js server runs in a container.
  • You use docker-compose for local development only (docker-compose was never intended to be a production deployment tool anyway).
  • The 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.

Getting Started

If this was your Node.js app, to start local development you would:

  • Running docker compose up is 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 nodemon to 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, just add to compose file and they'll be added to the custom Docker network for this app on up.
  • Compose won't rebuild automatically, so either run docker compose build after changing package.json or do what I do and always run docker compose up --build.
  • Be sure to use 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>
  • This installs it inside the running container.
  • Nodemon will detect the change and restart.
  • --save will add it to the package.json for next docker compose build

To execute the unit-tests, you would:

  • Execute docker compose exec node npm test, It will:
  • Run a process npm test in the container.
  • You can use the vscode to debug unit-tests with config Docker Test (Attach 9230 --inspect), It will:
    • Start a debugging process in the container and wait-for-debugger, this is done by vscode tasks
    • It will also kill a previous debugging process if existing.

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

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号