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_modules
outside 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 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 keep container lean.dce node npm install --save <package name>
(dosn't work on all systems)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 container by default. Using docker-compose will build with them by default.node index.js
rather then 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 on file change in host pwd.up
.docker-compose build
works for manually building.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 build
To execute the unit-tests, you would:
docker-compose exec node npm test
, It will:npm test
in 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:
MIT License,
Copyright (c) 2015-2019 Bret Fisher
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.
1.拉取镜像 docker pull redis 2.创建目录,分别存放redis配置文件以及数据 mkdir /root/docker/redis/data 3.下载redis.conf文件(或者用我下面给出的),并存放在/root/docker/redis wget http://download.redis.io/redis-stable/redis.conf 4.修改redis.co
1. 安装方式 压缩包 容器安装docker(推荐,一分钟安装) 2. 环境 linux服务器 已安装好 docker + docker-compose (不了解的客官,请点击进入) 3. 步骤: Step 1: linux下建立如下目录结构,以及文档 /mnt/docker/ docker容器安装目录(可随意创建) /mnt/docker/redis/ redis容器安装目录(可随意创建)
首先在另外一台联网电脑拉取最新的redis,zookeeper,kafka镜像 docker pull redis docker pull zookeeper docker pull wurstmeister/kafka 然后导出镜像为tar文件 docker save -o redis.tar redis ##-o:指定保存的镜像的名字 #另外两个相同 导入镜像到生产服务器 脚本总已经写
version: "3.0" services: mysqldb: image: mysql:5.7.19 container_name: mysql restart: always ports: - "3306:3306" volumes: - /root/mysql/conf:/etc/mysql/conf.d
Docker Node Tester(DNT)提供了一个测试平台,Node.js 项目针对 Docker 容器中多个版本的 Node.js 运行,然后以表格方式输出结果。你还可以针对最前沿版本的 Node 进行测试,无论是什么版本。请注意,不同版本的 Node 都是从源代码构建的,这意味着你最后会得到 Node 整个源代码树的本地副本;确保你有足够的空间来存储它。
RESTful API Server Boilerplate Featuring Docker, Node, Express, MongoDB, Mongoose, & NGINX License & Purpose MIT License. This is something I've used in production before with success that I found use
Good First Issues Find good first issues right from your CLI! Install Good First Issues Requires Python 3.6.1 or higher. $ pip3 install good-first-issues --upgrade The CLI uses the alias gfi to run co
This is as Jekyll theme built to make single page websites, articles, or literary masterpieces readable anywhere. Demo and Documentation Built on top of Solo, by Shu Uesugi
The Good Karma Kit A Docker Compose project to run on servers with spare CPU, disk, and bandwidth. Help the world by contributing your unused computing power to good causes. Quickstart Download docker
[STARTER] Docker-NodeJS-MongoDB-React Under the hood NodeJS ExpressJS MongoDB Mongoose ReactJS Webpack Nginx (serving static content compiled by react) Docker docker-compose Prerequisites Docker Node