springboot使用docker-maven-plugin上传docker镜像

网上关于docker-maven-plugin的使用资料比较零散而老旧,这里将他们整理起来并解决一些配置中遇到的问题。

系统环境:win10 idea2020 Docker19 ubuntu20(WSL)

(1)pom插件配置

<plugins>
...
<plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>1.2.2</version>
        <configuration>
          <!-- docker镜像名 -->
          <imageName>name</imageName>
          <!-- 根据springboot官方推荐,使用openjdk8 -->
          <baseImage>openjdk:8-jdk-alpine</baseImage>
          <!--自行配置docker服务器地址 默认为localhost:2375 -->
          <dockerHost>http://<host>:2375</dockerHost>
          <!-- springboot运行命令 -->
          <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>
...
</plugins>

更多内容请查看官方链接:https://github.com/spotify/docker-maven-plugin

(2)配置docker开放端口

想要上传至docker,需要配置安全策略开放docker的对应端口。

  1)在docker desktop版本中通过下列不走配置即可:

Follow following steps for window 10
Step 1: Right click on "Dcoker Desktop is running icon "
Step 2: click on Settings
Step 3: In “General Tab” you must enable checkbox “Expose Demon on tcp://localhost:2375 without TLS”

   2)介于我是linux的远程服务端的docker,根据网上查阅的资料和新版本docker的配置文件语法变化,可以通过以下步骤配置:

vim /etc/sysconfig/docker

#本人使用的是WSL,目录在 /etc/default/docker

#添加以下内容 向外暴露2375端口
DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock"

:wq
 
service docker restart

 服务重新启动后,通过netstat -ano | grep 2375 看到端口被监听说明设置成功

(3)idea更新maven文件后,执行命令:

mvn clean package docker:build -DskipTests

清理、打包和构建上传镜像文件,并跳过测试。

成功后可以在docker镜像目录中找到上传的工程。

至此整合完成。

 

其余参考资料:

https://www.lzhpo.com/article/27

https://blog.csdn.net/wender/article/details/79693629

https://forums.docker.com/t/spotify-docker-maven-plugin-cant-connect-to-localhost-2375/9093/25

 

posted on 2020-09-02 14:13  SoftwareTestingHUST  阅读(268)  评论(0编辑  收藏  举报