Docker 生成Node.js web app(含端口映射)

1.新建目录src,并进入src目录

[xiejdm@localhost Documents]$ mkdir src
[xiejdm@localhost Documents]$ cd src/

2.创建package.json和index.js文件,文件内容如下:

package.json

[xiejdm@localhost Documents]$ mkdir src
[xiejdm@localhost Documents]$ cd src/
[xiejdm@localhost src]$ cat package.json
{
"name": "docker-centos-hello",
"private": true,
"version": "0.0.1",
"description": "Node.js Hello world app on CentOS using docker",
"author": "Gideon xie <xiejdml@gmail.com>",
"dependencies": {
"express": "3.2.4"
}
}

index.js

[xiejdm@localhost src]$ cat 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.创建Dockfile

[xiejdm@localhost src]$ cat Dockerfile
FROM centos:centos7 # Enable EPEL for Node.js
RUN yum install -y epel-release
# Install Node.js and npm
RUN yum install -y npm # Bundle app source
COPY . /src
# Install app dependencies
RUN cd /src; npm install EXPOSE
CMD ["node", "/src/index.js"
]
[xiejdm@localhost src]$

4.使用docker build构建生成镜像

[xiejdm@localhost src]$ docker build -t gideon/centos-node-hello .
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon
Step : FROM centos:centos7
---> 7322fbe74aa5
Step : RUN yum install -y epel-release
---> Running in 858c0e3e9a22
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.yun-idc.com
* extras: mirrors.yun-idc.com
* updates: mirrors.nwsuaf.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch :- will be installed
--> Finished Dependency Resolution Dependencies Resolved ================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
epel-release noarch - extras k Transaction Summary
================================================================================
Install Package Total download size: k
······
······
······
npm http https://registry.npmjs.org/qs/-/qs-0.6.4.tgz
npm WARN engine formidable@1.0.: wanted: {"node":"<0.9.0"} (current: {"node":"v0.10.36","npm":"1.3.6"})
express@3.2. node_modules/express
├── methods@0.0.
├── fresh@0.1.
├── range-parser@0.0.
├── cookie-signature@1.0.
├── buffer-crc32@0.2.
├── cookie@0.0.
├── commander@0.6.
├── mkdirp@0.3.
├── send@0.1. (mime@1.2.)
├── debug@2.2. (ms@0.7.)
└── connect@2.7. (pause@0.0., qs@0.6., bytes@0.2., formidable@1.0.)
---> 66738118fe5d
Removing intermediate container cad428ec3167
Step : EXPOSE
---> Running in 9ee7937e5835
---> f6ccd16494ac
Removing intermediate container 9ee7937e5835
Step : CMD node /src/index.js
---> Running in 9ca19e7392e9
---> ae5f980eea52
Removing intermediate container 9ca19e7392e9
Successfully built ae5f980eea52
[xiejdm@localhost src]$

5.查看我的镜像

[xiejdm@localhost src]$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
gideon/centos-node-hello latest ae5f980eea52 About a minute ago MB
nodejs_hello/node latest 2daac5743daa minutes ago 544.7 MB
gideon/nodejs latest f30730d7c260 days ago 544.7 MB
nginx latest 6886fb5a9b8d days ago 132.8 MB
ubuntu latest d2a0ecffe6fa weeks ago 188.3 MB
centos centos7 7322fbe74aa5 weeks ago 172.2 MB
centos latest 7322fbe74aa5 weeks ago 172.2 MB
hello-world latest 91c95931e552 months ago B
[xiejdm@localhost src]$

6.运行刚刚生成的镜像,并将容器的8080端口映射到主机的49161端口

[xiejdm@localhost src]$ docker run -p : -d gideon/centos-node-hello
71baf591eb27a2373daf5a802ee9406e635878ee1b1fcd047eb3f39e476b4406
[xiejdm@localhost src]$

7.在本机输入localhost:49161,访问helloword应用。

Docker 生成Node.js web app(含端口映射)

备注: CentOS 7中增加EPEL库,直接使用yum install epel-release即可(CentOS 5/6则需要下载对应的rpm包然后进行安装)。

参考: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

上一篇:PHP中spl_autoload_register()函数用法实例详解


下一篇:eclipse重定向输入输出到文件