Node.js & Docker

docker vs host

var http = require('http');
    http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337); 
console.log('Server running at http://0.0.0.0:1337/');

For the performance issue that we concern most, we did a comparative testing on docker vs host node based on node-v4.2.3. The conclusion is that the performance loss is between 1%~4%, depending on network and business code factors. The data is as follows:

  • External network environment

docker node
root@ubuntu-512mb-nyc3-01:~/wrk# ./wrk http://192.241.209.*:1337
Running 10s test @ http://192.241.209.*:1337
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    76.01ms    1.24ms  88.79ms   83.68%
    Req/Sec    65.51     16.12   101.00     76.77%
  1305 requests in 10.04s, 198.81KB read
Requests/sec:    129.99
Transfer/sec:     19.80KB
host node
root@ubuntu-512mb-nyc3-01:~/wrk# ./wrk http://192.241.209.*:1337 
Running 10s test @ http://192.241.209.*:1337
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    75.85ms    1.87ms  87.10ms   61.29%
    Req/Sec    65.66     12.35   101.00     57.07%
  1307 requests in 10.03s, 199.11KB read
Requests/sec:    130.27
Transfer/sec:     19.85KB  
  • Internal network environment

Installing Docker

For Ubuntu system, use apt to install:

Creation steps

  • Create folder

  • Create Dockerfile configuration file

  • Create start.sh startup script

Build Image

Run Image

Access the application

You can access http://localhost:5566 with a browser or run curl -s http://localhost:5566.

Save Mongodb Data Files

Since the Mongodb service runs in the Docker container, the data is also inside, but it is not conducive to data management and preservation. Therefore, we can use some methods to save Mongodb data files outside the container.

Disk Mapping

This is the easiest way, and there is a parameter for disk mapping -v in the docker run command.

However, this command fails on Mac and Windows because the boot2docker virtual machine does not support it. Therefore, you can save the data inside boot2docker and set up a shared folder for Mac or Windows to access.

Last updated