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")
Go to https://stakpak.dev/ , sign in, and create a new flow
Click on Agent (in the AI box)

Select "Dockerize my app"

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

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>

Paste this command in your terminal,

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

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

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

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

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

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

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



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

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