【问题标题】:How can I push the Docker image of a Spring Boot application to DockerHub with Maven?如何使用 Maven 将 Spring Boot 应用程序的 Docker 映像推送到 DockerHub?
【发布时间】:2021-02-08 01:08:13
【问题描述】:

我希望能够将我的 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(来自 Idea)将映像部署到 DockerHub 时,我收到以下错误:

#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

我想&lt;url&gt;https://index.docker.io/v1/&lt;/url&gt; 是错误的。

DockerHub 的正确 URL 模式是什么?

【问题讨论】:

    标签: java docker maven dockerhub


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      相关资源
      最近更新 更多