Dockerize Java RESTful Application

Please follow the below steps to dockerize your java RESTful application –

Prerequisites : Please insure that Docker, java and mvn is installed on your machine.

  1. Create Dockerfile : Go to the root of the application where pom.xml is contained. Below is the content of my Dockerfile –

cd yourProjectFolder -> vi Dockerfile

#Fetch the base Jav8 image

FROM java:8

#Expose the local application port

EXPOSE 8088

#Place the jar file to the docker location

ADD /target/lookupService-1.0-SNAPSHOT.jar lookupService-1.0-SNAPSHOT.jar

#Place the config file as a part of application

ADD /src/main/java/com/test/config/config.properties config.properties

#execute the application

ENTRYPOINT [“java”,”-jar”,”yourService-1.0-SNAPSHOT.jar”]

  1.  Build Docker Image :

docker build -f Dockerfile -t yourservice .

  1. Run the Docker Image :

docker run -p 8088:8088 -tyourservice

Option -p publishes or maps host system port 18080 to container port 8080.

Note* Please don’t forget to change your base uri that is localhost:8088 to 0.0.0.0:8080 network interface created inside the container listen to all available address.

Few important commands of docker to know :

  1. To find the Id of your running container –

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

77558fabec6c       yourservice       “java -jar lookupS…”   38 minutes ago      Up 38 minutes       0.0.0.0:8088->8088/tcp   affectionate_brahmagupta

  1. To kill the process inside the docker using container is –

docker container kill 77558fabec6c(Container Id)

Now you can test your RESt call on port 0.0.0.0:8080 using any of the REST client . Hope it helps 🙂