zoukankan      html  css  js  c++  java
  • 01-Docker实战,搭建NodeJs环境

    目的

    实现简单的docker的nodejs容器,使用Dockerfile构建我们的使用nodejs开发的系统

    技术栈

    • Docker
    • Nodejs
    • Express
    • Linux

    step1 下拉nodejs基础容器 node:

    本次我需要使用基础alpine操作系统构建的node基础容器


     
    image.png

    step2 创建项目文件夹

    创建一个空目录,并且创建如下文件

    root@ubuntu-docker:/server/nodejs# ls
    Dockerfile  package.json  server.js
    
     
    image.png

    Dockerfile内容

    FROM node:6.10.2-alpine
    ADD . /server/www/
    WORKDIR /server/www/
    RUN cd /server/www && npm install
    EXPOSE 3001
    CMD ["node","server.js"]
    

    package.json内容

    {
      "name": "docker-node-hello",
      "private": true,
      "version": "0.0.1",
      "description": "Node.js with docker",
      "author": "zhaojunlike@gmail.com",
      "dependencies": {
        "express": "3.2.4"
      }
    }
    

    server.js app的入口文件

    var express = require('express');
    
    // Constants
    var PORT = 3001;
    
    // App
    var app = express();
    app.get('/', function (req, res) {
      res.send('Hello World
    ');
    });
    
    app.listen(PORT)
    console.log('Running on http://localhost:' + PORT);
    
    

    step3 build

    使用以下命令来构建node-app容器

    #docker build -t node-hello ./
    
     
    image.png

    step4 运行测试

    root@ubuntu-docker:/server/nodejs# docker images
    REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
    node-hello                                             latest              58a5182bd055        40 seconds ago      56.34 MB
    root@ubuntu-docker:/server/nodejs# docker run -d -p 3001:3001 node-hello
    0af03634720b3572e860b9d353fd140b88f729e938713f3c4fc694cfda3ec065
    root@ubuntu-docker:/server/nodejs# curl localhost:3001
    Hello World
    root@ubuntu-docker:/server/nodejs# 
    
    

    本节学习源码:https://github.com/zhaojunlike/docker-node-hello



    作者:Godtoy
    链接:https://www.jianshu.com/p/022aadc70c80
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    Linux_C smsh1
    ACM&排序问题,操作符重载
    ACM&找双亲,并查集
    struct dirent/DIR
    关于win8如何查找出当前的密钥
    php之留言板
    php之include&require
    工作中的问题 和 所用到的知识点
    jQuery.extend 和 jQuery.fn.extend
    JavaScript 字符串函数 之查找字符方法(一)
  • 原文地址:https://www.cnblogs.com/gongxianjin/p/10845303.html
Copyright © 2011-2022 走看看