• 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
    ');
    });
    
    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
  • 相关阅读:
    当简单的计算遇上了大数,其实大数运算也很简单
    揭开源码的神秘面纱,让源码从此无处藏身
    JAVA对象和XML文档、原来他们之间还有这一出
    JAVA反射其实就是那么一回事
    Metatable让我从心认知了Lua(相知篇)
    Github
    常见问题汇总
    文章目录
    前后端分离下使用SignalR
    IdentityServer_0_参考资料
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/5160444.html
Copyright © 2020-2023  润新知