使用Docker創(chuàng)建Node.js方法服務(wù)器管理管嬉。利用Docker輕量級系統(tǒng)心褐,對Nodejs實(shí)現(xiàn)實(shí)時管理真屯,宿主機(jī)同步文件几颜。
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
進(jìn)入express同步文件夾進(jìn)行寫node.js
[root@VM_0_16_centos nodejs]# cd /data/nodejs/express02
進(jìn)入docker容器node02中查看文件夾
[root@VM_0_16_centos express02]# docker exec -it node02 /bin/bash
root@46101615ca3a:/# cd /home/express/
root@46101615ca3a:/home/express# ls
進(jìn)入宿主機(jī)配置node服務(wù)器
[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]#
創(chuàng)建禁止同步文件.dockerignore
[root@VM_0_16_centos express02]# cat .dockerignore
node_modules/
創(chuàng)建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]#
進(jìn)入容器內(nèi)操作啟動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是實(shí)時監(jiān)控node.js服務(wù)的內(nèi)容修改進(jìn)程
在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