Push a Docker image to DockerHub using docker maven plugin fabric8.io
Introduction
If you want to push a docker image to Docker Hub repository, it can be achieved using docker maven plugin from fabric8.io. This plugin lets you build images, start and stop containers and push it to Docker repositories
In case you are wondering the difference between a container and image, please note that in docker terminology a running image is called container.
In this tutorial, we are going to
- Build a docker image
- Push image to Docker Hub repository
&nsbp;
Want to build a docker image first using this plugin ?
Read Here : How to Build a Docker Image and start stop container
Read Here : How to Build a Docker Image and start stop container
- For the below example, I have used Spring Boot Rest service project which is connected to MongoDB. See below link for reference.
- Using io.fabric8 plugin, under the plugin section add the io.fabric8 plugin123456<plugin><groupId>io.fabric8</groupId><artifactId>docker-maven-plugin</artifactId><version>0.20.0</version>....
- We need to first build the image from Dockerfile, this plugin supports all the command which are in Dockerfile and you can directly create a docker image by giving arguments in pom.xml but In my view making docker images from Docker file is much easier and simpler. So we will ask maven plugin in below step to look for Dockerfile in root( project base directory)1234567<name>springboot-mongo-dockerimage:${project.version}</name><alias>springboot-mongo-dockerimage</alias><build><dockerFileDir>${project.basedir}</dockerFileDir></build>
- Next we specify the docker registry address123<registry>registry.hub.docker.com/YOUR_DOCKER_HUB_USERNAME</registry>
- Now we need to add authentication credentials in <authconfig> tag123456<authConfig><username>ENTER YOUR DOCKER HUB USERNAME LIKE abhishek</username><password>ENTER YOUR DOCKER HUB PASSWORD</password></authConfig>
- We will add execution phases, where we will push the image123456789<execution><id>push</id><phase>post-integration-test</phase><goals><goal>push</goal></goals></execution>
- Now run the pom.xml using mvn clean install and check your docker hub repo after some time to see if image has been pushed. If you are pushing the image for first time its gonna take some time depending on the size of image.
Here is the complete build section from the plugin, hope this help.
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.20.0</version> <configuration> <registry>registry.hub.docker.com/YOUR_DOCKER_HUB_USERNAME</registry> <images> <image> <name>springboot-mongo-dockerhub:${project.version}</name> <alias>springboot-mongo-dockerhub</alias> <build> <dockerFileDir>${project.basedir}</dockerFileDir> </build> <run> <namingStrategy>alias</namingStrategy> <dependsOn> <container>mongo</container> </dependsOn> <links> <link>mongo</link> </links> <ports> <port>9876:8080</port> </ports> <log> <prefix>TC</prefix> <date>default</date> <color>cyan</color> </log> </run> </image> </images> <authConfig> <username>ENTER YOUR DOCKER HUB USERNAME LIKE abhishek</username> <password>ENTER YOUR DOCKER HUB PASSWORD</password> </authConfig> </configuration> <executions> <execution> <id>start</id> <phase>pre-integration-test</phase> <goals> <goal>stop</goal> <goal>build</goal> <goal>start</goal> </goals> </execution> <execution> <id>push</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> <goal>push</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
If you get an error : io.fabric8:docker-maven-plugin:0.20.0:build failed: A tar file cannot include itself
How to fix- A tar file cannot include itself
Can you please upload your entire project? It will be helpful for me…
Request you to include entire pom.xml so that it will be usefull to find the tags which are included in correct order.