How to deploy Java EE Rest services in Docker?

In the previous article, I have discussed how to build REST web application on Java EE, Spring MVC and Spring Boot. Today, we are going to use the same application (based on Java EE) and deploy in Docker container.

What is Docker?

Docker is a platform for creating and running applications inside containers. Containers are simply just a package of applications with all the libraries and dependencies needed to run.

Installing Docker

We will start installing docker by clicking on this link:

We will need to create a Docker account prior installing.

After downloading and install, a docker icon will appear. Just wait for it for finish initializing and proceed to clicking the icon and sign in. After successful sign in, verify you have docker command in terminal by typing

docker version

Create our war file

War file is a web application resource. It is basically the output of our web application after running maven build. We can then deploy this war file to our tomcat server. Locally, you can copy the war file to tomcat folder in your local pc of path /webapps. When you startup tomcat, this war file will be automatically deployed.

Create Dockerfile

Dockerfile basically contains instruction to build our docker image. Let’s create a Dockerfile at root path /jaxrsdemo/Dockerfile. Our docker file has only 2 lines of commands.

FROM tomcat
COPY /target/jaxrsdemo.war /usr/local/tomcat/webapps/jaxrsdemo.war

FROM basically tells docker where to get our docker image. The docker hub contains a lot of docker images, each with specific usage, for example if you need to build an application on nodejs, then you can build it on top of nodejs docker image. In our case, we would like a ready made tomcat image.

COPY duplicates our war file to the path /usr/local/tomcat/webapps/ which exists only in the docker container. When you put war file in this location, the tomcat will be able to deploy the application automatically.

Build Docker Image

Use the following command to build your docker image

docker build . --tag <docker-image-name>
(base) DAVIDs-MBP:jaxrsdemo davidcheah$ docker build . --tag jaxrsdemo
Sending build context to Docker daemon  22.61MB
Step 1/2 : FROM tomcat
 ---> 89481b5d9082
Step 2/2 : COPY /target/jaxrsdemo.war /usr/local/tomcat/webapps/jaxrsdemo.war
 ---> Using cache
 ---> f0a82a4b9342
Successfully built f0a82a4b9342
Successfully tagged jaxrsdemo:latest

Run Docker Image

Use the following command to run your image

docker run -it -p 8081:8080 jaxrsdemo

-it is short for –interactive + –tty which takes you straight into the container

-p is short for port <docker host port> : <container port>

After running the command, you should be able to see some logs related to apache tomcat, similar to what you would see when running on eclipse.

Test your REST Application on Docker

From the docker run command, we have mapped our docker host (our computer) as port 8081. In our Postman, use url with method GET


We should get the same result as running our application on eclipse. Thus, we have successfully containerized our application in Docker!

About the author

Founder of Loves music, sport and most importantly software development.

Leave a Reply

Your email address will not be published. Required fields are marked *