• 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
  • 相关阅读:
    openjudge 2750
    hexo部署云服务器
    freemaker传输数据问题
    FormData在axios中的骚操作
    Docker安装与初次使用
    docker-compose实现前后端分离的自动化部署
    centos7下设置静态ip
    centos7 安装mariadb 并配置主从复制
    centos7安装solr服务
    centos7安装redis
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/5160444.html
Copyright © 2020-2023  润新知