Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of Building docker+jenkins+node.js Automation deployment Environment from scratch

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This case is based on CentOS 7 system.

It is suitable for people who have some experience in using docker. It is suitable for those who have experience in using linux commands.

1. Docker part

1.1introduction to docker

Docker is an open source application container engine that allows developers to package their applications and dependency packages into a portable container, publish them to any popular Linux machine, and virtualize them. Containers completely use sandbox mechanism and will not have any interface with each other.

1.2.The docker architecture

Simply put, docker is a lightweight linux system. The Docker container is created through a Docker image. The relationship between containers and mirrors is similar to objects and classes in object-oriented programming. The docker architecture is shown in the figure:

1.3.The docker virtual machine management command

1.4. install docker

Update the software library

Yum update-y

Install docker

Yum install docker-y

1.5.Starting docker service

Start the docker service

Service docker start

Other related commands

Service docker restart / / restart docker service service docker stop / / stop docker service

2. Node part

You can write any hello-world project through the koa framework, and then create a new Dockerfile file in the root directory of the project

Dockerfile is a text file that contains Instruction, each of which builds a layer, so the content of each instruction is to describe how the layer should be built.

The author's own Dockerfile file configuration information is as follows, which can be configured by those who are familiar with docker.

# Dockerfile# uses node as a mirror FROM node# to create this directory in the container RUN mkdir-p / home/project# sets the container's working directory to the directory WORKDIR / home/project# provides the command CMD npm install-- registry= https://registry.npm.taobao.org & & node. / start.js executed after the creation of the container at port 3000 EXPOSE 300

Publish the project to github to prepare for future jenkins deployment

3. Jenkins part

Query 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 purl 8080-v / root/jenkins_node1:/var/jenkins_home jenkins:latest

Command parsing:

-u 0

It refers to passing root account ID to overwrite the built-in account in the container.

-v / root/jenkins_node1:/var/jenkins_home

Refers to mapping the directory / var/jenkins_home in the docker container to the host / root/jenkins_node1 directory

-- name jenkins_node1

Name the container jenkins_node1

-p 49003purl 8080

Port mapping, mapping port 8080 of the container to port 49003 of the host

-- privileged

Give the highest authority

The meaning of the whole order

Run a container mirrored as jenkins:latest, named jenkins_node1, overwrite the account in the container with root account, grant the highest permission, map the container's / var/jenkins_home to the host's / root/jenkins_node1 directory, and map port 8080 in the container to host 49003.

View jenkins

After the execution is complete, wait for dozens of seconds for the jenkins container to initiate initialization.

You can check whether there are too many files under the host / root/jenkins_node1.

Enter localhost:49003 in the browser to see if jenkins starts successfully.

The following interface indicates that the startup is successful:

Get the password

Cat / root/jenkins_node1/secrets/initialAdminPassword

Copy the output password, paste it into the page, click continue to take you to the following page

Click install

Wait for the installation to complete and go to the interface of creating administrator account

Enter the account password information and click Save (to fill in the information) to go to the home page

Configure jenkins, go to the system management page, and manage plug-ins

Select the ssh plug-in

Install directly, wait for the installation to complete, go back to the home page

Go to system Management-> system configuration

Drag it to the bottom Publish over SSH.

Select Advanced, enter the server IP, user name, password, and then click Test Configuration

Displaying Success indicates that the configuration is fine. Then save it and go back to the home page

Create a new project

Enter project name

Select source code management, use git management, enter github warehouse address, and add github users

Finish and come to choose the build environment

Orders executed

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 保存后,点击立即构建 构建成功后,可以在宿主机的目录/root/jenkins_node1/workspace/node下看到你的项目文件了 在浏览器输入 docker服务器地址 localhost:3000 即可访问到页面信息 启动失败的,可以查看日志来确定一下失败的原因 docker logs nodeapp 4、jenkins + github自动部署 如果想本地代码提交push到github后,jenkins自动拉取最新代码重新部署,请继续看 服务器要外网能访问,本地环境想测试的可以尝试内网穿透 natapp 或者 ngrok 在首页点击用户 首页 ->

User-> root

Click Settings-> show API Token

Copy the values in API Token

Go back to the home page-> node-> configure-> build triggers to paste into the authentication token

Log in to your github project page and open Setting-> Webhooks-> Add webhooks

Add webhooks

Modify the security policy of jenkins

On the home page of jenkins, select system Administration-> Configure Global Security (the one below the system settings) to make the following settings

At this point, after the git push is complete, jenkins automatically builds the automatic deployment.

5. Common commands of docker

Those who are interested in docker can learn about it and continue to study.

Mirror correlation

Query image

Docker search [name]

Pull the image

Docker pull [name]

Import Mirror

Docker load

< /home/node.tar.gz 导出镜像 docker save >

/ home/node.tar.gz

Query all mirrors

Docker images

Delete Mirror

Docker rmi [name]

Modify the image name

Docker tag docker.io/node node

Container dependence

Start

# run and enter interactive mode docker run-it-- name myjava java bash # run docker run-d-name myjava java in the background

Port mapping

Docker run-it-- name myjava-p 9000 java bash 8085-p 9000 Swiss 8086

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 it 8085-p 9000 it 8086 java bash

Pause the container

Docker pause node

Stop pausing the container

Docker unpause node

Stop the container

Docker stop node

Start the container

Docker start-I node

View the container

Docker ps-a

Related to docker network segment

Create 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

The author recently studied node, so I would like to try to achieve this automated deployment process through Docker+jenkins. I stepped on a sinkhole and finally managed to configure it successfully. Previously, pm2 was used to manage node projects, and pm2 was used to automate the deployment of node projects. If you are interested, you can take a look. Use pm2 to automate the deployment of node projects. It's like taking a summary note for yourself. Where the writing is not good, please point out.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report