2016年11月2日 星期三

[nodejs, docker] How to run your nodejs code in docker



How to run your nodejs code in docker

Github: (view)
Google Doc: This document.

之前我都用 vagrant 把 nodejs run 在 VM 裡面, 現在改成 run 在 docker 裡面, 系統輕盈多了.

Pull the Image

sudo docker pull node

Write your node.js code

var server,
port=2012,
http=require('http');
server=http.createServer(function(req,res){
res.writeHead(200,{'Content-type':'text/plain'});
res.end('Hello World');
console.log('guest visted');
});

server.listen(port);
console.log('Server is running');


Run your node.js code in the container

sudo docker run --net=host \
-it --rm \
--name my-running-script \
-v "$PWD":/usr/src/app \
-w /usr/src/app node:latest \
node example.js


Stop

sudo docker -a
sudo docker stop {id}

Verification




Reference