一、在 Maven 的 XML 配置文件中添加 DockerHub 或其他镜像仓库的 Server 地址
找到 Maven 的 setting.xml
文件,添加一个 DockerHub 或其他镜像仓库的 Server 地址,以下示例以 DockerHub 为例,注意替换 username
,password
,email
为你自己注册的 DockerHub Server 信息或待使用的镜像仓库信息,id
可以随意命名,注意可辨识性并保持和后面的引用一致即可
1 2 3 4 5 6 7 8 9 10 11
| <servers> <server> <id>docker-hub</id> <username>docker-hub 用户名</username> <password>docker-hub 密码</password> <configuration> <email>docker-hub 邮箱</email> </configuration> </server> </servers>
|
二、在项目 pom.xml 文件中添加 Maven 插件
以下示例以 DockerHub 为例,注意 serverId
要和上面配置的 id
一致,且将 imageName
替换为你自己的项目镜像名,其他参数,如有必要,也可以自行替换,请不要直接复制,直接复制无法使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>${docker.maven.plugin.version}</version> <executions> <execution> <id>build-image</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions>
<configuration> <serverId>docker-hub</serverId> <registryUrl>https://index.docker.io/v1/</registryUrl> <imageName>test/${project.artifactId}:${project.version}</imageName> <baseImage>java:8</baseImage> <entryPoint>["java", "-jar", "-Dspring.profiles.active=prod","/${project.build.finalName}.jar"]</entryPoint> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin>
|
三、打包应用
直接点击 IDE 的 package 按钮打包应用即可,如果是命令行,则执行如下命令打包
1
| $ mvn clean package docker:build -DpushImage
|