Create Docker image for a Spring Boot Application and deploy the container to Kubernetes (Docker Desktop)

Lets see how to create and run a docker image for a spring boot application.


Install Docker Desktop if you don’t already have.

Spring boot application:

Then create a simple spring boot application via Spring Initializr with only the “Spring Web” dependency.

Update the Spring Boot Application class file to add a rest endpoint.

Then build the project using mvn clean install.

public class BootdockerApplication {

private String sayHello(){
return "Hello from Spring Boot application running from Docker";

public static void main(String[] args) {, args);


Docker File:

Then create a file named “Dockerfile” in the root directory of the spring boot app with the following contents. The below file uses Jdk 11 for docker. Copies the jar file created out of the mvn clean install step we did above to “app.jar”. Then declare the entry point as app.jar.

FROM adoptopenjdk/openjdk11:latest
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Build Docker Image:

Then run the below command to build the docker image. This command will use the Dockerfile we created above and create a docker image with the tag (-t) jsession4d/my-spring-boot-docker

docker build -t jsession4d/my-spring-boot-docker .

Run Docker image:

To Run the Docker image run the below command. -p 8081:8080 binds the container (spring boot app) port 8080 to the local host port 8081.

docker run -p 8081:8080 jsession4d/my-spring-boot-docker


Now if we hit the url: http://localhost:8081/ in a browser we will see the hello message from our spring boot app running in a docker container.

We can also view the running container in the Docker Desktop.


Lets see how to deploy the container image that we created in the previous step (“jsession4d/my-spring-boot-docker”) into Kubernetes in Docker Desktop.

Enable Kubernetes in Docker Desktop:

In Docker Desktop for windows click the (gear icon) Settings icon. Then navigate to Kubernetes tab and enable “Enable Kubernetes” Check box. Then click Apply & Restart. If Kubernetes started successfully the Kubernetes button will be green in the bottom left corner.

Then in a command prompt type the below command to deploy the container to a pod. Here

mypod is the pod name, –image for image name , –image-pull-policy=Never to pull the image from local machine.

kubectl run mypod --image=jsession4d/my-spring-boot-docker --image-pull-policy=Never

Then run “kubectl get pods“. If the status is “Running” we are good.

Note: As we didn’t push the docker image to Docker Hub, we will get “ErrImagePull” status if we dont specify “–image-pull-policy=Never ” flag.

C:\Users\saji2>kubectl get pods
mypod   0/1     ErrImagePull   0          5s

After the the pod is running, to access the application in the container from local, run the below command.

kubectl port-forward mypod 8082:8080

After that you can navigate to a browser and type http://localhost:8082/ to see the output of the application that runs inside mypod.

After that you can delete the pod using the command below.

C:\Users\saji2>kubectl delete pod mypod
pod "mypod" deleted

Leave a Reply

%d bloggers like this: