micronaut-projects / micronaut-gradle-plugin

A Gradle Plugin for Micronaut
Apache License 2.0
66 stars 44 forks source link

dockerBuild fails for Apple silliconMac #794

Closed sdelamo closed 1 year ago

sdelamo commented 1 year ago

Generate an app with the defaults.

./gradlew dockerBuild

> Task :dockerfile
Dockerfile written to: /Users/sdelamo/Downloads/dockertest/build/docker/main/Dockerfile

> Task :dockerBuild
Building image using context '/Users/sdelamo/Downloads/dockertest/build/docker/main'.
Using Dockerfile '/Users/sdelamo/Downloads/dockertest/build/docker/main/Dockerfile'
Using images 'demo'.
Step 1/8 : FROM openjdk:17-alpine

> Task :dockerBuild FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':dockerBuild'.
> Could not build image: no matching manifest for linux/arm64/v8 in the manifest list entries

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.2.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 5s
6 actionable tasks: 6 executed

Environment Information

Operating system: MacOS Ventura Architecture: Apple Silicon Mac Micronaut Gradle Plugin 4.0.2

Example Application

No response

Version

4.0.2

alvarosanchez commented 1 year ago

The default base image we use is not multi-platform.

We should change the default to eclipse-temurin:17-jre

sdelamo commented 1 year ago

The workaround would be to run dockerfile

copy the Dockerfile from build/docker/main/Dockerfile to the root of the project and replace:

FROM openjdk:17-alpine
WORKDIR /home/app
COPY layers/libs /home/app/libs
COPY layers/classes /home/app/classes
COPY layers/resources /home/app/resources
COPY layers/application.jar /home/app/application.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/home/app/application.jar"]

with:

FROM eclipse-temurin:17-jre
WORKDIR /home/app
COPY layers/libs /home/app/libs
COPY layers/classes /home/app/classes
COPY layers/resources /home/app/resources
COPY layers/application.jar /home/app/application.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/home/app/application.jar"]
alvarosanchez commented 1 year ago

That's a brute-force workaround 😅 . You can simply do

tasks.named("dockerfile") {
    baseImage = 'eclipse-temurin:17-jre'
}
sdelamo commented 1 year ago

it does not work:

Could not determine the dependencies of task ':dockerBuild'.
> Could not create task ':dockerfile'.
   > Could not set unknown property 'baseImage' for task ':dockerfile' of type com.bmuschko.gradle.docker.tasks.image.Dockerfile.
sdelamo commented 1 year ago

it seems the config is:


tasks.named("dockerfile") {
    from 'eclipse-temurin:17-jre'
}

but the generated Dockerfile is incorrect.

it generates:

FROM openjdk:17-alpine
WORKDIR /home/app
COPY layers/libs /home/app/libs
COPY layers/classes /home/app/classes
COPY layers/resources /home/app/resources
COPY layers/application.jar /home/app/application.jar
EXPOSE 8080
FROM eclipse-temurin:17-jre
ENTRYPOINT ["java", "-jar", "/home/app/application.jar"]
alvarosanchez commented 1 year ago
image

Pretty sure this worked in Micronaut 3. Perhaps the documentation is left behind @melix ?

melix commented 1 year ago

mmm, we do have baseImage : https://github.com/micronaut-projects/micronaut-gradle-plugin/blob/1c6fba3323eabf7f543bd0fbfec89527d3a42b5a/docker-plugin/src/main/java/io/micronaut/gradle/docker/MicronautDockerfile.java#L162

but it seems that here the task type is the Docker plugin docker file, not ours :thinking:, so I think it doesn't work because you still have a Dockerfile at the root.

sdelamo commented 1 year ago

Yeah, this works:

tasks.named("dockerfile") {
    baseImage = "eclipse-temurin:17-jre"
}

and setting the Dockerfile in the project root works as well.

I think we should change the base image to improve the user experience.

melix commented 1 year ago

There are many problems reported with Alpine in any case, so changing the base image makes sense to me. I don't have the context why this was chosen in the first place.

alvarosanchez commented 1 year ago

Alpine became famous because it had small containers compared to that of the full distros.

But perhaps nowadays the situation is different. In fact, 17-jre is smaller than 17-alpine.

sdelamo commented 1 year ago

+1 for having a default base image with multi-platform support.

joaquindiez commented 1 year ago

I add this to my build.gradle.kts

tasks.named<io.micronaut.gradle.docker.MicronautDockerfile>("dockerfile") { baseImage.set( "eclipse-temurin:17-jre") }

and it is working in version 4.1.0

if this base image it the recomendend for multi-platform support I vote for having it as default base image

joaquindiez commented 1 year ago

But the above is not working when executing optimizedDockerBuild

`Step 1/8 : FROM openjdk:17-alpine

Task :app:optimizedDockerBuild FAILED

FAILURE: Build failed with an exception.