1. Create a new directory src, and enter the SRC directory
mkdir Src[[email protected] documents]$ CD src/
2. Create Package.json and Index.js files with the following contents:
Package.json
[Email protected] documents]$mkdirSrc[[email protected] documents]$ CD src/[email protected] src]$CatPackage.json {"name":"Docker-centos-hello", "Private":true, "version":"0.0.1", "Description":"node. JS Hello World app on CentOS using Docker", "author":"Gideon Xie <[email protected]>", "Dependencies": { "Express":"3.2.4" }}
Index.js
var express = require (' Express '); // Constants var PORT = 8080; // App var app = Express (); App.get (function (req, res) {res.send (' Hello world\n ') );}); App.listen (PORT);
3. Create Dockfile
[Email protected] src]$CatDockerfile from centos:centos7# Enable EPEL fornode. jsRUN Yum install -y epel- Release# Install node. js and npmRUN Yum install - y npm# Bundle app sourceCOPY. / src# Install app dependenciesRUN CD /src; npm installEXPOSE 8080CMD ["node", " /src/index.js" ][[email protected] src]$
4. Building a build image using Docker build
[Email protected] src]$Docker build-t gideon/centos-node- hello. Sending build context to Docker daemon4.096kbsending Build context to Docker daemon Step0: From Centos:centos7--->7322fbe74aa5step1: RUNYum Install-Y epel-Release---> Runninginch858c0e3e9a22loaded plugins:fastestmirrordetermining fastest mirrors* base:mirrors.yun-idc.com* extras:mirrors.yun-idc.com*updates:mirrors.nwsuaf.edu.cnResolving Dependencies-Running Transaction Check---> Package epel-release.noarch0:7-5Would be installed-finished Dependency resolutiondependencies resolved================================================================================Package Arch Version Repository Size================================================================================Installing:epel-release Noarch7-5Extras -ktransaction Summary================================================================================Install1packagetotal Download Size: -k NPM http $Https//registry.npmjs.org/qs/-/qs-0.6.4.tgzNPM WARN engine [email protected]1.0. -: Wanted: {"node":"<0.9.0"} (current: {"node":"v0.10.36","NPM":"1.3.6"}) [email protected]3.2.4node_modules/Express├──[email protected]0.0.1├──[email protected]0.1.0├──range[Email protected]0.0.4├──cookie[Email protected]1.0.1├──buffer[Email protected]0.2.1├──[email protected]0.0.5├──[email protected]0.6.1├──[email protected]0.3.4├──[email protected]0.1.0([email protected]1.2.6) ├──[email protected]2.2.0([email protected]0.7.1) └──[email protected]2.7.9([email protected]0.0.1, [email protected]0.6.4, [email protected]0.2.0, [email protected]1.0. -) --->66738118fe5dremoving Intermediate Container cad428ec3167step5: EXPOSE8080---> Runninginch9ee7937e5835--->f6ccd16494acremoving Intermediate Container 9ee7937e5835step6: CMD node/src/Index.js---> Runninginch9ca19e7392e9--->ae5f980eea52removing Intermediate container 9ca19e7392e9successfully built Ae5f980eea52[[email protected] src]$
5. View my images
[[email protected] src]$ docker images REPOSITORY TAG IMAGE ID CRE ATED VIRTUAL Sizegideon/centos-node-hello latest ae5f980eea52 about a minute ago408Mbnodejs_hello/node Latest 2DAAC5743DAA theMinutes ago544.7Mbgideon/nodejs Latest f30730d7c2603Days ago544.7Mbnginx Latest 6886fb5a9b8d6Days ago132.8Mbubuntu Latest D2A0ECFFE6FA2Weeks ago188.3Mbcentos Centos7 7322fbe74aa55Weeks ago172.2Mbcentos Latest 7322fbe74aa55Weeks ago172.2Mbhello-world Latest 91c95931e5523Months ago910B[[email protected] src]$
6. Run the image you just generated and map the container's 8080 port to the host's 49161 port
49161:8080 -D gideon/centos-node- Hello
7. In the native input localhost:49161, access the Helloword app.
Note : Add the Epel library to CentOS 7 and use the Yum install epel-release directly (CentOS 5/ 6 You need to download the corresponding RPM package and install it).
Reference: http://docs.docker.com/examples/nodejs_web_app/
Http://www.rackspace.com/knowledge_center/article/install-epel-and-additional-repositories-on-centos-and-red-hat
Docker builds node. JS Web App (with port mappings)