Play with docker image and store on Harbor and Docker-Hub

Disclaimer: This blog content has been taken from my latest book:

“Cloud Native Microservices with Spring and Kubernetes”

In this blog, I will cover how to create a simple docker image using a SpringBoot Java application, store and pull docker images using Docker-Hun and Harbor image repositories and finally how to run this app on local Docker desktop client.

Docker image registry is a persistent storage to store docker images from where it can be pulled by the CI/CD pipeline or K8s deployments and deploy on container.

Docker build services like docker, Buildpack CNB kPack,, VMware Tanzu Build Service (TBS) and other build libraries build docker images and store to docker registries and it can be updated automatically after any commit in source code repository like GitHub.

Prerequisite:

  1. Install Docker Desktop
  2. Install Harbor image registry
  3. Create Docker-Hub account
  4. Install Java
  5. Install Maven
  6. Install Git
  7. Install Homebrew

Note: This demo app has been setup and run on Mac system.

1. Install Docker Desktop:

Install Docker Desktop. The docker CLI requires the Docker daemon, so you’ll need to have that installed and running locally.

Create a Docker Hub account on https://hub.docker.com/ and get set go!

You can create docker images locally, then you have choice to push images to Docker Hub cloud SAAS or set a local Harbor private repository for security reason.

Note: Your docker desktop should be always running when you work with Docker containers like building, packaging running and persist in image registry. You can use same Docker Hub login credentials for Docker Desktop.

There are two types of Repositories:

a. Docker Hub Public repositories

This is very convenient public cloud where anyone can create their docker hub account and store docker images free.

Note: DockerHub also provides private private repository.

b. Private repositories

There are many private repositories like Harbor, jFrog, Google Container Registry, Amazon Elastic Container Registry (ECR) which are available on on-prem and on public cloud as a paid service.

I will cover Harbor private registry which is open source, can be deployed locally on-prem and it has enterprise support from VMware.

2. Install Harbor Image Registry:

There are two ways to install –

  1. Install open-source Harbor
  2. Install Harbor on Vmware TKGI (PKS)

If your Docker Desktop is already running and you have logged on your machine, then no need to provide Docker login credentials:

1. Image Registry Login:

# Docker-Hub Login:

docker login
# Harbor Login:

docker login <harbor_address>   
docker login -u admin -p <password> <Harbor Host> 

#Note: Create a Harbor project where you can store all your docker images.e g.: /library

Tip: Docker-Hub provides secret token which is advisable to use when connecting from registry or login.

2. Build Docker Image:

Create a SpringBoot micro-service project. Or you can simple clone and use this readymade Github public repo for local demo purpose:

git clone https://github.com/rajivmca2004/catalogue-service.git && cd catalogue-service

Build Docker Images using Maven

If you are using Maven and SpringBoot APP to actually build the Docker image. Go to source project folder and run this Maven command. You need to install Maven before running this command on Mac , Linux and Windows:

mvn clean install dockerfile:build

Maven command to push image to current image registry (You need to be logged in on DockerHub or Harbor on your local system:

mvn install dockerfile:push

List all Docker images:

docker image ls

Show a breakdown of the various layers in the image:

docker image history catalogue-service:latest

Note (Optional): You can also try to build image like this for non Java projects.Go to project folder of source code’s home path (in this case its Java based SpringBoot) project and run this command:

docker image build -t <Docker_Harbor_userId>/<image_name:tag> .

docker image build -t itsrajivsrivastava/catalogue-service .

3. Push Image to Docker /Harbor Registry:

a. Tag your image before pushing:

docker tag <dockerId>/image:tag

#Docker-Hub:
docker tag itsrajivsrivastava/catalogue-service itsrajivsrivastava/catalogue-service:latest

#Harbor:
docker tag itsrajivsrivastava/catalogue-service harbor.tanzu.cloudification.in/library/catalogue-service:latest

b. Now you should be able to push it:

#Docker-Hub Push (When you are logged-in to Docker-Hub thru local Docker Desktop client

#Docker-Hub:
docker push itsrajivsrivastava/catalogue-service:latest

#Harbor:
docker push harbor.tanzu.cloudification.in/library/catalogue-service:latest

4. Pull Image to docker from Docker-Hub:

docker pull <image_name>

#Docker-Hub:
docker pull itsrajivsrivastava/catalogue-service:latest

#Harbor
docker pull harbor.tanzu.cloudification.in/library/catalogue-service:latest

5. Run Docker Image

Running a Container Docker Image:

docker run -p 8010:8010 itsrajivsrivastava/catalogue-service:latest

Now, test application by –

http://localhost:8010/catalogue