Monday 14 January 2019

How to run SpringBoot API as a Docker container

How to run SpringBoot API as a Docker container

We have created SpringBoot API in our previous post and you can download.

Docker is a container management service that eases building and deployment.The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

The official site for Docker is https://www.docker.com/ The site has all information and documentation about the Docker software. It also has the download links for various operating systems.

Prerequisite:

We will need these files to run SpringBoot API as a Docker container.
  • pom.xml
  • Dockerfile
How to run SpringBoot API as a Docker container
pom.xml: We are going to use a Maven plugin from Spotify to make a docker container from the springboot app. We can see the plugin in the pom.xml:

<!-- dockerfile plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
Dockerfile: Next we need a Dockerfile which tells how the image should be build up.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This just says to use openjdk version 8 and to add the springboot jar.

Step 1: Go to where project is placed using CMD and run 


mvn package

How to run SpringBoot API as a Docker container

Step 2: Now run

mvn dockerfile:build
to make a docker image which we can run. As you can see we have successfully build the niteshkumar/employeesapi-docker:1.0.0 image
How to run SpringBoot API as a Docker container

Step 3: If we want to be able to push the image to DockerHub where we can store it in a repository, we first have to login.
docker login
How to run SpringBoot API as a Docker container

Step 4: Now, we should be able to push the image.
mvn dockerfile:push
How to run SpringBoot API as a Docker container

After success you can go to DockerHub and you can see your image in Repositories.

Now, we can pull this image from DockerHub and run it. To achieve this we will execute below commands.

docker images
docker rmi -f XXXXXXXXXXX
docker pull niteshkumar/employeesapi-docker:1.0.1
docker run -p 8080:8080 -t niteshkumar/employeesapi-docker:1.0.1

you should be able to connect API with Postman or RestClient.


Next Post: How to Deploy your docker container on Amazon EC2 Elastic Container Service



Related Post: 

How to build simple microservice using Spring Boot

Implementation of swagger in SpringBoot API

How to Deploy your docker container on Amazon EC2 Elastic Container Service

1 comment:

  1. Swagger API Testing solves business needs while assuring great developer experience by its definition-focused approach.

    ReplyDelete