Dockerization of Spring Boot application

 Dockerization of spring Boot

The post explains you how to conatainerize the previously created Resource server
and the Auth Server using Docker.

The post assumes that the following pre-requisites are completed:
  1. Docker is setup on your machine https://docs.docker.com/get-started/
  2. You have an account setup with docker hub.( This enables us to store our docker images in docker hub)
  3. You have created a public repository in docker hub.
  • Docker file creation
The first step is to create a docker file for all the microservice applications:
Employee Microservice - Docker File

FROM openjdk:8-jdk-alpine
MAINTAINER PRASHANT
LABEL description="Employee Microservice"
ADD ./target/employee-0.0.1-SNAPSHOT.jar /app/service.jar
EXPOSE 7003
CMD ["java", "-Xmx1024m", "-jar", "/app/service.jar"]

Similarly create for the other microservices.

  • Create,tag and push docker image
Once the docker file is created as placed in the root folder of the application, we can perform the following steps:

Make sure your docker is running and you are signed to docker repo with the docker application.

--create docker image for employee microservice
docker build -t employee .

--tag the docker image .
( Note: use the name of your public repo).
docker tag employee:latest [docker public repo]/employee:one

--push image to docker hub repo
docker push [docker public repo]/employee:one

Repeat for oauth server:
docker build -t oauth-server .
docker tag oauth-server:latest /oauth-server:one
docker push /oauth-server:one

Repeat for oauth client:
docker build -t oauth-client .
docker tag oauth-client:latest /oauth-client:one
docker push /oauth-client:one

Once the above steps are complete, you can see the image available on the docker hub repo.
The image will also be available in your local docker and can be seen with the help of the following command::

--list all docker images
docker images

  • Executing the docker image
We have configured the employee application to run on port 7003 in tomcat. This is confiigured in the appplication.properties file of the employee microservice.

When running the image via docker, we must publish a container's port or a range of ports to the host machine by using the -p argument with docker run.
format: hostPort:containerPort

--Running the docker image
Employee Microservice:
docker run -p 7003:7003

Oauth Server:
docker run -p 7777:7777

Oauth Client:
docker run -p 7004:7004

where the image id can be obtained by executing the command docker images .

The sample docker files can be found in the respective applications root folders of the respective applications in GitHub

Comments