[Docker系列·3] 搭建基于Docker的NodeJS服务器-阿里云开发者社区

开发者社区> 云原生> 正文

[Docker系列·3] 搭建基于Docker的NodeJS服务器

简介: ####0 node应用·盗梦空间 ![2014_08_17_02_25_00](http://img1.tbcdn.cn/L1/461/1/462fcbef2aa688216c90fb6edbe364cc69629528) ####1 docker的NodeJs工作目录 ``` /home/erichan/docker-room/ubuntu-node ``` ####2

0 node应用·盗梦空间

2014_08_17_02_25_00

1 docker的NodeJs工作目录

/home/erichan/docker-room/ubuntu-node

2 docker的Dockerfile

[erichan@mars-centos7 ubuntu-node]$ nano Dockerfile

# Version: 0.0.1
FROM feuyeux/ubuntu-base
MAINTAINER Eric Han "feuyeux@gmail.com"
RUN apt-get -yq install nodejs
RUN apt-get -yq install npm
COPY index.js ./index.js
COPY package.json ./package.json
RUN npm install
EXPOSE 8080
CMD ["nodejs","./index.js"]

3 nodejs工程

[erichan@mars-centos7 ubuntu-node]$ nano package.json

{
  "name": "ubuntu-node",
  "private": true,
  "version": "0.0.5",
  "description": "pi arcas app on CentOS using docker",
  "author": {
    "name":"Eric Han",
    "email":"feuyeux@gmail.com"
  },
  "dependencies": {
      "tracer": "0.7.1"
    },
    "devDependencies": {
        "mocha": "1.21.3",
        "should": "4.0.4",
        "expect": "0.1.1",
        "nodeunit": "0.9.0",
        "date-format-lite": "0.5.0",
        "express":"4.8.4"
    }
}

[erichan@mars-centos7 ubuntu-node]$ nano index.js

var express = require('express');
var config = {
  root: "/tmp",
  format: "{{timestamp}} {{message}}",
  dateformat: "yyyy-mm-dd HH:MM:ss.L"
};

var log = require('tracer').dailyfile(config); 
var PORT = 8080;

var app = express();
app.get('/', function (req, res) {
  log.info(req);
  res.send("Qui, c'est la pi-arcas!\n");
});
app.listen(PORT);
console.log('Running on http://localhost:' + PORT);

4 制作镜像

d build -t feuyeux/ubuntu-node .

5 验证镜像

d images

screenshot

6 运行node

[erichan@mars-centos7 ubuntu-node]$ d run -p 49160:8080 -d feuyeux/ubuntu-node

7 验证NodeJs

查询进程

[erichan@mars-centos7 ubuntu-node]$ d ps -l
CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS              PORTS                             NAMES
658829faa174        feuyeux/ubuntu-node:latest   nodejs ./index.js   4 seconds ago       Up 4 seconds        22/tcp, 0.0.0.0:49160->8080/tcp   focused_davinci     

cURL测试

[erichan@mars-centos7 ubuntu-node]$ curl -i localhost:49160
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 24
ETag: W/"18-1035073832"
Date: Sat, 16 Aug 2014 18:06:11 GMT
Connection: keep-alive

Qui, c'est la pi-arcas!

2014_08_17_02_30_40

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云原生
使用钉钉扫一扫加入圈子
+ 订阅

云原生时代,是开发者最好的时代

其他文章