This case is based on CentOS 7 system
1. Docker part 1.1 Introduction to Docker Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and then publish it to any popular Linux machine, and can also achieve virtualization. The containers are completely sandboxed and there are no interfaces between them. 1.2、Docker architecture Simply put, Docker is a lightweight Linux system. Docker containers are created from Docker images. The relationship between containers and images is similar to that between objects and classes in object-oriented programming. The docker architecture is shown in the figure: 1.3. Docker virtual machine management commands 1.4. Install Docker Update software library yum update -y Install Docker yum install docker -y 1.5. Start the Docker service Start the Docker service service docker start Other related commands service docker restart // Restart the docker service service docker stop // Stop the docker service 2. Node part You can use the Koa framework to write a hello-world project, and then create a new Dockerfile file in the root directory of the project. Dockerfile is a text file that contains instructions. Each instruction builds a layer, so the content of each instruction describes how the layer should be built. The configuration information of my own Dockerfile is as follows. People who are familiar with Docker can configure it by themselves. # Dockerfile # Use node as mirror FROM node # Create the directory in the container RUN mkdir -p /home/project # Set the working directory of the container to this directory WORKDIR /home/project # Provide port 3000 to the outside world EXPOSE 3000 # Command executed after the container is created CMD npm install --registry=https://registry.npm.taobao.org && node ./start.js Publish the project to GitHub to prepare for subsequent Jenkins deployment 3. Jenkins part Query the Jenkins image docker search jenkins Pull the latest Jenkins image docker pull jenkins:latest Start Jenkins sudo docker run -d -u 0 --privileged --name jenkins_node1 -p 49003:8080 -v /root/jenkins_node1:/var/jenkins_home jenkins:latest Command analysis:
Refers to passing in the root account ID to overwrite the built-in account in the container
Refers to mapping the directory /var/jenkins_home in the docker container to the host machine /root/jenkins_node1 directory
Name the container jenkins_node1
Port mapping, mapping the container's port 8080 to the host's port 49003
Grant highest authority The meaning of the entire command Run a container with the image jenkins:latest, named jenkins_node1, use the root account to overwrite the account in the container, grant the highest authority, map the container's /var/jenkins_home to the host's /root/jenkins_node1 directory, and map port 8080 in the container to port 49003 on the host. View Jenkins After the execution is complete, wait for dozens of seconds for the Jenkins container to start initialization. You can check whether there are many more files under the host/root/jenkins_node1 Enter localhost:49003 in the browser to check whether Jenkins is started successfully The following interface indicates successful startup: Get Password cat /root/jenkins_node1/secrets/initialAdminPassword Copy the output password, paste it into the page, and click continue to enter the following page Click Install Wait for the installation to complete and enter the administrator account creation interface Enter the account and password information and click Save (complete the information) to go to the home page Configure Jenkins, enter the system management page, and manage plug-ins Select the ssh plugin, Install directly, wait for the installation to complete, and return to the home page. Go to System Management -> System Configuration Drag to the bottom and click Publish over SSH. Select Advanced, enter the server IP, username, password, and click Test Configuration If Success is displayed, it means that the configuration is correct. Then save and return to the home page Create a new project Enter the project name Select source code management, use git management, enter the github repository address, and add a github user Finish and come to select the build environment, Executed commands sudo docker stop nodeapp || true \ && sudo docker rm nodeapp || true \ && cd /root/jenkins_node1/workspace/node \ && sudo docker build --rm --no-cache=true -t node - < Dockerfile \ && sudo docker run -d --privileged=true --name nodeapp -p 3000:3000 -v /root/jenkins_node1/workspace/node:/home/project node After saving, click Build Now After the build is successful, you can see your project files in the host directory /root/jenkins_node1/workspace/node Enter the docker server address localhost:3000 in the browser to access the page information If the startup fails, you can check the log to determine the cause of the failure. docker logs nodeapp 4. Jenkins + GitHub automatic deployment If you want Jenkins to automatically pull the latest code and redeploy it after submitting and pushing the local code to GitHub, please continue reading The server needs to be accessible from the Internet. If you want to test it locally, you can try to penetrate the intranet with natapp or ngrok Click User on the home page Home -> Users -> root Click Settings -> Show API Token Copy the value in the API Token Go back to Home -> Node -> Configuration -> Build Triggers and paste in the Authentication Token Log in to your github project page and open Settings -> Webhooks -> Add webhooks Adding webhooks Modify Jenkins security policy On the Jenkins homepage, select System Management -> Configure Global Security (the one below System Settings) and set the following At this point, after the git push is completed, Jenkins automatically builds and deploys. 5. Common commands of docker Those who are interested in docker can learn more and continue to learn Mirror related Query image docker search [name] Pull the image docker pull [name] Importing an Image docker load < /home/node.tar.gz Exporting an Image docker save > /home/node.tar.gz Query all images docker images Deleting an image docker rmi [name] Modify the image name docker tag docker.io/node node Container related start up # Run and enter interactive mode docker run -it --name myjava java bash # Run in the background docker run -d --name myjava java Port Mapping docker run -it --name myjava -p 9000:8085 -p 9000:8086 java bash Directory Mapping docker run -it --name myjava -v /home/project:/soft --privileged docker.io/node bash Enter the container running in the background docker exec -it name bash Automatic restart docker run --restart=always -it --name myjava -p 9000:8085 -p 9000:8086 java bash Pause a container docker pause node Stop and pause the container docker unpause node Stop the container docker stop node Start the container docker start -i node View Container docker ps -a Docker network segment related Creating a network segment docker network create net1 View network segment information docker network inspect net1 Delete network segment information docker network rm net1 6. Summary I have been learning node recently, so I wanted to try to implement this automated deployment process through Docker+jenkins. After a day of hard work, I finally succeeded in the configuration. Previously, pm2 was used to manage node projects, and pm2 was used to automatically deploy node projects. If you are interested, you can take a look. Use pm2 to automatically deploy the node project. It's a summary note for myself. Please point out any poorly written parts. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Summary of several commonly used string methods in JavaScript (must-read for beginners)
>>: Summary of Problems in Installation and Usage of MySQL 5.7.19 Winx64 ZIP Archive
Effect: Ideas: Use the input type attribute to di...
Table of contents Component Design Defining the f...
Download image docker pull mysql:5.7 docker pull ...
Unicode Signature BOM - What is the BOM? BOM is th...
Chapter 1: Introduction to keepalived The purpose...
When the page is not responding, displaying the l...
1. Install the database 1) yum -y install mysql-s...
1. Why does nginx use gzip? 1. The role of compre...
Table of contents 1. Basic grammar 2. Filter by c...
This article shares the specific code for impleme...
Table of contents Related dependency installation...
There are probably as many modular solutions for ...
Table of contents vue custom directive Global Dir...
Since Uniapp does not have DingTalk authorization...
Build the image There are two main ways to build ...