Jenkins deploys projects to remote docker containers

  1. Install the Publish Over SSH plug-in , if the online installation is slow, you can use offline installation
    insert image description here

  2. To configure SSH , configure the remote server ssh in System Management - System Configuration, as shown in the figure:
    Configure Server Password insert image description here
    Click Add to configure the ssh connection address. If proxy access is required, click Advanced.
    insert image description hereTest server configuration
    insert image description here

  3. Configure the project ssh
    insert image description here
    , select Send build artifacts over SSH to configure the remote server and the files to be uploaded after the build operation,
    insert image description here
    such as the file directory after uploading to the server here is /root/source/hzdsn-hyt-0.0.1-SNAPSHOT.jar and / root/source/classes/Dockerfile

  4. Write a shell script that runs on a remote server

#!/bin/bash
echo "上传远程服务器成功"
#定义项目名和端口,我这里项目名即为容器名和镜像名,可按需定制
PROJECTNAME="hzdsn-hyt"
#该端口为Docker宿主机端口
PORT=8131
pwd
ls -l
#切换到/root/source目录,此目录结构为
#--hzdsn-hyt-0.0.1-SNAPSHOT.jar
#--classes/Dockerfile
#因此需要复制Dockerfile到source目录下
cd /root/source
ls -l
#重命名jar文件(强迫症选项)
cp "$PROJECTNAME"-0.0.1-SNAPSHOT.jar app.jar
echo "重命名文件app.jar"
#复制Dockerfile到source目录下
cp ./classes/Dockerfile ./Dockerfile
echo "复制Dockerfile成功"
ls -l
#构建镜像
echo "开始构建镜像文件"
docker build -t $PROJECTNAME . 
echo "构筑镜像结束"
{
    
     # try
#删除已有容器
  docker rm  -f  $PROJECTNAME
} || {
    
     # catch
  echo "容器不存在"
}
#创建并运行容器
docker run --name "$PROJECTNAME" --restart=always -d -p ${
    
    PORT}:8080 $PROJECTNAME
echo "创建容器成功"
  1. Verify the project configuration , click Build Now, the console output is as follows:
    insert image description here

Guess you like

Origin blog.csdn.net/wyanyi/article/details/117079722