小小又开始学习新的内容了。这次学习的是,把一个Node.js 应用封装到Docker容器,完成本教程的前提是拥有一个可以安装的,已经正常可以工作的Docker。以及对Node.js应用如何工作,有一个大致的了解。本教程的第一部分,需要创建一个Web应用程序,然后为这个应用程序构建一个Docker镜像,最后把这个镜像作为容器进行运行。Docker允许应用对依赖进行打包完成一个标准化的单元,这是一个容器,对于应用而言,Docker被称为一个标准的Linux操作系统,一个镜像是进行加载到容器的软件。创建Node.js应用
创建Node.js应用
首先,需要创建一个package.json文件,以及包含的依赖。
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last ",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}
{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last ",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.1"
}
}然后输入npm install 进行安装相关的依赖。npm install 然后创建一个server.js 文件,创建一个web应用。
'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');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
'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');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);这样就完成了一个标准的,最简单的web应用。下面将需要创建一个镜像,用于对镜像进行封装。创建一个 Dockerfile文件创建一个 Dockerfile文件
touch Dockerfile
touch Dockerfile打开文件,输入相关的基础镜像
FROM node:12
FROM node:12创建相关的工作目录
# Create app directory
WORKDIR /usr/src/app
# 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 ci --only=production
# 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 ci --only=production复制相关的程序代码
# Bundle app source
COPY . .
# Bundle app source
COPY . .绑定相关的端口号
EXPOSE 8080
EXPOSE 8080创建持久化的命令,让系统在前台运行。
CMD [ "node", "server.js" ]
CMD [ "node", "server.js" ]最后Dockerfile构建如下
FROM node:12

# 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 ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]
FROM node:12

# 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 ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]关于dockerignore文件关于dockerignore文件此文件,是防止复制到相关的文件,例如node_modules 不需要复制到docker镜像内部
node_modules
npm-debug.log
node_modules
npm-debug.log构建docker镜像构建docker镜像
docker build -t /node-web-app .
docker build -t /node-web-app .输入如上的命令,构建docker镜像。最后构建出的docker镜像如下
$ docker images

# Example
REPOSITORY
TAG
ID
CREATED
node
12
1934b0b038d1 5 days ago
/node-web-app latest
d64d3505b0d2 1 minute ago
$ docker images

# Example
REPOSITORY
TAG
ID
CREATED
node
12
1934b0b038d1 5 days ago
/node-web-app latest
d64d3505b0d2 1 minute ago运行相关镜像运行相关镜像此时镜像已经构建完成,这里需要对镜像进行运行。
docker run -p 49160:8080 -d /node-web-app
docker run -p 49160:8080 -d /node-web-app需要进入容器,输入如下的命令
# Enter the container
$ docker exec -it /bin/bash
# Enter the container
$ docker exec -it /bin/bash测试测试输入ps,可以看到docker的镜像的详细内容
$ docker ps

# Example
ID
IMAGE
COMMAND ... PORTS
ecce33b30ebf /node-web-app:latest npm start ... 49160->8080
$ docker ps

# Example
ID
IMAGE
COMMAND ... PORTS
ecce33b30ebf /node-web-app:latest npm start ... 49160->8080使用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 2021 20:53:59 GMT
Connection: keep-alive

Hello world
$ 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 2021 20:53:59 GMT
Connection: keep-alive

Hello world以上就是Node.js web 应用如何封装到Docker容器中的详细内容,关于Node.js 应用封装到Docker容器的资料请关注其它相关文章!