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

How to deploy a web project using jenkins in a Docker container

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use jenkins in the Docker container to deploy the web project", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use jenkins in the Docker container to deploy the web project" bar!

(1) you need to install the docker container and install jenkins,gogs,tomcat in the docker container. Create a new maven project and add findbugs plugin.

The command to start jenkins,gogs,tomcat using docker: gogs:

The copy code is as follows:

Docker run-itd-p 10022 restart=always-- privileged=true-- name=gogs-v / var/gogs:/data gogs/gogs

Jenkins:

The copy code is as follows:

Docker run-itd-p 8800 restart=always-- privileged=true-- name=jenkins-v / home/jenkins:/var/jenkins_home jenkins

Tomcat:

The copy code is as follows:

Docker run-itd-p 8080 restart=always-- privileged=true-- name=tomcat-v / usr/local/tomcat:/var/tomcat_home

Tomcat:8.0

The copy code is as follows:

Docker run-itd-p 8080 restart=always-- privileged=true-- name=tomcat-v / usr/local/tomcat:/home/tomcat/tomcat_home tomcat:8.0

Later, start the command of tomcat:

Explanation:

-I: means to open it interactively

-d: running in the background

-t: pseudo terminal

-p: the port number in front of the specified port is the port number that you specify the user to access, and the following refers to the default port number of the software.

-- restart=always: makes the program always running and starts automatically

-- privileged=true: related to the firewall, selinux permissions (setting this program will not be affected by the firewall)

-- name: specifies the name of the container to run

-v: container mount, with the real data volume in front and the mount directory in the back

The last gogs/gogs jenkins tomcat:8.0 is the mirror name, followed by the argument to the docker pull command

(2) install plug-ins on jenkins: maven intergration plugin, gogs-plugin, publish over ssh, findbugs-plugin,deploy to a container (jdk and git all use the default in docker, and you do not need to configure the paths of these two items when installing jenkins)

(3) tomcat needs to configure users: through find /-name "tomcat", find the installation path of tomcat, and then add the content to this location in the conf/tomcat-users.xml file.

(4) when gogs creates the warehouse, remember to privatize, configure the git hook, and add the pre-commit file in the .git / hooks/ directory. The contents of the pre-commit file are as follows

#! / bin/sh#execute shell before commit,check the codemvn clean install#recieve the execute resultresult=$?#output the result, if the result less or equal 0, it proves this project has bugs,otherwise don't.echo $resultif [$result-ne 0] then mvn findbugs:gui echo "regretful! build failure" exit 1else echo "congraturation! build success" exit 0fi

Note: if there is a 403 error when configuring webhook, check to see if the plug-in gogs-plugin is installed in jenkins (because I made an error for a long time, just because gogs-plugin is not installed)

Webhook example: http://172.150.15.9:8800/gogs-webhook/?job=webdemoin7 / / webdemoin7 is my enkins project name

(5) when creating a maven project, the contents of the pom.xml

4.0.0 cn.demo webdemoin7 war 0.0.1-snapshot webdemoin7 maven webapp http://maven.apache.org webdemoin7 true org.apache.maven.plugins maven-compiler-plugin 3.5.1 ${compiler.source} ${compiler.target} ${project.build.sourceencoding} ${project .basedir} / src/main/webapp/web-inf/lib org.apache.maven.plugins maven-jar-plugin 2.6 cn.demo.javademoin7.application.applicationmain Org.codehaus.mojo findbugs-maven-plugin 3.0.4 low medium true true run-findbugs install check Utf-8 1.7 1.7 3.1.0 2.3.1 1.2 4.12 org.apache.maven.plugins maven-clean-plugin 2.5 junit junit ${junit.version} test javax.servlet javax.servlet-api ${servlet.version} Provided javax.servlet.jsp javax.servlet.jsp-api ${jsp.version} provided javax.servlet jstl ${jstl.version}

(6) when jenkins builds a project, the previous configuration is the same as usual. You can view other cases.

Mainly configure source code management, build trigger, build, post-build operation

Then the deployment can access it.

Http://172.150.12.32:8080/webdemoin7

Write shell scripts to build java web images and containers:

1. Select from the post steps directory

Fill in the following shell script

#! / bin/bashimageid= `sudo docker images | grep-I test | awk'{print $3} '`echo "test image id=" $imageid containid= `sudo docker ps-a | grep-I test | awk' {print $1} '`echo "test container id=" $containidproject=/var/jenkins_home/workspace/test/src/main/resources/docker# to determine whether there is an old test image if test-z "$imageid" thenecho "test image does not exist" else if test-z "$containid" thenecho "test container does not exist" elseecho "test container is about to exist Execute the stop command "sudo docker stop testecho" test container in stop state "fiecho" the old test image will be deleted "sudo docker rmi-f $imageidecho" successfully delete the old test image "sudo mv $project/dockerfile / usr# directory where fi#dockerfile is located, change the directory to usrcd / usr# and copy the tms war package to sudo mv / var/jenkins_home/workspace/test/target/test.war in the directory where dockerfile is located. Echo "test image construction:->" # build tms image sudo docker build-t test. # determine whether there is an old tms container if test-z "$containid" thenecho "test container does not exist" else echo "old test container will be deleted" sudo docker rm-f $containidecho "successfully delete the old test container" fi# create container echo "start creating a new test container" sudo docker run-d-p 8088808080-v / usr/ Logs:/usr/tomcat/logs-name test test

Echo "New test container created successfully"

two。 Click Save now and build now

Thank you for reading, the above is "how to use jenkins in the Docker container to deploy the web project" content, after the study of this article, I believe you have a deeper understanding of how to use jenkins in the Docker container to deploy the web project, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report