Ultra-simple pictures and texts based on Linux use Docker to deploy Node.js project

Understand at a glance with pictures and texts Install and Dockerize your Node.js application based on CentOS Linux release 7.8.2003 (Core)

@[TOC](Install and use Docker based on CentOS Linux release 7.8.2003 (Core))

illustrate

  • This article describes how to deploy and use Docker on CentOS Linux release 7.8.2003.
  • It aims to use the simplest and fastest way to solve the needs of using docker to deploy nodejs.

premise

You need to have a server~~
After purchasing, open it like this below

insert image description here

Remark

  • Because the Alibaba Cloud server was originally used for installation, the ECS instance of the Aliyun Linux 2.1903 LTS 64-bit operating system encountered some problems later, and a lot of information was queried and operated according to the information, but the problem has not been resolved yet, so a colleague's server was used for the installation. install again
  • The specific configuration is as follows
  • View the Linux kernel
uname -a
cat /proc/version

insert image description here

  • View the details of the current system version
1.cat /etc/redhat-release(此方法只适合Redhat 系的Linux)
2.lsb_release -a (此命令适用于所有的Linux 发行版本)

insert image description here

Step 1: Connect to the server

  1. After the link is successful, as shown in the figure below, if you don’t know how to link, you can refer to (this address)
    insert image description here
  2. Run the following commands in sequence to add yum sources.
yum update
yum install epel-release -y
yum clean all
yum list

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

  1. Install and run Docker.
yum install docker-io -y
systemctl start docker

insert image description here
insert image description here
insert image description here

  1. To solve the error report, check the error message: (Note, if you use CentOS Linux, you can skip this step directly, and generally there are no problems below)
    insert image description here
[root@iZ2ze67ifeuz62igol9apzZ ~]# docker -v
Docker version 1.13.1, build 4ef4b30/1.13.1
[root@iZ2ze67ifeuz62igol9apzZ ~]#  systemctl status docker.service -l
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 日 2020-06-28 15:31:55 CST; 6min ago
     Docs: http://docs.docker.com
  Process: 24481 ExecStart=/usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland-proxy-path=/usr/libexec/docker/docker-proxy-current --init-path=/usr/libexec/docker/docker-init-current --seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES (code=exited, status=1/FAILURE)
 Main PID: 24481 (code=exited, status=1/FAILURE)

628 15:31:54 iZ2ze67ifeuz62igol9apzZ systemd[1]: Starting Docker Application Container Engine...
628 15:31:54 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: time="2020-06-28T15:31:54.799852050+08:00" level=warning msg="could not change group /var/run/docker.sock to docker: group docker not found"
628 15:31:54 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: time="2020-06-28T15:31:54.800671443+08:00" level=info msg="libcontainerd: new containerd process, pid: 24490"
628 15:31:55 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: time="2020-06-28T15:31:55.813485177+08:00" level=error msg="'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded."
628 15:31:55 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: time="2020-06-28T15:31:55.814654057+08:00" level=error msg="'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded."
628 15:31:55 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: Error starting daemon: error initializing graphdriver: devicemapper: Error running deviceCreate (CreatePool) dm_task_run failed
628 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
628 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: Failed to start Docker Application Container Engine.
628 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: Unit docker.service entered failed state.
628 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: docker.service failed.

The solution is as follows
Uninstall the old version of docker and its related dependencies

# 1、先查询所有安装的包

yum list installed | grep docker*
# 或者
rpm -qa docker*
# 2、删除查询出来的包

# 一般情况会有一个
yum remove -y docker-ce
# 18.09新增了cli工具
yum remove -y docker-ce-cli
# 3、删除镜像文件

rm -rf /var/lib/docker
rm -rf /var/lib/docker*

Upgrade the linux kernel and reinstall docker:

#查看已经安装的和未安装的软件包组,来判断我们是否安装了相应的开发环境和开发库;
yum grouplist
#一般是安装这两个软件包组,这样做会确定你拥有编译时所需的一切工具
yum groupinstall "Development Tools"
#你必须这样才能让 make *config 这个指令正确地执行
yum install ncurses-devel
#如果你没有 X 环境,这一条可以不用
yum install qt-devel
#创建 CentOS-6 内核时需要它们
yum install hmaccalc zlib-devel binutils-devel elfutils-libelf-devel 
  • Sorry, the above error report has not been resolved, and I have found many ways to deal with it, but there is still no result, so I changed my colleague's server and installed it according to the above steps.
  • The following content is based on CentOS Linux release 7.8.2003 (Core)
  1. Check the installed version and start docker
docker version
systemctl start docker

insert image description here
insert image description here
6. Verify that the installation is successful

docker info

insert image description here
7. Set startup

 chkconfig docker on

insert image description here

Configure Docker

  1. Switch domestic image sources to speed up access to Docker Hub
echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker
systemctl daemon-reload
service docker restart

insert image description here
2. Install and configure the node environment

docker search node
docker pull docker.io/node:7.8.0

insert image description here
insert image description here

  1. Create a Dockerfile in your favorite directory
[root@VM_0_5_centos ~]# cd /var/opt/test
[root@VM_0_5_centos test]# ll
总用量 0
[root@VM_0_5_centos test]# touch Dockerfile

insert image description here
insert image description here

  1. Suppose the startup command of the Node.js application is node server.js, and the listening port is 8090
# 可以指定依赖的node镜像的版本 node:<version>,如果不指定,就会是最新的
FROM node
WORKDIR /app

# 把其他源文件复制到工作目录
COPY ./app /app
# 把 package.json,package-lock.json(npm@5+) 或 yarn.lock 复制到工作目录(相对路径)
RUN npm install
# 只安装dependencies依赖
# 替换成应用实际的端口号
EXPOSE 8090
# 这里根据实际起动命令做修改
CMD [ "node", "server.js" ]


insert image description here
5. Add .dockerignore

touch .dockerignore

The content of .dockerignore is as follows

node_modules
npm-debug.log

insert image description here
insert image description here
6. Create an image

docker build -t node78:v1 .

Before the Docker daemon process executes the instructions in the Dockerfile, it will first check the syntax of the Dockerfile, and if there is a syntax error, it will return: (If you follow the above Dockerfile configuration file, this error will not be reported, the configuration file was modified later, and Re-screenshot~)

[root@VM_0_5_centos test]# docker build -t node78:v1 .
Sending build context to Docker daemon 3.584 kB
Error response from daemon: Unknown instruction: ​:
  1. view mirror
docker images
# 示例输出
REPOSITORY                      TAG        ID              CREATED
node                            8          1934b0b038d1    5 days ago
${your_name}/${image_name}    latest     d64d3505b0d2    1 minute ago

insert image description here

  1. Try docker run node, not only can you find the mirror, but if you don’t have it, it will help you download the latest version.
    Because what I downloaded is not the latest mirror, it will automatically start to update.
docker run node

insert image description here
9. View mirror


You will see that we have two images, one is the latest version, and the other is the initial downloaded version. If you don’t want it, you can delete it later~ Don’t execute the docker run command in the picture~

docker image ls

insert image description here

  1. Prepare a simple nodejs file
var http = require('http');

http.createServer(function (request, response) {
    
    


    response.writeHead(200, {
    
     'Content-Type': 'text/plain' });


    response.end('Hello World\n');
}).listen(8090);

console.log('Server running at http://127.0.0.1:8090/');

insert image description here
insert image description here
insert image description here

  1. Build a mirror image, don’t forget that there is a little bit behind
docker build -t docker.io/node:latest .

insert image description here

  • start container script
docker run -d -p 8090:8090  docker.io/node:latest 

insert image description here
12. Test, successful connection
insert image description here

Congratulations, the deployment is successful!

Common commands

  • In addition, some common commands are attached:
# 查看所有容器(包括已终止的)
docker ps -a
# 或使用新命令
docker container ls -a
# 查看运行中的容器
docker ps
# 或使用新命令
docker container ls
# 示例输出
ID            IMAGE                                COMMAND    ...   PORTS
ecce33b30ebf  ${your_name}/${image_name}:latest  npm start  ...   49160->8080
# 查看某容器内日志
docker logs -f ${container_id}
# 进入某容器,并有shell执行环境
# 进入容器
# -i表示:交互式操作,-t表示:终端
docker exec -it ${container_id} bash
# 可通过输入 exit 退出 
# 停止容器
docker container stop ${container_id}
# 启动已终止的容器
docker container start ${container_id}
#删除容器
docker container rm ${container_name || container_id}

#查看镜像
docker images
# 或使用新命令
docker image ls

#删除镜像
docker image rm ${image_id}

#其他命令
doctor run -it -d --name springboot -p 8080:8080 springboot:latest
-d 后台运行容器,并返回容器ID
-i  以交互模式运行容器,通常与-t同时使用
-t  为容器新分配一个伪输入终端,通常与i同时使用
-P  大写,随机端口映射,容器内部端口随机
-p 小写,指定端口映射,格式为: 主机端口:容器端口
--name 重命名
FROM 100.111.222.111/library/centos/jdk:1.8.0 -----OPEANJDK:8
从镜像下载JDK
FROM 是指需要仓库下载一个JDK的镜像作为底层系统,然后在该JDK上执行后面那些操作。

epilogue

This tutorial aims to quickly complete the deployment of the Node.js project. The details of other configuration items are not listed. The configuration article will be added later~~ Welcome to point out the article that needs to be
corrected~
If there is a better method, welcome Everyone put forward, let's make progress together~~

Je suppose que tu aimes

Origine blog.csdn.net/tangdou369098655/article/details/107007920
conseillé
Classement