Maven项目集成Docker,推送私服并运行

在日常开发过程中,会将项目进行打包,并运行到服务器上。在本文中介绍使用docker的方式,将本地jar包推送到docker私服,并运行在远程服务器上。

步骤

  1. 新建一个maven项目,目录结构如下:
    在这里插入图片描述
  2. 将maven编译打包插件、docker打包插件引入到pom文件中
    2.1. 多环境配置
    在这里插入图片描述
    pom文件中profile下的profile.name要与application-{env} 中的**{env}**保持一致
    2.2. Maven编译打包插件
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>${
    
    maven-compiler-plugin.version}</version>
    <configuration>
        <compilerArguments>
            <bootclasspath>${
    
    env.JAVA_HOME}\jre\lib\rt.jar;${
    
    env.JAVA_HOME}\jre\lib\jce.jar</bootclasspath>
        </compilerArguments>
        <source>${
    
    java.version}</source>
        <target>${
    
    java.version}</target>
        <encoding>${
    
    maven.compiler.encoding}</encoding>
        <verbose/>
    </configuration>
</plugin>
<plugin>
    <!--打包跳过测试-->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${
    
    maven-surefire-plugin.version}</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>${
    
    spring-boot.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>${
    
    maven-resources-plugin.version}</version>
    <configuration>
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>woff</nonFilteredFileExtension>
            <nonFilteredFileExtension>eot</nonFilteredFileExtension>
            <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
            <nonFilteredFileExtension>svg</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

2.3. docker打包插件配置

<!-- docker打包插件 -->
<plugin>
     <groupId>io.fabric8</groupId>
     <artifactId>docker-maven-plugin</artifactId>
     <version>0.33.0</version>
     <configuration>
         <verbose>true</verbose>
         <!-- docker远程管理url -->
         <dockerHost>tcp://ip:2375</dockerHost>

         <registry>registry.cn-hangzhou.aliyuncs.com</registry>
			
         <authConfig>
         	<!-- docker私服账户密码 -->
             <push>
                 <username>zfl_aliases@163.com</username>
                 <password>123456</password>
             </push>
         </authConfig>

         <buildArgs>
             <JAR_FILE>${
    
    project.build.finalName}.jar</JAR_FILE>
         </buildArgs>
         <images>
             <image>
                 <!-- 镜像名称  命名空间/仓库名称:镜像版本号 -->
                 <name>${
    
    docker.image.prefix}/${
    
    project.artifactId}:${
    
    project.version}</name>
                 <build>
                     <dockerFile>${
    
    project.basedir}/src/main/docker/Dockerfile</dockerFile>
                     <assembly>
                         <name>/</name>
                         <!-- artifact是预定义的值,表示将项目打包后的jar拷贝到编译上下文中,便于Dockerfile ADD指令 -->
                         <descriptorRef>artifact</descriptorRef>
                     </assembly>
                 </build>
                 <run>
                     <!-- 运行时容器名称 -->
                     <containerNamePattern>${
    
    project.artifactId}</containerNamePattern>
                     <extraHosts>
                         <extraHost>服务器ip</extraHost>
                     </extraHosts>
                 </run>
             </image>
         </images>
     </configuration>
 </plugin>
  1. 编写Dockerfile
// FROM 可根据实际情况来编写 openjdk:8
FROM registry.cn-hangzhou.aliyuncs.com/lee/jdk8-lee:1.0.0
MAINTAINER zhangfalu zfl_aliases@163.com 2021-01-25
ARG JAR_FILE
COPY ${
    
    JAR_FILE} app.jar
EXPOSE 8063
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Duser.timezone=GMT+08", "-jar", "/app.jar"]
  1. 打包
    命令:mvn clean package -Dmaven.skip.tests=true -P online
    查看classes下打包好的文件信息
    在这里插入图片描述

  2. 本地测试
    在这里插入图片描述

  3. 将本地打好的jar包,打成镜像推动到远程私服,并运行docker容器
    命令:mvn docker:stop docker:remove docker:build docker:push docker:start
    在这里插入图片描述
    在这里插入图片描述

  4. 查看私服
    在这里插入图片描述

  5. 查看容器运行状态
    在这里插入图片描述

使用idea集成Docker,来本地管理镜像的生成,删除、容器的启动与停止,避免使用命令。还可以更好的查看容器运行日志信息。

  1. 点击 File-Settings
    在这里插入图片描述
  2. 填完之后点击apply,在开发工具下方会多出一个Docker窗口
    在这里插入图片描述
  3. 点击打开进行连接
    在这里插入图片描述
    打开Containers,查看制定容器,可方便的查看容器日志
    在这里插入图片描述

注:项目地址

猜你喜欢

转载自blog.csdn.net/qq_37640410/article/details/113124667