gpt4 book ai didi

java - 如何使用 Maven 将 Spring Boot 应用程序的 Docker 镜像推送到 DockerHub?

转载 作者:行者123 更新时间:2023-12-02 18:33:48 25 4
gpt4 key购买 nike

我希望能够将我的 Spring Boot 应用程序部署到 DockerHub。我关注了this tutorial , 创建 Dockerfile在根项目目录中并修改我的 pom.xml文件。

Dockerfile看起来像这样:

FROM openjdk:8-jre-alpine3.9

COPY target/myproduct-1.0-SNAPSHOT.jar /myproduct.jar

CMD ["java","-jar","/myproduct.jar"]
EXPOSE 8080/tcp

myproduct.jar是我的应用程序的 JAR 文件。

pom.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>myproduct</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<docker.registry>myusername</docker.registry>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>docker-clean</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<successCodes>1,0</successCodes>
<arguments>
<argument>rmi</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>docker-build</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>build</argument>
<argument>-t</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
<argument>.</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>docker-login</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>login</argument>
<argument>-u</argument>
<argument>${docker.user}</argument>
<argument>-p</argument>
<argument>${docker.password}</argument>
<argument>${docker.url}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>docker-push</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>push</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

当我运行 mvn clean install ,镜像在本地Docker上更新,我可以成功启动它。

但是,当我尝试通过运行 mvn clean deploy -Ddocker.user=myusername -Ddocker.password=XXXXXXXX -Ddocker.url=https://hub.docker.com/repository/registry-XXX.docker.io/myusername/myproduct/ -f pom.xml 将镜像部署到 DockerHub 时(从 Idea 中),我收到以下错误:

#7 exporting to image
#7 exporting layers
#7 exporting layers 0.4s done
#7 writing image sha256:6ea4c81c5c2df2d39a1a1e6c95c1d298e63745b460194fa421dc8a04bf2ba1b1 done
#7 naming to XXXXXXXX/myproduct:1.0-SNAPSHOT done
#7 DONE 0.4s
[INFO]
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ myproduct ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.885 s
[INFO] Finished at: 2020-10-25T16:49:55+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project myproduct: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter -> [Help 1]

所以我将其添加到 pom.xml文件:

<distributionManagement>
<repository>
<id>DockerHub</id>
<name>DockerHub</name>
<uniqueVersion>false</uniqueVersion>
<layout>legacy</layout>
<url>https://index.docker.io/v1/</url>
</repository>
</distributionManagement>

this answer 中的建议.

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project myproduct: Failed to deploy artifacts: Could not transfer artifact mycompany:myproduct:jar:1.0-20201025.160739-1 from/to DockerHub (https://index.docker.io/v1/): Transfer failed for https://index.docker.io/v1/XXXXXX 405 Not Allowed

我想 <url>https://index.docker.io/v1/</url>是错误的。

DockerHub 的正确 URL 模式是什么?

最佳答案

请尝试以下网址:https://index.docker.io/v2/

关于java - 如何使用 Maven 将 Spring Boot 应用程序的 Docker 镜像推送到 DockerHub?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64525899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com