Containerize a Java Spring Boot app
Tags: docker, java, springboot, learning
Published: 2024-05-14 17:37:24

I have reached the stage where I need to decide how to deploy my Spring Boot API. I used this opportunity to learn Docker. I started by reading their documentation to grasp the technology and its usage. I reviewed how to build a Dockerfile and all the components needed to build one. I started with an Alpine image. I had seen the name frequently during my previous research. Although I could have started with the openJDK image, the description indicated it was deprecated, so I reverted to using Alpine.

##Step 1: Creating the .jar File
The first thing we need to do is run the Maven clean command in the root directory of your Spring Boot application file.

```
./mvnw clean
```
This will remove the previous target directory, which contains your class files and the application's .jar file. To regenerate this directory with the current updates, you need to run the Maven install command.

To regenerate this directory with the current updates you need to run the maven install command.

```
./mvnw install
```
After running the install command, you should now see the target directory in your file structure.

![target folder in spring application file structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2bcpr4s7smgr9pehqt9.png)

##Step 2: Creating the Dockerfile
Now we can work on our Dockerfile. There are two ways to create a Dockerfile: you can use the Docker init command or create the file manually. I chose to create the file manually as I wanted to learn the manual process before using the Docker init command.

Create a file in the root directory named Dockerfile. Let's review the contents you need to put in the file.
```
FROM alpine:3.19.1
RUN apk update
RUN apk add openjdk21-jre
EXPOSE 8080
ARG JAR_FILE=target/RocketBank-*.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT [ "java", "-jar", "app.jar" ]
```
**FROM**: This keyword is used to specify the base image, which in our case is "alpine:3.19.1".

**RUN**: This keyword is used to run some preliminary commands. In our case, we need to update the package manager and then install Java 21 openJDK or whatever version you used to build your app.

**EXPOSE**: This keyword is used to expose port 8080 on the container.

**ARG**: This keyword is used to store key-value pairs. I used it to store the file path to the application .jar file. I used a wildcard (*) in the .jar filename to be able to reuse this file when I create a new version.

**ADD**: This keyword is used to add files from the main directory into the container directory. I am using the ARG variable that was created to retrieve the .jar file and copying it to the container with the name app.jar.

**ENTRYPOINT**: This is the last command that will be run to start the Spring Boot app in the container.

##Step 3: Building and Running the Docker Image
Now we can run the Docker build command.

```
docker build  -t rocketbankapi:latest .
```
The -t is the tag command to name your image. The "." at the end specifies that the Dockerfile can be found in the current directory.

The output from the command should look like the following, if the build was successful.

![Terminal output from the docker build command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qle8jmm1f581abuqaeq5.png)

We can verify that the image was built by running the Docker image list command.

```
docker image ls
```
![Terminal output from the docker image ls command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dgaw7outujqltz9ly8ld.png)

We are ready to run the container. You can run the container using the Docker run command.

```
docker run -p 8080:8080/tcp rocketbankapi:latest .
```
The "-p 8080:8080/tcp" part port forwards the 8080 port on the container to the 8080 port on the host. The "rocketbankapi:latest" part is the repository name followed by the tag.

Once the Docker run command is ran. The terminal will be outputting the logs for the Spring application. These logs are just output copied from the container since we did not use a "-d" option to run the container in detached mode.

##Conclusion: Ready to Launch Your Dockerized Spring Boot App

Congratulations! You've successfully containerized your Spring Boot API using Docker. By following these steps, you've gained valuable insights into Docker's workflow and how it can streamline the deployment process for your applications.

Now that your Docker image is built and ready, you're just a few commands away from launching your application into the cloud. With Docker, scalability and portability are at your fingertips, making it easier than ever to deploy your Spring Boot projects with confidence.

But this is just the beginning of your Docker journey. As you continue to explore this powerful technology, you'll discover even more ways to optimize your development workflow and streamline your deployment process.

So go ahead, run your container, and see your Spring Boot API come to life in its Dockerized environment. And remember, the possibilities are endless when you combine the power of Spring Boot with the flexibility of Docker. Happy coding!