Home Explore Blog CI



docker

1st chunk of `content/guides/java/containerize.md`
ca4ee6ef00044250e6e572a56a36f8ce54417beb3bc376100000000100000fca
---
title: Containerize a Java application
linkTitle: Containerize your app
weight: 10
keywords: java, containerize, initialize, maven, build
description: Learn how to containerize a Java application.
aliases:
  - /language/java/build-images/
  - /language/java/run-containers/
  - /language/java/containerize/
  - /guides/language/java/containerize/
---

## Prerequisites

- You have installed the latest version of [Docker Desktop](/get-started/get-docker.md).
  Docker adds new features regularly and some parts of this guide may
  work only with the latest version of Docker Desktop.

* You have a [Git client](https://git-scm.com/downloads). The examples in this
  section use a command-line based Git client, but you can use any client.

## Overview

This section walks you through containerizing and running a Java
application.

## Get the sample applications

Clone the sample application that you'll be using to your local development machine. Run the following command in a terminal to clone the repository.

```console
$ git clone https://github.com/spring-projects/spring-petclinic.git
```

The sample application is a Spring Boot application built using Maven. For more details, see `readme.md` in the repository.

## Initialize Docker assets

Now that you have an application, you can create the necessary Docker assets to
containerize your application. You can use Docker Desktop's built-in Docker Init
feature to help streamline the process, or you can manually create the assets.

{{< tabs >}}
{{< tab name="Use Docker Init" >}}

Inside the `spring-petclinic` directory, run the `docker init` command. `docker
init` provides some default configuration, but you'll need to answer a few
questions about your application. Refer to the following example to answer the
prompts from `docker init` and use the same answers for your prompts.

The sample application already contains Docker assets. You'll be prompted to overwrite the existing Docker assets. To continue with this guide, select `y` to overwrite them.

```console
$ docker init
Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

WARNING: The following Docker files already exist in this directory:
  - docker-compose.yml
? Do you want to overwrite them? Yes
? What application platform does your project use? Java
? What's the relative directory (with a leading .) for your app? ./src
? What version of Java do you want to use? 21
? What port does your server listen on? 8080
```

In the previous example, notice the `WARNING`. `docker-compose.yaml` already
exists, so `docker init` overwrites that file rather than creating a new
`compose.yaml` file. This prevents having multiple Compose files in the
directory. Both names are supported, but Compose prefers the canonical
`compose.yaml`.

{{< /tab >}}
{{< tab name="Manually create assets" >}}

If you don't have Docker Desktop installed or prefer creating the assets
manually, you can create the following files in your project directory.

Create a file named `Dockerfile` with the following contents.

```dockerfile {collapse=true,title=Dockerfile}
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

################################################################################

# Create a stage for resolving and downloading dependencies.
FROM eclipse-temurin:21-jdk-jammy as deps

WORKDIR /build

# Copy the mvnw wrapper with executable permissions.
COPY --chmod=0755 mvnw mvnw
COPY .mvn/ .mvn/

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.m2 so that subsequent builds don't have to

Title: Containerizing a Java Application
Summary
This section guides you through containerizing and running a Java application. It starts with cloning a sample Spring Boot application built using Maven. Then, it explains how to initialize Docker assets, either using Docker Desktop's Docker Init feature or by manually creating the necessary files like Dockerfile and compose.yaml.