How to use MongoDB & NodeJS with Docker
陳兆麟老師 chenlin2@ms9.hinet.net https://www.facebook.com/groups/247788228752702/ (2015/5/25貼文)
https://chenlinslim.wordpress.com/
參考文獻:
http://ifdattic.com/how-to-mongodb-nodejs-docker/
https://github.com/pertrai1/365-programming/tree/master/mongo/Week3/homework_3_3_mongoproc_version/hw3-2and3-3/hw3-2and3-3/blog (教材程式碼)
http://proserge.kh.ua/coding/index.php/post/33/MongoDB+for+NodeJs+devs+week4%3A+Perfomance (教材解答)
說明:
node.js + mongo db 是大家習慣使用建立 [Web + database] project 的工具;以上教材是在 docker 的環境下的建立簡易部落格.教材程式碼部分露空,主要讓讀者自己完成;答案也有網址,為了方便展示執行結果,我將 2 者合併為一個 project [hw3-2and3-3.zip] , project 作業環境 Win7.
操作步驟 (docker 安裝及環境設定)
https://docs.docker.com/machine/ (Win7 安裝 docker machine)
mkdir c:\docker-software
$ curl -L https://github.com/docker/machine/releases/download/v0.2.0/docker-machine_windows-amd64.exe > mkdir C:\docker-software\docker-machine.exe (安裝 docker machine)
$ curl -L https://get.docker.com/builds/Windows/x86_64/docker-latest.exe > C:\docker-software\docker.exe (安裝 docker cli)
控制台=> 系統 => 進階系統設定 => 環境變數
PATH=c:\docker-software;..................................
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL
https://www.virtualbox.org/wiki/Downloads (安裝 virtualbox)
//--------------------------------------------------------------------------------------------
$docker-machine create --driver virtualbox dev (vitualbox 產生 dev )
// 請不要在 virtualbox 開啟 dev
C:\Windows\system32>docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM
dev * virtualbox Running tcp://192.168.99.100:2376
//如何讓 dev 主機與 container 共用資料;container 與 window 互傳資料
//請參考https://github.com/rocker-org/rocker/wiki/Sharing-files-with-host-machine
// 以下指令使 dev:/c/Users/slim/foobar, container: /home/rstudio/foobar 資料同步
$docker run -d -p 8787:8787 -v /c/Users/slim/foobar:/home/rstudio/foobar rocker/rstudio
// dev:/c/Users/slim/foobar 資料夾
// container: /home/rstudio/foobar 資料夾
// rocker/rstudio: images
// 顯示結果
$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
rocker/rstudio latest cdf09599493c 9 days ago 808.6 MB
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e937efff19e rocker/rstudio:latest "/usr/bin/supervisor 24 minutes ago Up 24 minutes 0.0.0.0:8787->8787/tcp insane_bell
$docker-machine ip
192.168.99.100
瀏覽器輸入:http://192.168.99.100:8787/
登錄 user/password:rstudio/rstusio
// 透過右下角 upload 將 window 檔案匯入 container: /home/rstudio/foobar
// 選擇檔案透過右下角 more->export 將 container /home/rstudio/foobar 檔案匯出至 window
$docker-machine ssh dev
docker@dev:~$ls /c/Users/slim/foobar
//會看到 rocker/rstudio container: /home/rstudio/foobar 的內容
//可將 rocker/rstudio container 視為中間傳遞工具,其他的 container 可透過它擷取 windows 的資料
(dev)$ifconfig |less // 檢查網址 dev 192.168.99.100
(dev)$passwd docker
Changing password for docker
#Old password:xxxx
#New password:xxxx
// 使用 putty 的好處是操作方面
下載 putty.exe, 設定 ip=192.168.99.100 起動
login:docker
password:xxxxxxx
(dev)$mkdir /tmp/share
(dev)$id docker
uid=1000(docker) gid=50(staff) groups=50(staff),100(docker)
(dev)$mkdir /home/docker/work_directory
(dev)$cd /home/docker/work_directory
(dev)$$docker pull mongo:2.6
(dev)$$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mongo 2.6 28c8b6d98aa7 44 hours ago 392.4 MB
(dev)$docker run -v "$(pwd)":/data --name mongo -d mongo:2.6 mongod --smallfiles
// -v "$(pwd)":/data:將 dev 目前路徑掛載於 container /data 之下
// --name mongo: container 名稱為 mongo
// mongo: image 名稱
// mongod --smallfiles:執行命令 mongo; --smallfiles:Sets MongoDB to use a
// smaller default file size
(dev)$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
616bc65fb5ad mongo:2.6 "/entrypoint.sh mong 13 seconds ago Up 11 seconds 27017/tcp mongo
(dev)$docker exec -it mongo bash
$ mongo
>use test (建立資料庫 test)
> db.col.insert({"a": 4})
WriteResult({ "nInserted" : 1 })
> db.col.find().pretty()
{ "_id" : ObjectId("555d2b0e30798a76ebb19bf7"), "a" : 4 }
> show dbs
admin (empty)
local 0.031GB
test 0.031GB
>exit
$exit
$docker ps -a
06876ff7384e mongo:2.6 "/entrypoint.sh mong 27 hours ago Up 50 minutes 27017/tcp mongo
//以下指令作用直接進入mongo 提示: (此時 container已連接container)
(dev)$docker run -it --rm=true --link mongo:mongo mongo:2.6 sh -c 'exec mongo "$MONGO_PORT_27017_TCP_ADDR:$MONGO_PORT_27017_TCP_PORT/test"'
// -it: 交談式,並執行 bash
// --link mongo:mongo: Add link to another container:
// --rm mongo:Automatically remove the container when it exits
// sh -c 'exec mongo
// 以下測試連結 test 資料庫是否成功
>show dbs
> show dbs
admin (empty)
local 0.031GB
test 0.031GB
> db
test
> db.col.find().pretty()
{ "_id" : ObjectId("555d446834ee62be367979ce"), "a" : 4 }
(使用 puuty 再開一台終端機連接 dev:觀察 container)
(dev)$docker ps -a
311be231a055 mongo:2.6 "\"/entrypoint.sh sh 13 seconds ago Up 13 seconds 27017/tcp
06876ff7384e mongo:2.6 "/entrypoint.sh mong 27 hours ago Up 50 minutes 27017/tcp
// 回至原終端機
>exit (跳出 database 及 container )
docker ps -a // container (311be231a055)已被移除
06876ff7384e mongo:latest "/entrypoint.sh mong 27 hours ago Up 50 minutes 27017/tcp
// 另外一種連接 container (06876ff7384e) 的方法
(dev)$docker exec -it mongo bash // 登錄 container 06876ff7384e
// 以下測試連結 test 資料庫是否成功
$mongo
> show dbs
admin (empty)
local 0.031GB
test 0.031GB
> db
test
> db.col.find().pretty()
{ "_id" : ObjectId("555d446834ee62be367979ce"), "a" : 4 }
//-----------------------------------------------
//如何將 container: mongo 資料庫匯出至 dev:
(dev)$docker exec -it mongo bash (bash 連接 container 06876ff7384e )
// 將 database:test 資料庫輸出之 /data/test-backup
(container)$mongodump --db test --out /data/test-backup
connected to: 127.0.0.1
2015-05-07T04:59:29.474+0000 DATABASE: test to /data/test-backup/test
2015-05-07T04:59:29.474+0000 test.system.indexes to /data/test-backup/test/system.indexes.bson
2015-05-07T04:59:29.475+0000 1 documents
2015-05-07T04:59:29.475+0000 test.col to /data/test-backup/test/col.bson
2015-05-07T04:59:29.476+0000 1 documents
2015-05-07T04:59:29.476+0000 Metadata for test.col to /data/test-backup/test/col.metadata.json
//dev 顯示資料庫內容
(dev)$ls /home/docker/work_directory/test-backup/test/
col.bson col.metadata.json system.indexes.bson
//如何將資料庫由 container 匯出至 window
//使用 rocker/rstudio container 中間傳遞工具:
//將已匯出至 dev 之資料庫 /data/test-backup/test 匯出至 window
(dev)$cp -r /home/docker/work_directory/test-backup/ /c/Users/slim/foobar
//進入 http://http://192.168.99.100:8787/ ,右下角出現 test-backup 資料夾, 選擇more ->export; 系統會將其打包 .zip 匯出 window
// mongo 資料庫由 window 匯入 container (請參考前面如何讓 container 與
// window 互傳資料
// 以下為 dev -> container 步驟):
// 將 dev 之 database 回覆至 container 之 database:test-restored
(dev)$docker exec -it mongo bash
//資料庫 /data/test-backup/test 檔案回存至新資料庫test-restored)
(container)$mongorestore --db test-restored /data/test-backup/test
connected to: 127.0.0.1
2015-05-07T05:11:04.258+0000 /data/test-backup/test/col.bson
2015-05-07T05:11:04.258+0000 going into namespace [test-restored.col] 1 objects found
2015-05-07T05:11:04.259+0000 Creating index: { key: { _id: 1 }, name: "_id_", ns: "test-restored.col" }
(container)$mongo
>use test-restored
>db.col.find().pretty() //驗證 database:test-restored 內容
{ "_id" : ObjectId("553f095dabab2eeb97f5ae14"), "a" : 4 }
>exit (離開 mongo)
$exit (結束 container)
//-------------------------------------------------------
node.js 安裝
//-------------------------------------------------------
(dev)$docker pull node:latest
(dev)$docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mongo 2.6 28c8b6d98aa7 45 hours ago 392.4 MB
node latest 86a1fcd53530 3 days ago 711.1 MB
(dev)$docker run -it --rm node // 執行 container 並進入交談模式,結束後移除 container
>console.log('It works!!')
顯示 It works
ctl^C (2 次) 跳開,結束 container
//將附加檔 hw3-2and3-3.zip 解壓縮至 c:\home\share\hw3-2and3-3
//瀏覽器輸入 http://192.168.99.100:8787
//選擇 upload 將 hw3-2and3-3.zip 匯入 container rocker/rstudio: /home/rstudio/foobar
(dev)$ ls /c/Users/slim/foobar
hw3-2and3-3/
(dev)$cd /c/Users/slim/foobar/hw3-2and3-3/
(dev)$docker run -it --name node -v "$(pwd)":/data --link mongo:mongo -w /data -p 8082:8082 --rm node bash
// 掛載 mongo container
// -w /data: Working directory inside the container (登錄 container直接進入 /data
// -p 8082:8082:Publish all exposed ports to random ports
// node: images name
// bash: bash 登錄
(container)$docker@8383d8ebc3a5:/data$cd blog
npm install (安裝 npm)
node app.js (啟動 server 端)
Express server listening on port 8082
瀏覽器輸入 http://192.168.99.100:8082 // 簡易部落格管理及登錄
//可能出現的錯誤
MongoError: driver is incompatible with this server version
問題解答:https://github.com/sharelatex/sharelatex/issues/296
转载于:https://www.cnblogs.com/slimchen/p/4539921.html