Search Tutorials


Deploying Spring Based WAR Application to Docker | JavaInUse

Deploying Spring Based WAR Application to Docker

Docker Tutorial - Table Of Contents

Docker Deploying Spring Based WAR Application to Docker Deploying Spring Based JAR Application to Docker Deploying Multiple Spring Boot Microservices using Docker Networking Deploying Multiple Spring Boot Microservices using Docker Compose Deploying Spring Boot + MYSQL Application to Docker Publishing Docker Image to DockerHub Deploy Docker Swarm services using Play With Docker Deploy Docker Swarm services using Docker Stack Deploy Docker Swarm services to multiple AWS EC2 instances Docker Cheat Sheet

Video

This tutorial is explained in the below Youtube Video.

In a previous tutorial we had created a simple Spring Boot Web Application named employee-producer, that exposed a REST web service. This was developed to be deployed using WAR packaging.
We will be creating an image for deploying this war application to tomcat.
Deploying Spring Based WAR Application to Docker
Create the dockerfile. Docker file is a list of commands that we want the docker engine to execute. We will be needing a tomcat image using which we will be deploying our application. Go to dockerhub and search tomcat, we will be using official tomcat images. Here since we are using jdk 8 so we select alpine 8.

Docker Get Tomcat Image
Go to the spring boot project folder and create a docker file as follows-
From tomcat:8.0.51-jre8-alpine
CMD ["catalina.sh","run"]

Create Dockerfile
Open the terminal and install the docker
yum install docker




Install Docker
Another terminal, start the docker
systemctl start docker

docker-start
Now open the terminal and go to the Spring Boot project folder.
Next we will build an image with the name producer.
docker image build -t employee-producer .

docker-producer-image
Next we will run the above image as a container

docker container run -d employee-producer

Docker Run Image as Container
Here we have started the container. Now using the following command check the logs
docker container logs lb

Docker get producer logs

Docker get producer logs2
The application has started successfully. If we go to localhost:8080 we see that tomcat is not running.
Docker start tomcat
This is because tomcat would have started inside the docker on port 8080. But we cannot access this port directly since its running inside the docker.

docker-producer-tomcat
Now stop the existing running container-
docker-container-stop
Again run the image as a container but this time using the publish command where we link the docker internal port 8080 to external port 8080, so that it can be accessed externally.

docker container run -p 8080:8080 -d employee-producer

docker-container-starting
Now go to localhost:8080
docker-container-running1

docker-container-running2
So we have linked the docker internal port 8080 to our external port 8080. Now go to localhost:8080 and we can see that tomcat has started successfully.
Next we will see how to go inside the docker and investigate it using the docker exec command.
docker container exec -it 03 /bin/sh

docker-container-exec
We will need to copy our WAR file in the webapps folder of the tomcat inside the docker. This we will do it adding commands to the docker file.

From tomcat:8.0.51-jre8-alpine
RUN rm -rf /usr/local/tomcat/webapps/*
COPY ./target/employee-producer-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT.war
CMD ["catalina.sh","run"]

docker-boot-dockerfile
Now stop the docker container we had previously started.

docker container stop 03

docker-boot-conatiner-stop
Again build the image, and start the container.
docker-boot-conatiner-image-build

docker-boot-conatiner-start
Go to localhost:8080/employee, we will see that our application is deployed successfully.
docker-boot-container-tomcat