Containerizing applications

A dockerized application is a basic deployable unit that can be integrated later as part of your whole ecosystem of applications. When you're dockerizing your application, you'll have to create your own Dockerfile with all the required instructions to make your application work.

As we mentioned in the last section, we can create a container using an existing base image with the FROM command. You can also copy the Dockerfile content of the base image, but this practice is highly discouraged because it does not make sense to duplicate the code that was already written when the image was created.

It is strongly recommended that you find official images in DockerHub. Since the Dockerfile is available, you should always read it in order to avoid security issues and fully understand how the image works.

Before dockerizing an application, it is important that you make your system use environmental variables instead of configuration files. In this way, you can create images that can be reused by other applications. One of the biggest advantages of using Spring Framework is the ability to use different approaches to configure your applications. This is something that we did in Chapter 8, Microserviceswhen we used a configuration server to centralize all the application configurations. Spring makes it possible for us to use a local configuration file as part of our application, and we can override those configuration values using environment variables later. 

Now let's look at how to dockerize a Spring Boot application.

In the first step, we will create the Dockerfile to run our application. The content of this file is shown in the following code:

FROM java:8 
WORKDIR /
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

Let's briefly review the commands listed in the Dockerfile:

Command Description
FROM java:8 The base java:8 image is used
WORKDIR Default directory within the image filesystem
ARG We are going to use an argument for the JAR file
COPY The provided file will be copied inside the container as app.jar
EXPOSE Port 8080 of the container is exposed
ENTRYPOINT Run the Java application inside the container

 

This Dockerfile should be located in the root of the project. The following screenshot shows the project layout:

Project layout

The application JAR is located under the PROJECT/build/libs directory. This artifact is generated by running the bootRepackage task using the Gradle wrapper, as follows:

./gradlew clean bootRepackage

Once the artifact has been created, it's time to create the Docker image by running the following command:

$ docker build -t spring-boot:1.0 . --build-arg JAR_FILE=build/libs/banking-app-1.0.jar

Once the command has finished, the image should exist locally. You can check this by running the docker images command:

Docker images console output

Note that the java image is also present. This was downloaded during the spring-boot image build process. We can then create a container using the recently created image by running the following command:

$ docker run -p 8081:8080 -d --name banking-app spring-boot:1.0

You can now visit the application deployed in the container in the http://localhost:8081/index URL. The following screenshot shows this application:

 

Application deployed in the container

The build process for the image can and should be automated using your preferred build tool. There are plugins for Gradle and Maven that can be integrated as part of your application. Let's look at how to integrate a Gradle plugin for this task.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.30