How can I push the Docker image of a Spring Boot application to DockerHub with Maven?

I want to be able to deploy my Spring Boot application to DockerHub. I followed this tutorial, created Dockerfile in the root project directory and adapted my pom.xml file.

Dockerfile looks like this:

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 is the JAR file of my application.

pom.xml looks like this:

<?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>

When I run mvn clean install, the image is updated on the local Docker and I can successfully start it.

However, when I try to deploy the image to DockerHub by running 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 (from within Idea), I get the following error:

#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]

So I added this to the pom.xml file:

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

as suggested in 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

I suppose that <url>https://index.docker.io/v1/</url> is wrong.

What is the correct URL pattern for DockerHub?

Source: Docker Questions