将Node.js web app部署到docker中

一.创建一个简单的Node.js app

1.1 新建package.json文件

{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <first.last@example.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}

1.2 执行命令 npm install 来加载依赖包,如果npm的版本大于5, 同时会生成一个package-lock.json文件(这个也要copy到docker中)
1.3 新建server.js文件

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

此时,在浏览器中输入 localhost:8080 则会看到字符串’hello world’

二. 创建docker

2.1 新建文件Dockerfile

FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

FROM 定义我们创建的内容,carbon 是docker长期支持的一个node版本
WORKDIR 项目文件在docker中的位置
COPY 复制项目文件到docker中
RUN 执行的命令
EXPOSE docker暴露出的端口(用来和外部的端口做关联)
CMD 运行项目的命令

2.2 新建文件.dockerignore

node_modules
npm-debug.log

复制项目文件到docker时,会忽略这些文件

2.3 创建image-在Dockerfile 文件所在的目录下,执行命令

$ docker build -t <your username>/node-web-app .

-t 代表标签tag,标签能够方便查看和区分image
执行完后,应该时这样的

$ docker images

# Example
REPOSITORY                      TAG        ID              CREATED
node                            carbon     1934b0b038d1    5 days ago
<your username>/node-web-app    latest     d64d3505b0d2    1 minute ago

2.4 运行image

$ docker run -p 49160:8080 -d <your username>/node-web-app

-d 标示让项目在后台运行
-p 制定映射的端口
查看日志

# Get container ID
$ docker ps

# Print app output
$ docker logs <container id>

# Example
Running on http://localhost:8080

如果需要进入容器中,可以用exec命令

# Enter the container
$ docker exec -it <container id> /bin/bash

三.测试docker

$ docker ps

# Example
ID            IMAGE                                COMMAND    ...   PORTS
ecce33b30ebf  <your username>/node-web-app:latest  npm start  ...   49160->8080

8080是容器中的端口,49160是映射在本地的端口
可以通过curl命令来查看(如果没有该命令,安装命令:sudo apt-get install curl)


$ curl -i localhost:49160

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 12
ETag: W/"c-M6tWOb/Y57lesdjQuHeB1P/qTV0"
Date: Mon, 13 Nov 2017 20:53:59 GMT
Connection: keep-alive

Hello world

原文
https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值