使用Docker创建Node.js方法服务器管理。利用Docker轻量级系统,对Nodejs实现实时管理,宿主机同步文件。
Docker查询node镜像并拉取
[root@VM_0_16_centos ~]docker serarch node# 也可以通过dockerfile安装
[root@VM_0_16_centos ~]docker pull node
安装node容器
[root@VM_0_16_centos nodejs]# docker run -it -d --name node02 -p 443:3000 -v /data/nodejs/express02:/home/express node
查看安装的容器
[root@VM_0_16_centos nodejs]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46101615ca3a node "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3001->3000/tcp node02
c6d2e4b0caf4 node "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3000->3000/tcp node01
ed5a7f11dd55 nginx "/docker-entrypoint.…" 17 hours ago Up 15 hours 0.0.0.0:8080->80/tcp nginx-01
进入express同步文件夹进行写node.js
[root@VM_0_16_centos nodejs]# cd /data/nodejs/express02
进入docker容器node02中查看文件夹
[root@VM_0_16_centos express02]# docker exec -it node02 /bin/bash
root@46101615ca3a:/# cd /home/express/
root@46101615ca3a:/home/express# ls
进入宿主机配置node服务器
[root@VM_0_16_centos express02]# npm init
[root@VM_0_16_centos express02]# ls
package.json
[root@VM_0_16_centos express02]# cd ../express
[root@VM_0_16_centos express]# ls
index.js node_modules package.json package-lock.json
[root@VM_0_16_centos express]# cp index.js ../express02/
[root@VM_0_16_centos express]# cd ../express02
[root@VM_0_16_centos express02]# ls
index.js package.json
[root@VM_0_16_centos express02]#
创建禁止同步文件.dockerignore
[root@VM_0_16_centos express02]# cat .dockerignore
node_modules/
创建express
[root@VM_0_16_centos express02]# ls
index.js package.json package-lock.json
[root@VM_0_16_centos express02]# npm install --save express
[root@VM_0_16_centos express02]# ls
index.js node_modules package.json package-lock.json
[root@VM_0_16_centos express02]#
进入容器内操作启动nodejs
[root@VM_0_16_centos express02]# docker exec -it node02 /bin/bash
root@46101615ca3a:/# cd /home/express/
root@46101615ca3a:/home/express# ls
index.js node_modules package-lock.json package.json
root@46101615ca3a:/home/express#
root@46101615ca3a:/home/express# node index.js
listening 3000
访问已开启的nodejs
http://xxx.xxx.xxx.xxx:3001/
http://xxx.xxx.xxx.xxx:3000/
安装nodemon
nodemon是实时监控node.js服务的内容修改进程
在package.json中添加快捷启动,代码如下
{
"name": "package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server":"nodemon /hoem/express/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
以nodemon启动命令
npm run server