docker部署nodejs,mongodb

node项目启动方式
docker run --rm -itd -p 3000:3000 --name node_api -v "$(pwd)":/webapp/nodejs/node_api -w /webapp/nodejs/node_api  node npm start
将当前项目更目录挂载到容器/webapp/nodejs/node_api

docker run -d -p 27017:27017 -v "$(pwd)"/opt/mongodb/affiliate:/webapp/nodejs/node_api_db--name mongo

docker run -p 27017:27017 -v opt/mongodb/affiliate:/webapp/nodejs/node_api_db -d mongo
docker run -p 27017:27017 -v $PWD/opt/mongodb/affiliate:/webapp/nodejs/node_api_db -d mongo
docker run -p 27017:27017 -v $PWD:/webapp/nodejs/node_api_db --name mongo -d mongo --dbpath /opt/mongodb/affiliate
启动mongo的时候如果不指定数据存储路径,则默认在容器根目录下/data/db进行存储数据

管理mongodb

docker ps -a 查看所有的容器
docker ps 查看正在运行中的容器

root@ubuntu:/# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
2d3cd251be6b        node                "npm start"              15 minutes ago      Up 15 minutes       0.0.0.0:3000->3000/tcp     node_api
bf0cda8a8e0a        mongo               "docker-entrypoint..."   2 hours ago         Up 2 hours          0.0.0.0:27017->27017/tcp   hungry_wright

进入已经启动的容器

root@ubuntu:/# docker exec -it hungry_wright bash
root@bf0cda8a8e0a:/# ps -ef | grep mongo
mongodb       1      0  0 07:47 ?        00:00:32 mongod
root        335    328  0 09:28 pts/0    00:00:00 grep mongo
root@bf0cda8a8e0a:/# ps -ef | grep node 
root        337    328  0 09:28 pts/0    00:00:00 grep node
root@bf0cda8a8e0a:/# mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings: 
2017-11-14T07:47:37.188+0000 I STORAGE  [initandlisten] 
2017-11-14T07:47:37.188+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-11-14T07:47:37.188+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] 
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] 
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] 
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-11-14T07:47:37.447+0000 I CONTROL  [initandlisten] 
> show dbs
admin      0.000GB
affiliate  0.011GB
local      0.000GB
> use affiliate
switched to db affiliate
> show collections
affiliate
dy2018
dytt8
sessions
tradedoubler
users
> 
然后可以在容器里使用mongodb shell 管理数据库
查看容器中,mongodb数据存在的目录
root@bf0cda8a8e0a:/# ls
bin   data  docker-entrypoint-initdb.d  etc   lib    media  opt   root  sbin  sys  usr  webapp
boot  dev   entrypoint.sh       home  lib64  mnt    proc  run   srv   tmp  var
root@bf0cda8a8e0a:/# cd data/
root@bf0cda8a8e0a:/data# ls
configdb  db
root@bf0cda8a8e0a:/data# cd db
root@bf0cda8a8e0a:/data/db# ls
WiredTiger                 collection-17--7989148561081090875.wt  index-18--7989148561081090875.wt
WiredTiger.lock                collection-2--7989148561081090875.wt   index-3--7989148561081090875.wt
WiredTiger.turtle              collection-5--7989148561081090875.wt   index-4--7989148561081090875.wt
WiredTiger.wt                  collection-8--7989148561081090875.wt   index-6--7989148561081090875.wt
WiredTigerLAS.wt               diagnostic.data                index-7--7989148561081090875.wt
_mdb_catalog.wt                index-1--7989148561081090875.wt        index-9--7989148561081090875.wt
collection-0--7989148561081090875.wt   index-11--7989148561081090875.wt       journal
collection-10--7989148561081090875.wt  index-12--7989148561081090875.wt       mongod.lock
collection-13--7989148561081090875.wt  index-14--7989148561081090875.wt       sizeStorer.wt
collection-15--7989148561081090875.wt  index-16--7989148561081090875.wt       storage.bson
root@bf0cda8a8e0a:/data/db# 
#由于在启动的时候没有制定数据文件地址,所以默认在容器里面的/data目录下

查看node项目

root@ubuntu:/# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
2d3cd251be6b        node                "npm start"              23 minutes ago      Up 23 minutes       0.0.0.0:3000->3000/tcp     node_api
bf0cda8a8e0a        mongo               "docker-entrypoint..."   2 hours ago         Up 2 hours          0.0.0.0:27017->27017/tcp   hungry_wright
root@ubuntu:/# docker exec -it node_api bash
root@2d3cd251be6b:/webapp/nodejs/node_api# ls
api common    log4js-master.json  models        opt            public       test
app.js  controls  log4js-worker.json  node_api.iml  package-lock.json  routes       views
bin log.js    logs            node_modules  package.json       settings.js
root@2d3cd251be6b:/webapp/nodejs/node_api# cd /
root@2d3cd251be6b:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  webapp
root@2d3cd251be6b:/# cd webapp/
root@2d3cd251be6b:/webapp# ls
nodejs
root@2d3cd251be6b:/webapp# cd nodejs/
root@2d3cd251be6b:/webapp/nodejs# ls
node_api
root@2d3cd251be6b:/webapp/nodejs# cd node_api/
root@2d3cd251be6b:/webapp/nodejs/node_api# ls
api bin controls  log4js-master.json  logs    node_api.iml  opt            package.json  routes   test
app.js  common  log.js    log4js-worker.json  models  node_modules  package-lock.json  public        settings.js  views
root@2d3cd251be6b:/webapp/nodejs/node_api# 

参考
http://blog.shiqichan.com/Dockerizing-a-Node-js-Web-Application/#more 在Docker中运行Node.js的Web应用
http://www.jb51.net/article/116783.htm docker中使用mongodb数据库详解(在局域网访问)

你可能感兴趣的:(nodejs)