Containerize Spring Boot Application in 30 mins

This step-by-step guide walks you through containerizing a Spring Boot application using Stakpak’s AI DevOps agent.

By the end of the guide, Stakpak's AI agent will:

  • Perform discovery to learn about the application structure, entry point, Java version, and listening port

  • Create and iterate on a Dockerfile for the Spring Boot app

  • Create a Docker Compose file to test the Dockerized application

  • Build and run the Spring Boot app in a Docker container

The application we'll dockerize

This is an open-source stateful Spring Boot app that depends on Postgres or MySQL https://github.com/spring-projects/spring-petclinic

Create a Stakpak project (called "Flow")

  1. Go to https://stakpak.dev/ , sign in, and create a new flow

  2. Click on Agent (in the AI box)

AI box
  1. Select "Dockerize my app"

  1. Stakpak Agent will ask you some questions about the application, the technology stack you use, and any dependencies. Because you won't always have all this information available for legacy apps, you can just type in "I'm not sure" and Stakpak Agent will do its own discovery to figure these details out

  1. To run commands, Stakpak needs shell access in your environment, This is done by pasting the following command in your terminal (if you haven't already, install the Stakpak CLI here)

stakpak agent run --checkpoint-id <checkpointid>
  1. Paste this command in your terminal,

  1. Now, Stakpak Agent will clone the application code locally to start containerizing the app

  1. Since the Agent now knows it's a Java application, it will look for the required Java version

  1. Identify the main class to find the app's entry point

  1. Identify the listening port of your app (since we didn't answer this question when the agent asked about it)

  1. Create a Dockerfile draft, notice how Stakpak Agent uses a multi-stage build to reduce the final image size and attack surface

  1. In practice, creating a Dockerfile is not enough to dockerize an app, you have to build it and test it to make sure the application works. That's why Stapak Agent creates a docker-compose.yml file with all the application dependencies to test the Dockerfile it created

  1. The 1st Dockerfile generated failed to build, so Stakpak Agent tweaks it and tries again

  1. After the build succeeds, Stakpak Agent tests the web server to make sure it's healthy

  1. Finally, we test the webapp ourselves by navigating to http://localhost:8000 according to Stakpak's containerization summary

## Additional Notes
1. The application is currently configured to use MySQL by default.
2. You can access the application at http://localhost:8080
3. To stop the application, run `docker compose down` in the spring-petclinic directory.
4. To rebuild the application after code changes, run `docker compose build` followed by `docker compose up -d`.
5. For production use, consider securing the database passwords and using environment variables or Docker secrets.

The containerization process is complete and the application is running successfully!
[Mission Accomplished]

Congratulations! You've dockerized a Spring Boot app in less than 30 minutes!

Last updated