Show List

Pushing Docker Image to Docker Hub

Docker hub is a service provided by docker to host the docker images and share with others. In this example, we are going to create a docker image from docker file and push it to docker hub.

Create a docker hub account if you do not have that already by going to the official website https://hub.docker.com/.  

Create a repository by clicking on the "Create Repository" button on top right. 
Once the repository is created, command to push the image to the docker hub is displayed on the repository page.
We are using the below Dockerfile to create the image to push to the above repo. This for a Java Spring Boot application that we created in the Earlier Chapter .

Dockerfile :
FROM adoptopenjdk/openjdk15:ubi
COPY target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
The docker file is present in the root of the folder. Jar file for the project is present in the target folder.

Creating Docker Image

Start the Docker desktop application. Navigate to the root folder from command prompt and run below command. Note that the name matches the format provided on docker hub page:

 "docker build -t itcodelab/firstrepo:docker-rest-image .

This would create the image and tag with name "docker-rest-image"
C:\Users\mail2\Downloads\Spring-Boot-Rest-Service>docker build -t itcodelab/firstrepo:docker-rest-image .

 => [internal] load build definition from Dockerfile                                                                                                            0.1s
 => => transferring dockerfile: 534B                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                               0.0s
 => => transferring context: 76B                                                                                                                                0.0s
 => [internal] load metadata for docker.io/adoptopenjdk/openjdk15:ubi                                                                                           0.8s
 => [auth] adoptopenjdk/openjdk15:pull token for registry-1.docker.io                                                                                           0.0s
 => [internal] load build context                                                                                                                               0.0s
 => => transferring context: 80B                                                                                                                                0.0s
 => [1/3] FROM docker.io/adoptopenjdk/openjdk15:ubi@sha256:c860508ce2f180bb58dcb225b0ec967b3730e2f21fbc9351aeaeb71c498aa5f8                                     0.0s
 => CACHED [2/3] COPY target/*.jar app.jar                                                                                                                      0.0s
 => [3/3] RUN useradd springuser                                                                                                                                0.9s
 => exporting to image                                                                                                                                          0.1s
 => => exporting layers                                                                                                                                         0.1s
 => => writing image sha256:b3972f58abb04761e79baa5366c8462e0627dc1fe85152dfa89bc5657b8121f8                                                                    0.0s
 => => naming to docker.io/itcodelab/firstrepo:docker-rest-image                                                                                                0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

The image can now be pushed to docker hub using command "docker push itcodelab/firstrepo:docker-rest-image"
C:\Users\mail2\Downloads\Spring-Boot-Rest-Service>docker push itcodelab/firstrepo:docker-rest-image
The push refers to repository [docker.io/itcodelab/firstrepo]
2242c1bf58a6: Pushed
a3bc21525fe2: Pushed
070cec23ffc6: Pushed
6f50cb0a77ab: Pushed
525ed45dbdb1: Pushed
5bc03dec6239: Pushed
docker-rest-image: digest: sha256:162cf4bb85a916fd09913a623ad252fdd0214129086811a61cf8e5fe580bb81c size: 1582
The image can also be pushed from the Docker Desktop

    Leave a Comment


  • captcha text