6 Containerizing Spring Boot

 

This chapter covers

  • Working with container images on Docker
  • Packaging Spring Boot applications as container images
  • Managing Spring Boot containers with Docker Compose
  • Automating image build and push with GitHub Actions

So far, we have developed a Catalog Service application that exposes a REST API and persists data through a PostgreSQL database running inside a container. We’re getting closer to deploying the first components of the Polar Bookshop system to a Kubernetes cluster. Before doing that, however, you need to learn how to package Spring Boot applications as container images and manage their life cycles.

This chapter will teach you the essential characteristics of container images and how to build one. We’ll use Docker to work with containers, but you can do the same with any other container runtime compatible with the Open Container Initiative (OCI) standards (https://opencontainers.org). In the remainder of the book, whenever I refer to a container image or Docker image, I mean an image compatible with the OCI Image Specification.

Along the way, I’ll share with you several considerations regarding building container images for production, such as security and performance. We’ll explore two possibilities: Dockerfiles and Cloud Native Buildpacks.

When we start working with more than one container, the Docker CLI is not very efficient. Instead, we’ll use Docker Compose to manage several containers and their life cycles.

6.1 Working with container images on Docker

6.1.1 Understanding container images

6.1.2 Creating images with Dockerfiles

6.1.3 Publishing images on GitHub Container Registry

6.2 Packaging Spring Boot applications as container images

6.2.1 Preparing Spring Boot for containerization

6.2.2 Containerizing Spring Boot with Dockerfiles

6.2.3 Building container images for production

6.2.4 Containerizing Spring Boot with Cloud Native Buildpacks

6.3 Managing Spring Boot containers with Docker Compose

6.3.1 Using Docker Compose to manage the container life cycle

6.3.2 Debugging Spring Boot containers