Search Tutorials


Kubernetes + Spring Boot Tutorial - Create Docker Image | JavaInUse



Kubernetes + Spring Boot Tutorial - Create Docker Image

In previous tutorial, we set up minikube - a single node kubernetes server. Next we will be deploying spring boot application using minikube. So the spring boot application will run inside docker container which will be managed by minikube pods.
Minikube cluster
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.
Minikube + Spring boot Kubernetes cluster

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.
Spring boot Kubernetes example
Open the ubuntu terminal that we had previously installed using WSL. Next we will navigate to the location where we have the spring boot project.
Ubuntu Spring boot Kubernetes example
Next we will build an image with the name employee-producer-kubernetes.
docker image build -t employee-producer-kubernetes .

Spring boot Kubernetes example create docker image
If now list the available docker images, we can see that the new image has been created.
Spring boot Kubernetes list images
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

docker-container-running2
If now list the available docker containers, we can see that the new container has been created.
Spring boot Kubernetes list containers
Here we have started the container with name as producer. Now using the following command check the logs
docker container logs producer

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

Spring boot Kubernetes curl example

Pass Docker Image to Kubernetes

The docker image can be passed to minikube in 2 ways-
  • Local docker image -
    Kubernetes local image
    Minikube makes use of the locally created docker image. So if we run following command
    docker image ls
    

    Spring boot Kubernetes list images
    We can see the image is present locally. This will be used by minikube to run containers using pods.
  • Image from docker hub -
    Kubernetes dockerhub image
    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
    

    Docker Terminal 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
    Docker Image Upload 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
    

    Docker Tag Image
    If we now list the docker images we see two docker images with same id.
    docker image ls 
    

    Docker Image list
  • Now we can push the image to docker hub as below-
    docker image push javainuse/employee-producer-kubernetes
    
If we now go to the dockerhub repository for javainuse, we can see that the image has been uploaded.
Kubernetes dockerhub image

Download Source Code

Download it - Spring Boot Jar Application To Docker