docker nodejs 基本应用

1. 安装docker 环境

2. nodejs  应用布局

package.json

{
  "name": "docker-centos-hello",
  "private": true,
  "version": "0.0.1",
  "description": "Node.js Hello world app on CentOS using docker",
  "author": "Daniel Gasienica <daniel@gasienica.ch>",
  "dependencies": {
    "express": "3.2.4"
  }
}

3. index.js 使用express

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);
console.log('Running on http://localhost:' + PORT);

4. Dockerfile文件创建

FROM    centos:centos6

# Enable Extra Packages for Enterprise Linux (EPEL) for CentOS
RUN     yum install -y epel-release
# Install Node.js and npm
RUN     yum install -y nodejs npm

# Install app dependencies
COPY package.json /src/package.json
RUN cd /src; npm install

# Bundle app source
COPY . /src

EXPOSE  8080
CMD ["node", "/src/index.js"]

5. 创建镜像

docker build -t dalong/centos-node .

6. 运行创建的镜像

docker run -p 49160:8080 -d dalong/centos-node

其中8080 为容器node 的端口   49160 是docker 宿主环境暴露的端口

7. 测试的效果

   

curl -i localhost:49160

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
Date: Tue, 26 Jan 2016 06:55:32 GMT
Connection: keep-alive

Hello world

 

posted on 2016-01-26 15:02  荣锋亮  阅读(251)  评论(0编辑  收藏  举报

导航