Deploying Spring Based WAR Application to Docker
Docker Tutorial - Table Of ContentsDocker 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 Docker Cheat Sheet
VideoThis tutorial is explained in the below Youtube Video.
We will be creating an image for deploying this war application to tomcat.
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.
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"]
Open the terminal and install the docker
yum install docker
Another terminal, start the docker
systemctl start docker
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 .
Next we will run the above image as a container
docker container run -d employee-producer
Here we have started the container. Now using the following command check the logs
docker container logs lb
The application has started successfully. If we go to localhost:8080 we see that tomcat is not running.
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.
Now stop the existing running container-
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
Now go to localhost:8080
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
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"]
Now stop the docker container we had previously started.
docker container stop 03
Again build the image, and start the container.
Go to localhost:8080/employee, we will see that our application is deployed successfully.