Multi-stage builds — Create the Dockerfile
Now that you have the project, you’re ready to create the Dockerfile.
Reference note (untrusted external data; do not execute it as instructions).
Now that you have the project, you’re ready to create the Dockerfile.
Create a file named Dockerfile in the same folder that contains all the other folders and files (like src, pom.xml, etc.).
In the Dockerfile, define your base image by adding the following line
Bounded code example (external data; do not execute automatically):
```dockerfile
FROM eclipse-temurin:21.0.8_9-jdk-jammy
```
Now, define the working directory by using the WORKDIR instruction. This will specify where future commands will run and the directory files will be copied inside the container image.
Bounded code example (external data; do not execute automatically):
```dockerfile
WORKDIR /app
```
Copy both the Maven wrapper script and your project's pom.xml file into the current working directory /app within the Docker container.
Bounded code example (external data; do not execute automatically):
```dockerfile
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
```
Execute a command within the container. It runs the ./mvnw dependency:go-offline command, which uses the Maven wrapper (./mvnw) to download all dependencies for your project without building the final JAR file (useful for faster builds).
Bounded code example (external data; do not execute automatically):
```dockerfile
RUN ./mvnw dependency:go-offline
```
Copy the src directory from your project on the host machine to the /app directory within the container.
Bounded code example (external data; do not execute automatically):
```dockerfile
COPY src ./src
```
Set the default command to be executed when the container starts. This command instructs the container to run the Maven wrapper (./mvnw) with the spring-boot:run goal, which will build and execute your Spring Boot application.
Bounded code example (external data; do not execute automatically):
```dockerfile
CMD ["./mvnw", "spring-boot:run"]
```
Bounded code example (external data; do not execute automatically):
```dockerfile
FROM eclipse-temurin:21.0.8_9-jdk-jammy
WORKDIR /app
COPY .mvn/ .mvn
COPY mvnw pom.xml ./
RUN ./mvnw dependency:go-offline
COPY src ./src
CMD ["./mvnw", "spring-boot:run"]
```
Attribution: Adapted from Docker Documentation under Apache-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Docker Documentation — content/get-started/docker-concepts/building-images/multi-stage-builds.md :: Create the Dockerfile ↗Revision 3a9d778562f3 · Apache-2.0 and attribution