码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-07-24 20:35:55      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:

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  8080
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 0 : FROM centos:centos7
 ---> 7322fbe74aa5
Step 1 : 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 0:7-5 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-5             extras            14 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 14 k
······
······
······
npm http 200 https://registry.npmjs.org/qs/-/qs-0.6.4.tgz
npm WARN engine formidable@1.0.13: wanted: {"node":"<0.9.0"} (current: {"node":"v0.10.36","npm":"1.3.6"})
express@3.2.4 node_modules/express
├── methods@0.0.1
├── fresh@0.1.0
├── range-parser@0.0.4
├── cookie-signature@1.0.1
├── buffer-crc32@0.2.1
├── cookie@0.0.5
├── commander@0.6.1
├── mkdirp@0.3.4
├── send@0.1.0 (mime@1.2.6)
├── debug@2.2.0 (ms@0.7.1)
└── connect@2.7.9 (pause@0.0.1, qs@0.6.4, bytes@0.2.0, formidable@1.0.13)
 ---> 66738118fe5d
Removing intermediate container cad428ec3167
Step 5 : EXPOSE 8080
 ---> Running in 9ee7937e5835
 ---> f6ccd16494ac
Removing intermediate container 9ee7937e5835
Step 6 : 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   408 MB
nodejs_hello/node          latest              2daac5743daa        52 minutes ago       544.7 MB
gideon/nodejs              latest              f30730d7c260        3 days ago           544.7 MB
nginx                      latest              6886fb5a9b8d        6 days ago           132.8 MB
ubuntu                     latest              d2a0ecffe6fa        2 weeks ago          188.3 MB
centos                     centos7             7322fbe74aa5        5 weeks ago          172.2 MB
centos                     latest              7322fbe74aa5        5 weeks ago          172.2 MB
hello-world                latest              91c95931e552        3 months ago         910 B
[xiejdm@localhost src]$ 

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

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

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

技术分享

 

备注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

 

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

标签:

原文地址:http://www.cnblogs.com/Richard-xie/p/4674368.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!