Docker practice - docker + svn + maven + tomcat one-click deployment of Java Web projects

Keywords: svn Docker Attribute Tomcat

One of the features of Docker in automated deployment is that this article mainly deploys Java Web projects through docker + svn + maven + tomcat.

Environment to prepare

  • docker
  • maven
  • jdk
  • svn
  • docker Official tomcat image

Installation and configuration of the environment can be referred to.

Install JDK under linux

linux system installation maven

Install Docker and run tomcat in the container

Installing svn under centos can install yum install svn quickly with yum

To achieve a key deployment is to write a script to help us complete the deployment task. Next, we use a script to complete the project's automatic deployment to Docker Because there are many steps, we will decompose the steps to carry out in order to have a clearer understanding.

  1. Draw or update items from SVN
  2. Compile and package using MAVEN
  3. Move executable web applications to published directories
  4. Run the tomcat image with Docker and mount the web application directory in Step 3 to the Docker container

As shown above, let's begin.

Cheout project from SVN

mkdir -p /usr/local/svn-projects
svn checkout --username=massive --password=massive svn://192.168.1.108/test/basic-web

If the project has been checked out, update the project using svn update

Compile and package projects using MAVEN

mvn package -Dmaven.test.skip=true -f /usr/local/svn-projects/basic-web

Move executable web applications to published directories

cp /usr/local/svn-projects/basic-web/target/basic-web /app/deploy/basic-web

Running tomcat images with docker

docker run --privileged=true -v /app/deploy/basic-web:/usr/local/tomcat/webapps/basic-web -p 8081:8080 tomcat:7

Mount / app/deploy/basic-web resources into the container's / usr/local/tomcat/webapps/basic-web Directory

Write the above steps as a script to execute

After the analysis of the steps, what we need to do now is to put the above steps into a script to achieve the purpose of our construction and deployment.

mkdir -p /usr/local/script
touch /usr/local/script/docker-deploy
chmod +x /usr/local/script/docker-deploy
ln -s /usr/local/bin/docker-deploy /usr/local/script/docker-deploy

The script is as follows:

#!/bin/sh
# deploy options

project=basic-web

# Directory to deploy
deploy_dir=/app/deploy/$project


# svn options
project_svn_uri=svn://192.168.1.108/test/basic-web
svn_base_dir=/usr/local/svn-projects/
svn_user=massive
svn_password=massive



# docker options
# Receive the - p parameter, and if this parameter is not transmitted, take the default port 8080.
port=8080
while getopts ":p:" opt; do
  case $opt in
    p)
      port=$OPTARG   #get the value
      ;;
    ?)
      echo "How to use: $0 [-p port]" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done


# Take the name after the last `/` character of the svn address as the checkout directory
svn_full_dir=$svn_base_dir${project_svn_uri##*/}


echo '----------------------------------------------'
echo "checkout project    :    ${project_svn_uri}"
echo "checkout diretory   :    ${svn_full_dir}"
echo "deploy project      :    ${deploy_dir}"
echo "deploy port         :    ${port}"
echo '----------------------------------------------'


# ------------------------------------
# checkout or update from svn
# ------------------------------------
cd $svn_full_dir

echo "try to get svn info..."
svn info
# If the svn info command is executed successfully, it proves that the project has checkout, otherwise it has not checkout
# If not checkout, check out project, otherwise update project to the latest version
if [ $? -eq 0 ];
then
    echo "update svn ..."
    svn update
else
    echo 'checkout from svn...'
    echo "svn checkout --username=${svn-user} --password=${svn_password} ${project_svn_uri}"
    svn checkout --username=$svn_user --password=$svn_password $project_svn_uri
fi


# ------------------------------------
# install maven project
# ------------------------------------
mvn package -Dmaven.test.skip=true -f $svn_full_dir
# ------------------------------------

echo "copying project to ${deploy_dir}..."
mkdir -p $deploy_dir
cp $svn_full_dir/target/$project $deploy_dir -r
echo "copy project end"

echo "docker run --privileged=true -v ${deploy_dir}:/usr/local/tomcat/webapps/${project} -p ${port}:8080 tomcat:7"
docker run --privileged=true -v $deploy_dir:/usr/local/tomcat/webapps/$project -p $port:8080 tomcat:7

Once the script has been written, we can happily use the deployment commands.

docker-deploy

To specify the mapping port of the container, add the - p parameter

docker-deploy -p 8081

So far, we have implemented a command implementation application deployment, and the container does not care about environment variables, so we have to lament Docker's convenience for DevOps. This script performs tasks similar to Jenkins, but it has finer granularity and higher scalability.

If there are other needs, scripts can be extended to achieve more functions.
For example, the version of svn rolls back to 3000 and runs again: docker-deploy-b 3000-p 8081
For example, the version of SVN runs directly without updating: docker-deploy-U-p 8081 (can be set to-U without updating svn, -u with updating svn)

Posted by colforbin05 on Mon, 10 Dec 2018 13:51:05 -0800