This section provides the steps needed to run the Spring Boot micro service as a Docker container inside Kubernetes.
- Copy the MyFirstApp-0.0.1-SNAPSHOT.jar built in the previous section to another folder say C:\MyFirstApp.
- Let us now create a Docker container using this standalone jar. We need to introduce the Docker file. Create a file with name Dockerfile in C:\MyFirstApp and add the following lines.
FROM java:8ADD MyFirstApp-0.0.1-SNAPSHOT.jar app.jarENTRYPOINT ["java","-jar","/app.jar"]
- Dockerfile and the MyFirstApp-0.0.1-SNAPSHOT.jar should be under the same folder.
|Create Dockerfile |
- Understanding the elements of a basic Dockerfile.
- FROM specifies the base image on which container is built. In this case we use java:8 which uses Java container as both the build and runtime environment.
- ADD element copies the standalone Jar to app.jar.
- ENTRYPOINT specifies a command that will be executed when the container starts. In this case “java –jar app.jar”.
Note from Kubernetes:
When using a single VM of Kubernetes, it’s really handy to reuse the minikube’s built-in Docker daemon; as this means you don’t have to build a docker registry on your host machine and push the image into it – you can just build inside the same docker daemon as minikube which speeds up local experiments. Just make sure you tag your Docker image with something other than ‘latest’ and use that tag while you pull the image. Otherwise, if you do not specify version of your image, it will be assumed as :latest, with pull image policy of Always correspondingly, which may eventually result in ErrImagePull as you may not have any versions of your Docker image out there in the default docker registry (usually DockerHub) yet.
|Build docker image |
- Verify if the container is listed by running the “docker images” command. We will see an entry for the myfirstapp container.
|docker images listing |
- Now it is time to start our container in kubernetes. We will leverage kubectl to run this container.
kubectl run myfirstapp –image=myfirstapp:v1 –port=8080
|kubectl run |
- Launch the minikube dashboard and verify if the application is created. Run the command “minikube dashboard” this should launch a browser and display the dashboard. You can see the app running in dashboard.
|minikube dashboard launch |
|Kubernetes dashboard |
- Run the “kubectl get services” command again to see if myfirstapp is listed.
|kuebctl get services listing exposed service |
- Verify if the application works. Using the browser. Type the URL http://192.168.99.103:31608/sayhello to invoke the API. (or) Run the command “minkube service myfirstapp”. This should automatically launch the browser. Append the URL sayhello to get the message.
|minikube service myfirstapp |
|Launch myfirstapp API from kubernetes IP |
Now we have successfully deployed and run a Spring Boot application as a container on Kubernetes.