cvwiki

Docker

Nov 5, 2022

# Writing a Dockerfile for Spring

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
FROM maven:3.6.3-openjdk-11-slim as builder

WORKDIR /app
COPY pom.xml .
# Use this optimization to cache the local dependencies. Works as long as the POM doesn't change
RUN mvn dependency:go-offline

COPY src/ /app/src/
RUN mvn package

# Use AdoptOpenJDK for base image.
FROM adoptopenjdk/openjdk11:jre-11.0.8_10-alpine

# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/*.jar /app.jar

# Run the web service on container startup.
CMD ["java", "-jar", "/app.jar"]

# Useful Commands

You can run an interactive shell container using that image and explore whatever content that image has. For instance:

1
docker run -it image_name sh

Or the following for images with an entrypoint:

1
docker run -it --entrypoint sh image_name

Or, if you want to see how the image was build, meaning the steps in its Dockerfile, you can:

1
docker image history --no-trunc image_name > image_history

The steps will be logged into the image_history file.

1
docker load -i application-image.tar 
1
docker inspect application