Table of contents
Install Docker to run the environment (CentOs)
Or use domestic one-key installation
Install the Node.js image from the repository
Enter the container deployment Express environment and code
Enter Bash command line interaction
Install express framework and pm2 via npm
Verify that the deployment was successful
Docker architecture
Docker contains three concepts container ( Container ), image ( Image ), warehouse ( Repository ),
We can create countless containers on a host machine to run services such as PHP, Node.js, etc. These containers are all created with images (Image) as templates, and these images are all stored in the warehouse (Repository).
Docker official warehouse https://hub.docker.com/
Domestic acceleration warehouse http://hub-mirror.c.163.com
Install Docker to run the environment (CentOs)
Official installation command
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Or use domestic one-key installation
curl -sSL https://get.daocloud.io/docker | sh
manual installation
#删除旧的版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
#配置阿里云的源
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装docker相关
sudo yum install docker-ce docker-ce-cli containerd.io
#启动docker
sudo systemctl start docker
Uninstall Docker
#删除安装包
yum remove docker-ce
#删除镜像配置文件等
rm -rf /var/lib/docker
Install the Node.js image from the repository
sudo docker search node
In the first line below, we see the node image searched in the warehouse and then we pull the image
Pull the node image from the warehouse
sudo docker pull node
After pulling, check whether the local image has a node image and execute the following command
sudo docker images node
We found that the local image already contains the node image
Docker mounts node image
After the image is downloaded, we need to mount the node image to the Docker Host and execute the following command
#进入 交互状态 并且后台运行容器,将容器3000端口映射为主机3000端口
sudo docker run -itd --name test1 -p 3000:3000 node /bin/bash
After the command is executed, we execute sudo docker ps and we will find that the container has been started and port mapping has been implemented
Enter the container deployment Express environment and code
Enter Bash command line interaction
//进入test1 容器的bash交互
sudo docker exec -it test1 bash
Install express framework and pm2 via npm
//创建代码目录
mkdir -p /home/www/express && cd /home/www/express
//安装pm2
npm install pm2 -g
//安装express
npm install express --save
Deploy Express code
After the installation is complete, we create app.js in the express directory as follows
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('hello docker')
})
app.listen(3000)
pm2 starts express service
pm2 start app.js
Verify that the deployment was successful
We exit the container and check the running status of the container, as shown in the figure below
The express service has been successfully run, and the mapping between port 3000 of the container and port 3000 of the host has been realized
Let's open a browser on the host machine to visit http://127.0.0.1:3000 to see if we can access it. The results are as follows. So far we have successfully built the express service through docker.
So far, we have a preliminary understanding of how to use docker. For more usage methods, please refer to the official manual
Docker official documentation https://docs.docker.com/engine/install/centos/