spring-attic / top-spring-boot-docker

Spring Boot Docker:: Topical guide to using Docker and how to create container images for Spring Boot applications :: spring-boot
https://spring.io/guides/topicals/spring-boot-docker
180 stars 127 forks source link

fix not working unpack command to gradle example #29

Closed ckyeon closed 1 year ago

ckyeon commented 1 year ago

Problem

While following the guide, I found that the gradle Dockerfile example of Multi-Stage Build did not working.

# syntax=docker/dockerfile:experimental
FROM eclipse-temurin:17-jdk-alpine AS build
WORKDIR /workspace/app

COPY . /workspace/app
RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build
RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/build/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

The unpack command(jar -xf ../libs/*.jar) in the example above doens't work.

The other examples worked fine, so I looked for differences.

work

jar -xf ../*.jar

not work

jar -xf ../libs/*.jar

For some reason, the * pattern doesn't seem to work when the path contains directories.
(It was the same on macOS terminal.)

Solve

find ../libs -name "*.jar" -exec jar -xf {} \;

reference: https://stackoverflow.com/questions/45975019/extract-several-of-jar-in-one-command

Because it is a translator, sentences can be awkward. Thanks for reading πŸ˜„

ckyeon commented 1 year ago

Problem

While following the guide, I found that the gradle Dockerfile example of Multi-Stage Build did not working.

# syntax=docker/dockerfile:experimental
FROM eclipse-temurin:17-jdk-alpine AS build
WORKDIR /workspace/app

COPY . /workspace/app
RUN --mount=type=cache,target=/root/.gradle ./gradlew clean build
RUN mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar)

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=/workspace/app/build/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

The unpack command(jar -xf ../libs/*.jar) in the example above doens't work.

The other examples worked fine, so I looked for differences.

work

jar -xf ../*.jar

not work

jar -xf ../libs/*.jar

For some reason, the * pattern doesn't seem to work when the path contains directories. (It was the same on macOS terminal.)

Solve

find ../libs -name "*.jar" -exec jar -xf {} \;

reference: https://stackoverflow.com/questions/45975019/extract-several-of-jar-in-one-command

Because it is a translator, sentences can be awkward. Thanks for reading πŸ˜„

The cause of the problem seems to be that there are two *.jar files in gradle.

Test with find command

run build/dependency

gradle

find ../libs -name "*.jar"

<result>
../libs/docker-0.0.1-SNAPSHOT-plain.jar
../libs/docker-0.0.1-SNAPSHOT.jar

maven

run target/dependency

find .. -name "*.jar"

<result>
../docker-0.0.1-SNAPSHOT.jar

Solve

jar -xf ../libs/*-SNAPSHOT.jar
ckyeon commented 1 year ago

@dsyer If you are not busy, please review this PR πŸ˜‰

dsyer commented 1 year ago

LGTM. I think maybe this changed in gradle since the guide was written.