第一步:docker+jenkins+gitlab自动化部署配置,这里不说了,请看我的另一篇文章

第二步:pom.xml和Dockerfile配置内容

pom.xml

  <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!--docker镜像相关的配置信息-->
                <configuration>
                    <!--镜像名,这里用工程名-->
                    <imageName>${project.artifactId}-${project.version}</imageName>
                    <!--Dockerfile文件所在目录-->
                    <dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
                    <!--TAG,这里用工程版本号-->
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <dockerHost>http://你的ip:2375</dockerHost>
                    <registryUrl>http://你的ip:5000</registryUrl>
                    <pushImage>false</pushImage>
                    <imageName>你的ip:5000/${project.artifactId}:${project.version}</imageName>
                    <!--构建镜像的配置信息-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.artifactId}-${project.version}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

/src/main/resourcesDockerfile的内容

FROM java:8
ENV project bdkt-eureka
ENV version 1.0-RELEASE
ENV home_path /home
WORKDIR $home_path
ADD /$project-$version.jar $home_path/$project.jar
ENTRYPOINT ["java", "-jar", "bdkt-eureka.jar"]

FROM java:8   依赖于jdk
ENV project bdkt-eureka   环境变量 key value
ENV version 1.0-RELEASE
ENV home_path /home
WORKDIR $home_path  指定的工作目录
ADD /$project-$version.jar $home_path/$project.jar  复制功能
ENTRYPOINT ["java", "-jar", "bdkt-eureka.jar"]  相当于 java -jar bdkt-eureka.jar,这个命令就是把几个逗号里面的内容用空格拼接出来执行

第三步:构建任务中需要的配置

Pre Steps  1

docker+jenkins+gitlab+dockerfile部署配置-LMLPHP

bdkt-parent/bdkt-basics/bdkt-eureka/pom.xml
clean install

Pre Steps 2

docker+jenkins+gitlab+dockerfile部署配置-LMLPHP

内容为:

#!/bin/bash
#镜像名称
image_name=47.111.115.253:5000/bdkt-eureka
#启动的镜像运行的名字
run_image_name=bdkt-eureka
#版本
version=1.0-RELEASE
result=$(docker ps | grep "$run_image_name")
if [[ "$result" != "" ]]
then
echo "stop $run_image_name"
docker stop $run_image_name
fi
result1=$(docker ps -a | grep "$run_image_name")
if [[ "$result1" != "" ]]
then
echo "rm $run_image_name"
docker rm $run_image_name
fi
result2=$(docker images | grep "$image_name")

if [[ "$result2" != "" ]]
then
echo "rmi $image_name:$version"
docker rmi $image_name:$version
fi

Pre Steps 3

docker+jenkins+gitlab+dockerfile部署配置-LMLPHP

内容为:

clean package -f bdkt-parent/bdkt-basics/bdkt-eureka/pom.xml -DskipTests docker:build

Pre Steps 3

docker+jenkins+gitlab+dockerfile部署配置-LMLPHP

内容为:

其中ssh site是自己配置的,详细看我之前文章

docker run --name bdkt-eureka -p 8100:8100  -idt 47.111.115.253:5000/bdkt-eureka:1.0-RELEASE
docker logs bdkt-eureka
06-30 06:46