Kubernetes + Spring Boot Tutorial - Create Docker Image

For minikube to create spring boot docker container to be managed by pods, we need to provide minikube with docker image of the spring boot application to be deployed. In this tutorial, we will be going over docker basics and creating the spring boot docker image which we will then provide to minikube for deployment.

Video
This tutorial is explained in the below Youtube Video.Spring Boot + Kubernetes Tutorial
What is Kubernetes? Need for it? Install Ubuntu on Windows using WSL Installing Kubectl, Minikube and Docker on Ubuntu Create Docker Image Deploy to Minikube Pods Difference between ClusterIP, NodePort and LoadBalancer Service Service Hello World Example
Implementation
In previous tutorial, we had implemented examples for deploying spring boot applications to docker. In this tutorial series we had covered the basics of docker. We will be taking reference of the spring boot microservices to docker tutorial that we had implemented previously. We will be downloading the spring boot example we had implemented for this tutorial. The docker file used to create the docker image is as follows -From openjdk:8 copy ./target/employee-producer-0.0.1-SNAPSHOT.jar employee-producer-0.0.1-SNAPSHOT.jar CMD ["java","-jar","employee-producer-0.0.1-SNAPSHOT.jar"]Import the spring boot project in eclipse. Build it using maven install, so that we have the jar file inside the target folder.


Next we will build an image with the name employee-producer-kubernetes.
docker image build -t employee-producer-kubernetes .

If now list the available docker images, we can see that the new image has been created.

Next we will run the above image as a container. Also we will be publishing the docker port 8080 to centos port 8080.
docker container run --name producer -p 8080:8080 -d employee-producer-kubernetes

If now list the available docker containers, we can see that the new container has been created.

docker container logs producer

Now using curl we could test the application running in docker as follows-
curl localhost:8080/employee

Pass Docker Image to Kubernetes
The docker image can be passed to minikube in 2 ways--
Local docker image -
Minikube makes use of the locally created docker image. So if we run following commanddocker image ls
We can see the image is present locally. This will be used by minikube to run containers using pods. -
Image from docker hub -
In this case the image will be downloaded from docker hub which is central repository for docker images.
In previous docker tutorials we have already implemented examples for pushing images to docker hub. Using the terminal login to the docker hub account. It will ask for the dockerhub username and password.docker login
-
Next we will push the image named employee-producer-kubernetes to docker hub using the following command.
docker image push employee-producer-kubernetes
But we get the following exception
-
This is because only official docker images can be in the above format with only the name.
All other docker images should be in the format -
owner/docker-image-name So for this we will need to add the owner tag to the existing image that we want to push to docker hub.docker image tag employee-producer-kubernetes javainuse/employee-producer-kubernetes
If we now list the docker images we see two docker images with same id.docker image ls
-
Now we can push the image to docker hub as below-
docker image push javainuse/employee-producer-kubernetes

Download Source Code
Download it - Spring Boot Jar Application To Docker