免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2015年2月12日 星期四

Dockering nodejs and mongodb on EC2 ubuntu


(one host, two containers) scenario
(one ubuntu host, nodejs container + mongodb container)

//=== Official repo for nodejs on docker
https://registry.hub.docker.com/_/node/

[Dockerfile for myapp]
$ cd myapp
$ nano Dockerfile

FROM node:0.10-onbuild
EXPOSE 8080 [can be omitted?]

[build myapp image based on node:0.10?]
$ sudo docker build -t myapp .



//=== Official repo for mongodb on docker
https://registry.hub.docker.com/_/mongo/

[prerequisites]
* from https://github.com/docker-library/mongo/blob/d9fb48dbdb0b9c35d35902429fe1a28527959f25/2.2/Dockerfile
we can guess /data/db is where mongodb stores data;

* mydb.ns and mydb.0 are the data files of mongodb for myapp
copy them to local folder mymongo to build image based on Official repo mongo:2.2


[Dockerfile for mymong]
$ cd mymongo
$ nano Dockerfile

FROM mongo:2.2
Maintainer 3h
COPY mydb.ns /data/db/mydb.ns
COPY mydb.0 /data/db/mydb.0

$ sudo docker build -t mymongo .



//=== start mymongo first, then start myapp

http://blog.giantswarm.io/getting-started-with-docker-and-meanjs
[start mymongo with container named 'db']
$ sudo docker run -p 27017:27017 -d --name db mymongo


https://docs.docker.com/userguide/dockerlinks/
[start myapp with link to the running 'db' container via alias named 'mydb_alias']
$ sudo docker run -p 80:3000 --link db:mydb_alias myapp

* port 80 is for ubuntu host so that internet users can access my app;
* port 3000 is for container, which should match that used in app.js for nodejs


*** the alias name for db host, 'mydb_alias' should match that used in app.js for nodejs
ie. to establish db connection via
db host: mydb_alias
db port: 27017


//=== test mongod on docker container

[ start up mongod container ]
$ sudo docker run -p 0.0.0.0:27017:27017 -d --name db mongo:2.2
or
$ sudo docker run -p l27.0.0.1:27017:27017 -d --name db mongo:2.2
or
$ sudo docker run -p localhost:27017:27017 -d --name db mongo:2.2


[install mongo client tool]
$ sudo apt-get install mongodb-clients
$ mongo
...
>


沒有留言:

張貼留言