更多配置选项查看官方信息:
https://github.com/spotify/dockerfile-maven

Dockerfile

位置和pom.xml同级的文件,名称是Dockerfile

  1. FROM java:8
  2. VOLUME /tmp
  3. ADD target/${JAR_FILE} app.jar
  4. EXPOSE 8080
  5. ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

插件配置

插件地址 :https://github.com/spotify/dockerfile-maven
增加属性

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <docker.registry.url>registry.cn-beijing.aliyuncs.com</docker.registry.url>
  4. <docker.registry.namespace>baxiang</docker.registry.namespace>
  5. </properties>

增加插件配置

  1. <plugin>
  2. <groupId>com.spotify</groupId>
  3. <artifactId>dockerfile-maven-plugin</artifactId>
  4. <version>1.4.13</version>
  5. <executions>
  6. <execution>
  7. <id>default</id>
  8. <goals>
  9. <goal>build</goal>
  10. <goal>push</goal>
  11. </goals>
  12. </execution>
  13. </executions>
  14. <configuration>
  15. <tag>${project.version}</tag>
  16. <buildArgs>
  17. <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
  18. </buildArgs>
  19. <repository>${docker.registry.url}/${docker.registry.namespace}/${project.artifactId}</repository>
  20. <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
  21. </configuration>
  22. </plugin>

配置maven

参考 https://github.com/spotify/dockerfile-maven/blob/master/docs/authentication.md
隐藏 POM 文件中的密码
maven 提供密码加密的功能,具体过程见:https://maven.apache.org/guides/mini/guide-encryption.html
配置完 ~/.m2/setting-security.xml 后,在 ~/.m2/setting.xml 找到 servers 标签,添加:

  1. <servers>
  2. <server>
  3. <id>registry.cn-shanghai.aliyuncs.com</id>
  4. <username>userName</username>
  5. <password>{encryptedPassword}</password>
  6. </server>
  7. </servers>

修改 POM 文件中的配置即可

  1. <configuration>
  2. <repository>registry.cn-shanghai.aliyuncs.com/xxxxx/xxxxx</repository>
  3. - <username>userName</username>
  4. - <password>userPassword</password>
  5. + <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
  6. <buildArgs>
  7. <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
  8. </buildArgs>
  9. </configuration>

或者使用

  1. <useMavenSettingsForAuth>true</useMavenSettingsForAuth>

修改maven中的conf/settings.xml文件,找到节点,在里面添加阿里云的server节点

  1. <server>
  2. <!--maven的pom中可以根据这个id找到这个server节点的配置,配置成dockerhub 的URL地址-->
  3. <id>registry.cn-beijing.aliyuncs.com</id>
  4. <!--这里是在阿里云注册的账号-->
  5. <username>xxxx@xxx.com</username>
  6. <!--这里是在阿里云注册的密码-->
  7. <password>xxxx</password>
  8. </server>

打包命令

打tag命令

  1. mvn package dockerfile:build

推送远程

  1. mvn dockerfile:push

问题排查 增加-X 参数

  1. mvn clean package docker:build -X

IDEA插件

搜索插件Docker
image.png
设置中搜索Docker

image.png

修改docker p配置

  1. sudo vim /lib/systemd/system/docker.service

修改
image.png
增加

  1. ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375

重启docker

  1. sudo systemctl restart docker

查看2375端口是否开发

  1. netstat -nlp|grep 2375
  2. tcp6 0 0 :::2375 :::* LISTEN 14626/dockerd

设置连接
image.png

增加仓库
image.png

参考

https://www.cnblogs.com/javastack/p/14034812.html
https://spring.io/guides/topicals/spring-boot-docker/
https://spring.io/guides/gs/spring-boot-docker/
https://blog.csdn.net/u012702547/article/details/100153799