micronaut-projects / micronaut-gradle-plugin

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

Allow building static native images #82

Open alvarosanchez opened 3 years ago

alvarosanchez commented 3 years ago

For reference, this is a Dockerfile that works: https://github.com/micronaut-projects/micronaut-maven-plugin/blob/master/src/main/resources/dockerfiles/DockerfileNativeStatic

graemerocher commented 3 years ago

How are these built in the maven plugin?

alvarosanchez commented 3 years ago
./mvn package -Dpackaging=docker-native -Dmicronaut.native-image.static=true

That uses the above Dockerfile

graemerocher commented 3 years ago

What base image is used?

alvarosanchez commented 3 years ago

oracle/graalvm-ce-$version-java11

graemerocher commented 3 years ago

This can be done with:

dockerfileNative {
    baseImage = "gcr.io/distroless/cc-debian10"
}
nativeImage {
    args("-H:+StaticExecutableWithDynamicLibC")
}
alvarosanchez commented 3 years ago

Not quite the same, those are "mostly static" native images, libc is still required in the Docker base image

"Purely static", musl based native images can be run without any other dependencies (i.e. FROM scratch)

Xaseron commented 3 years ago
./mvn package -Dpackaging=docker-native -Dmicronaut.native-image.static=true

That uses the above Dockerfile

How can I achieve this with Gradle?

binoysankar commented 3 years ago

@alvarosanchez am getting the below error

cp: cannot stat '/usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.a': No such file or directory

The command '/bin/sh -c mkdir ${RESULT_LIB} &&     curl -L -o musl.tar.gz https://musl.libc.org/releases/musl-1.2.1.tar.gz &&     mkdir musl && tar -xzf musl.tar.gz -C musl --strip-components 1 && cd musl &&     ./configure --disable-shared --prefix=${RESULT_LIB} &&     make && make install &&     cd / && rm -rf /muscl && rm -f /musl.tar.gz &&     cp /usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.a ${RESULT_LIB}/lib/' returned a non-zero code: 1
Xaseron commented 3 years ago

@binoysankar you have to take a look at the base image which version of libstdc++ is present.

ilopmar commented 3 years ago

This can't be done at this moment with the Gradle plugin out of the box. You have a few options: