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:
** host node**[root@centos7-x64 statusbar]# wrk http://localhost:1337Running10stest@http://localhost:13372threadsand10connectionsThreadStatsAvgStdevMax+/-StdevLatency1.51ms1.34ms39.81ms98.03%Req/Sec3.46k821.794.29k76.50%68971requestsin10.05s,10.26MBreadRequests/sec:6862.84Transfer/sec:1.02MBdockernode`--net=host`mode[root@centos7-x64 ~]# wrk http://localhost:1337Running10stest@http://127.0.0.1:13372threadsand10connectionsThreadStatsAvgStdevMax+/-StdevLatency1.49ms287.14us3.81ms92.99%Req/Sec3.30k424.643.60k91.00%65866requestsin10.03s,9.80MBreadRequests/sec:6564.82Transfer/sec:0.98MBOverall, for regular web applications, the performance loss is relatively small when connecting to external networks, but it greatly facilitates our development and operation.
### Deploying Applications> What is MEAN architecture?> MEAN stands for Mongodb / ExpressJS / AngularJS /NodeJS, which is a combination of popular website application development frameworks that covers frontend to backend. Since these frameworks uses Javascript as the language, it is also commonly referred to as Javascript Fullstack.
Inthisexample,wewillattempttodeployaNodeJSapplicationwithMEANarchitecture.```shellDirectoryStructure├──docker-node-full/│├──start.sh│├──Dockerfile
Installing Docker
For Ubuntu system, use apt to install:
$sudoapt-getinstall-ydocker-engine
Creation steps
Create folder
mkdir~/docker-node-full&&cd $_
Create Dockerfile configuration file
# Set the base imageFROMubuntu:14.10# Install NodeJS and npmRUNapt-getinstall-ynodejsnpm# Because the node under apt-get is actually nodejs, create a shortcut for nodeRUNln-s/usr/bin/nodejs/usr/bin/node# Install GitRUNapt-getinstall-ygit# Install Mongodb (from official tutorial)RUNapt-keyadv--keyserverhkp://keyserver.ubuntu.com:80--recv7F0CEB10RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
RUNapt-getupdateRUNapt-getinstall-ymongodb-org# Set working directoryWORKDIR/srv/full# Clean up existing files (if any)RUNrm-rf/srv/full# Download the MEAN architecture website code prepared through GitRUNgitclonehttps://github.com/chuyik/fullstack-demo-dist.git.# Install NodeJS dependenciesRUNnpminstall--production# Create mongodb data folderRUNmkdir-p/data/db# Expose ports (for NodeJS application and Mongodb respectively)EXPOSE556627017# Set NodeJS application environment variablesENVNODE_ENV=productionPORT=5566# Add startup scriptADDstart.sh/tmp/RUNchmod+x/tmp/start.sh# Set default startup commandCMD ["bash", "/tmp/start.sh"]
Create start.sh startup script
# Start Mongodb in the backgroundmongod--fork--logpath=/var/log/mongo.log--logappend# Run NodeJS applicationnpmstart
Build Image
# Use this command to build a mirror based on the information configured in Dockerfiledockerbuild--rm-tnode-full.# Check if the mirror was created successfullydockerimages
Run Image
# Run the image just created# -p Set port in the format of "host port:container port"dockerrun-p5566:5566node-full
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.
# -v Disk mapping, in the format of "host directory:container directory"dockerrun-p5566:5566-v/var/mongodata:/data/dbnode-full
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.