DEV Community

tangdoudou
tangdoudou

Posted on

Use Docker to create a Node Development Environment Based on CentOS Linux release 7.8.2003 Core

@🌺Use Docker to create a Node Development Environment Based on CentOS Linux release 7.8.2003 Core 🌺

English | 简体中文

🍋🍓Introduction🍓🍋

  • This article describes how to deploy and use docker in CentOS Linux release 7.8.2003 🍒
  • The purpose is to use the simplest and fastest way to solve the deployment requirements of Nodejs using Docker 🍀

Precondition🍒

You need a server😆~~
Open it, just like this on below 🍓

在这里插入图片描述

Remarks

  • Because the alicloud server was originally used for the installation, the ECS instance of aliyun Linux 2.1903 LTS 64 bit operating system encountered some problems in the later stage. After querying a lot of data and operating according to the data, but the problem has not been solved for the time being, so I used my colleague's server to install it again.
  • The specific configuration is as follows
  • View Linux Kernel
uname -a
cat /proc/version
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  • View the details of the current system version
1.cat /etc/redhat-release(This method is only suitable for RedHat Linux)
2.lsb_release -a (This command applies to all Linux distributions)
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

Step 1: Link server 🍓

  1. After the link is successful, it is shown in the figure below. If you don't know how to link, you can refer to here

在这里插入图片描述

  1. Run the following commands in turn to add the yum source.🍏
yum update
yum install epel-release -y
yum clean all
yum list
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. Install and run Docker。😍
yum install docker-io -y
systemctl start docker
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. Solve the error report and check the error information: (Note: if you use CentOS Linux, you can skip this step directly, generally there is no problem below)🍉 在这里插入图片描述
[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)

6月 28 15:31:54 iZ2ze67ifeuz62igol9apzZ systemd[1]: Starting Docker Application Container Engine...
6月 28 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"
6月 28 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"
6月 28 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."
6月 28 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."
6月 28 15:31:55 iZ2ze67ifeuz62igol9apzZ dockerd-current[24481]: Error starting daemon: error initializing graphdriver: devicemapper: Error running deviceCreate (CreatePool) dm_task_run failed
6月 28 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
6月 28 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: Failed to start Docker Application Container Engine.
6月 28 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: Unit docker.service entered failed state.
6月 28 15:31:55 iZ2ze67ifeuz62igol9apzZ systemd[1]: docker.service failed.

Enter fullscreen mode Exit fullscreen mode

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

# 1、Query all installed packages first

yum list installed | grep docker*
# Perhaps
rpm -qa docker*
# 2、Delete the query package

# Generally, there will be one
yum remove -y docker-ce
# Added cli tool
yum remove -y docker-ce-cli
# 3、Delete image file

rm -rf /var/lib/docker
rm -rf /var/lib/docker*
Enter fullscreen mode Exit fullscreen mode

Upgrade the Linux kernel and re install docker:

#Check the installed and non installed packages to detemine whether we have installed the corresponding development environment and development library
yum grouplist

#This is usually done by installing both packages, which will make sure you have all the tools you need to compile
yum groupinstall "Development Tools"
#You have to do this to get the make * config command to execute correctly.
yum install ncurses-devel
#If you don't have this environment, you can use this one.
yum install qt-devel
#They are required to create the CentOS-7 Kernel.
yum install hmaccalc zlib-devel binutils-devel elfutils-libelf-devel 
Enter fullscreen mode Exit fullscreen mode
  • Sorry, the above error has not been solved, and many methods have been found to deal with it, but there is no result. So I changed my colleague's server and installed it according to the above steps.
  • The following is based on CentOS Linux release 7.8.2003 (Core)
  1. Check the installation version and start docker.
docker version
systemctl start docker
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述

  1. Verify that the installation was successful
docker info
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  1. Set boot up
 chkconfig docker on

Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

Configure Docker

  1. Switch domestic image source to accelerate access to Docker Hub
echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker
systemctl daemon-reload
service docker restart
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  1. Install and configure node environment.
docker search node
docker pull docker.io/node:7.8.0

Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述

  1. Create a dockerfile file 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

Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述

  1. Hypothesis Node.js The start command of the application is node server.js, The listening port is 8090
# You can specify the version of the dependent node image  node:<version>, if not specified, it will be the latest.
FROM node
WORKDIR /app

# Copy other source files to the working directory
COPY ./app /app
# Put package.json,package-lock.json(npm@5+) Or yarn.lock Copy to working directory(relstive path)
RUN npm install
# Install dependencies only
# Replace with the actual port number of the application 
EXPOSE 8090
# It is modified according to the actual start command
CMD [ "node", "server.js" ]


Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  1. Supplement .dockerignore
touch .dockerignore
Enter fullscreen mode Exit fullscreen mode

.dockerignore reads as follows

node_modules
npm-debug.log
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述

  1. Create mirror😍 🍒
docker build -t node78:v1 .

Enter fullscreen mode Exit fullscreen mode

Before the Docker deamons execute the instructions in the Dockerfile ,it will first check the syntax of the dockerfile. If there is a syntax error, it will be returned: (if you follow the above dockerfile cofiguration file, this error will not be reported. The configuration file has been modified later, and the screenshot is taker again.)

[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: ​:
Enter fullscreen mode Exit fullscreen mode
  1. 🍒 View image💌
docker images
​
# Sample output
REPOSITORY                      TAG        ID              CREATED
node                            8          1934b0b038d1    5 days ago
${your_name}/${image_name}    latest     d64d3505b0d2    1 minute ago
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  1. Try 'docker run node', which can not only find images, but also help you download the latest version if there is no node image. Because I don't download the latest image, it will automatically start to download the latest version.
docker run node
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  1. View image >You will see that we have two images, one is the latest version and the other is the original download version. If you don't want to delete it later~ >Do not execute the docker run command in the picture~
docker image ls
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

  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/');
Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  1. Build the image, don't forget there is a little bit behind it.
docker build -t docker.io/node:latest .

Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述

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

Enter fullscreen mode Exit fullscreen mode

在这里插入图片描述
12.Test, successful connected 🍉
在这里插入图片描述

Congratulations on the successful depluyment!
## Common commands

  • In addition, some common commands are attached:
# view all containers (including terminated)
docker ps -a
# or use the new command


docker container ls -a
Enter fullscreen mode Exit fullscreen mode
# view running containers
docker ps
# or use the new command
docker container ls# sample output
ID            IMAGE                                COMMAND    ...   PORTS
ecce33b30ebf  ${your_name}/${image_name}:latest  npm start  ...   49160->8080
Enter fullscreen mode Exit fullscreen mode
# view logs in a container
docker logs -f ${container_id}
Enter fullscreen mode Exit fullscreen mode
# 进入某容器,并有shell执行环境
# 进入容器
# -i表示:交互式操作,-t表示:终端
# enter a container and have a shell execution environment
# enter the container
# -i for interactive operation,-t for terminal
docker exec -it ${container_id} bash
# you can exit by typing 'exit'
Enter fullscreen mode Exit fullscreen mode
# stop container
docker container stop ${container_id}
Enter fullscreen mode Exit fullscreen mode
# start terminated container
docker container start ${container_id}
Enter fullscreen mode Exit fullscreen mode
# delete container
docker container rm ${container_name || container_id}

# view image
docker images
# or use the new command
docker image ls

# delete mirror
docker image rm ${image_id}
Enter fullscreen mode Exit fullscreen mode

conclusion🍉💌

this tutorial is designed to be done quickly 'node.js' project deployment, details of other configuration items are not listed, configuration articles will be 🍒 added later ~~

welcome to point out that the article needs to be corrected 🍇 ~~

if there is a better way, we are welcome to come forward and make progress together🍓 ~

Top comments (1)

Collapse
 
sammiee profile image
sammie

I personally think local server environment is a better way, such as Servbay, because docker uses too much system resources. Install it and you don't need to deploy any environment.
It's worth to try.