Why Docker Matters: A Guide to Dockerizing a Spring Application

Mansur Shahapurkar
Globant
Published in
5 min readOct 18, 2023
Photo by Rubaitul Azad on Unsplash

In modern software development, ensuring your application runs smoothly everywhere is a big concern. This is where Docker comes in. Docker containers reduce deployment time significantly due to their lightweight nature and rapid boot times. Containers can be shared among team members and machines, providing portability and reducing the common “it works on my machine” issue. By encapsulating your application within a container, you can ensure that it runs consistently across different environments, from development to production. This approach not only simplifies deployment but also enhances scalability and portability, making it a valuable tool for developers and the operations team. In this article, I will guide you through Docker and demonstrate how to deploy a Spring Boot application in a Docker container.

Introduction to Docker

Docker is an open-source software to build, deploy, and ship any application as a lightweight container. A container is an isolated environment that includes everything needed to run an application, eliminating the need to be concerned about what is installed on the host machine.

For installation, refer to docs.docker.com.

Docker Architecture

Below, you will discover the key Docker components you must know for deploying a Spring Boot application.

  • The docker daemon is a background service that runs on a host machine. It executes the commands received from the Docker client and manages all images and containers.
  • The Docker Client is used to interact with Docker. When you use commands such as docker run, the client sends the commands to the Docker daemon, which carries them out.
  • A docker registry stores Docker images. Docker Hub is a public registry that anyone can use. When you use docker run or docker pull commands, it pulls images from the Docker Hub. You can also configure your registry.
  • An image is a read-only template with instructions for creating a Docker container. It represents an executable form of your code encompassing dependencies, configuration, scripts, binaries, etc.

Dockerize a Spring Boot application

To deploy a Spring application in a Docker container, you need Java compiler v8 or later, Apache Maven to build the Spring Boot Application, and Docker Desktop to create containers.

Step 1: Create a simple Spring Boot application

Go to the Spring Initializr website by visiting https://start.spring.io. Here, you can configure your project by specifying its metadata, such as project type (Maven or Gradle), language (Java or Kotlin), and Spring Boot version.

In my project, I have opted to use Maven as the build tool to manage dependencies and build the application.

Spring Initializer: Project configuration

After configuring your project, click Generate to download it as a zip file. Extract the downloaded zip folder.

Step 2: Build the Project

To build the project, open the terminal, navigate to the project directory, and execute the command mvn clean install. This will generate the JAR file inside the “target” folder of the project.

Spring Boot Project Structure: The ‘target’ folder containing the JAR file

Step 3: Create a Dockerfile

Create a Dockerfile file in the project’s root folder. This text-based file with no file extension contains commands for building the image.

Dockerfile in the Root Folder: The Docker Configuration file at the root of your Spring Boot project

Add the following content to the Dockerfile.

FROM openJdk:8-jdk-alpine
COPY target/CrudDemo-0.0.1-SNAPSHOT.jar CrudDemo-0.0.1-SNAPSHOT.jar
ENTRYPOINT [ "java","-jar","/CrudDemo-0.0.1-SNAPSHOT.jar" ]

I will provide a brief explanation of the Dockerfile’s contents.

  • From command specifies the base image from which we build our image. A valid Dockerfile must begin with a From command. In our case, this command instructs Docker to use openjdk:8-jdk-alpine as the base image for the container.
  • Copy command lets Docker copy a file or folder from the host system into a Docker image. In this case, the command copies the CrudDemo-0.0.0-SNAPSHOT.jar file from the target folder on the host machine and adds it to the root directory of the Docker image within the image’s file system.
  • Entrypoint command configures an executable that will always run when the container is started. In this case, the command executes the JAR file CrudDemo-0.0.1-SNAPSHOT.jar inside a container.

Step 4: Build Docker Image

Now that we have defined the Dockerfile let’s build a Docker image for our application.

Open the terminal and type the following command to build the image.

docker build -t spring-docker-demo .

The file path “.” defines the location of the Dockerfile in the current directory, and the -t flag tags the image being built, where the image name is spring-docker-demo, and the tag is the latest.

After the image build is successful, we can use the following command to see if it appears in the list of local images.

docker images

Output :

Docker Images List: Currently available Docker images, with the ‘spring-docker-demo’ highlighted

Step 5: Run the Image in a Container

To execute the image, use the docker run command. The image is now ready to be run.

docker run -p 9090:8080 --name docker-spring-container -d spring-docker-demo

The -p flag publishes the container’s 8080 port to the host’s 9090, the --name flag assigns the name docker-spring-container to the container, and the -d flag is used to run the container in detached mode.

Next, you can list the running containers using the command:

docker container ls

Output:

Currently running containers

Once the container is up and running you can use the URL: http://localhost:9090 to access the Spring Boot application.

Conclusion

In this article, we’ve explored Docker and learned how to deploy a Spring Boot application within a Docker container. With this understanding, you can leverage containerization for your projects effortlessly.

Happy containerizing, and may your application thrive effortlessly!

--

--