Creating Docker Image – Spring Boot

Containerization is an important part while deploying the applications into the cloud environment. With Docker, applications that are run on the cloud are converted into images and packaged into a single standard unit called containers, that can be run independently. In this article, we will learn how to create the docker image of the spring boot application and run it.

In the previous article, we learned how to install Docker on the Windows system. If the docker is already not installed in your system, make sure to go through the article.

Technologies used in the article:

  • Spring boot version: 2.2.6.RELEASE
  • Java version 1.8
  • Docker server engine version: 19.03.5

Table of Contents

Create a Spring boot application

Create a simple Spring boot application with spring-boot-starter-web dependency. We will create a simple spring boot application that returns a simple greeting message.

Create a Rest controller class and an HTTP GET method.

This method returns a greeting message upon access.

@SpringBootApplication
public class SpringBootDokcerExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDokcerExampleApplication.class, args);
    }
    
    @RestController
    class HelloDocker{
        @GetMapping("/")
        public String getMessage() {
            return "Hello Docker!!";
        }
    }
}

Add the Docker file

Create a Dockerfile inside the spring boot application folder. Copy the following content into the file.

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
spring boot docker file

The file contains the following Docker commands.

  • FROM: Start the build with the given image. We are using the openjdk version 8 docker image to run our application. For other versions visit docker hub.
  • ARG: To specify the runtime arguments. We have specified the spring boot fat jar location(/target directory).
  • COPY: Copies the spring boot jar from /target directory to the current directory and creates a jar file with name app.jar.
  • ENTRYPOINT: Specifies how the image should be started. Since our application is a packaged jar file, we have used the required commands. The values should be passed in the JSON array format.

Run the Maven build with the below command. This command will build the spring boot application into a jar file.

mvn clean install
Maven build spring boot

Playing with Docker

Open the docker terminal. Navigate inside the spring boot application and run the below command. This command will build the docker image using the Dockerfile.

docker build -t myapp .

Here, -t specifies the tag.

Docker build example

We can list the created Docker images using the below command.

docker images
docker images list

Start the docker container by using the following command.

docker run --rm -p8080:8080 myapp
docker run spring boot app

This command maps the 8080 port of the docker container to the 8080 port for the docker host.

With the above method, we have to stop the running process to type any other command.

We can run the image in the background using the following command.

docker run -d -p8080:8080 myapp
run docker on background

The -d option specifies that the process should run as a background process. The myapp is the docker image name.

We can give a name to the Docker container while running the Docker image.

docker run -d -p8080:8080 --name my-app myapp
docker command with image name

–name can be used to specify the custom name for the docker container.

We can check the container running by using the below command.

docker container ls
Docker container running

Now the application is up and should be available at http://localhost:8080. If in case it is not available, access the application with the docker machine’s IP.

To check the Docker machine IP, we can use the following command.

docker-machine ip
docker command to get vm machine ip

Our application is up and running!! 🙂

Spring boot dockerization example

We can check the application logs using the below command.

docker logs -f my-app
docker logs example

Running Docker image on the SSL

For this example, let us create a self-signed SSL certificate using the java key tool.

keytool -genkeypair -alias asbnotebook -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore asbnotebook.p12 -validity 3650
self signed ssl generation with keytool

We have used the PKCS12 format to generate the self-signed SSL certificate.

Copy the generated certificate file into the classpath(resources folder) of the application.

Spring boot SSL example

Add the following configuration properties to the spring boot application’s application.properties(under /src/main/resources/ directory).

server.port=8443
server.ssl.key-store=classpath:asbnotebook.p12
server.ssl.key-store-password=password
server.ssl.keyStoreType=PKCS12

Generate the Spring boot jar file by running the maven build.

mvn clean install

Create the docker image using the below command.

docker build -t myappssl .
spring boot docker with ssl

Run the Docker container using the below command.

docker run -d -t -p 443:8443 --name my-app-ssl myappssl
Docker spring boot with ssl

Now we can access our spring boot application with HTTPS protocol.

Docker spring boot HTTPs example

To stop and remove the docker container, use the below command.

docker rm -f my-app
stop and remove docker container

Conclusion

In this article, we learned about Dockerizing the spring boot application.

We have learned basic docker commands and how to use them.

We also learned how to create a docker image for SSL enabled Spring boot application.

Example code is available on GitHub.

Creating Docker Image – Spring Boot
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading