Docker Hub – Push and Pull Images – Deploy as Container with Example

In our previous article, We learnt how to Dockerize SpringBoot Application with Simple Hello World example.  In this post, let us see how can we push the image to docker hub and how can we pull it and deploy the application as docker container.

So for pushing and pulling the image from Docker Hub, first we need to create an account in Docker hub.

Docker Hub – https://hub.docker.com/, We can login here and create an account.

We are going to use the same example used in our previous article,

 

 

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainfinite</groupId>
	<artifactId>SpringBoot_Docker</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBoot_Docker</name>
	<description>SpringBoot with Docker - Dockerizing</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

AppController.java

package com.javainfinite;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppController {
	
	@RequestMapping(value="/docker1")
	public String display() {
		return "Hello World!!!";
	}

}

App.java

package com.javainfinite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

Dockerfile

FROM openjdk:8
EXPOSE 8086:8080
MAINTAINER javainfinite
WORKDIR /usr/local/bin
COPY ./target/SpringBoot_Docker-0.0.1-SNAPSHOT.jar HelloWorld.jar
ENTRYPOINT ["java", "-jar", "HelloWorld.jar"]

First let us create an image from the above docker file,

docker image build -t “sampleImage” .

In this example let us use image name as helloworld,

docker image build -t “helloworld” .

The above command will generate the image and can be seen with command,

docker images

We have successfully build the image, So in order to push the image to docker hub. First we need to tag it,

docker image tag “imageid” dockerhub username/repository

For this example, My dockerhub account is sriis1987,

docker image tag 09b9013b2982 sriis1987/helloworld

This will tag the image name, you can see it with docker images.

Then we need to push it,

docker image push dockerhubid/repository

For this example,

docker image push sriis1987/helloworld

Now in our docker hub account, we can see the image been successfully pushed.

 

When clicking this repository, we can see our pull command too (public view),

Now we can pull this image and we can deploy the container using imageid.

docker container run imageid.

Suppose if we update the same application, do we have to create new repository?

No, We dont have to.

Suppose if you update the existing file and you can push the image to same repository. It will just update the same image.

Example:

docker image tag “imageId” sriis1987/helloworld:v1

docker image push sriis1987/helloworld:v1

The above command will update the repository with the new image.

NOTE:

Suppose if it shows cannot authenticate or need to authenticate use command,

docker login

This is request for username and password of docker hub.

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *