Cette image est directement compatible avec l’outil Pterodactyl.
Introduction
Comme à date il n’y a toujours pas d’image Docker pour GraalVM v23, j’ai décidé d’en construire une.
Il est nécessaire d’avoir installé au préalable Docker.
Pour construire une image Docker compatible AMD64 vous aurez besoin de 8 fichiers :
- build.sh
- platform.sh
- Dockerfile
- bashrc
- docker-java-home
- entrypoint.sh
Fichiers requis
platform.sh
[Fichier]
#!/bin/bash
# Used in Docker build to set platform dependent variables
case $TARGETARCH in
"amd64")
echo "x86_64-unknown-linux-gnu" > /.platform
echo "" > /.compiler
;;
"arm64")
echo "aarch64-unknown-linux-gnu" > /.platform
echo "gcc-aarch64-linux-gnu" > /.compiler
;;
"arm")
echo "armv7-unknown-linux-gnueabihf" > /.platform
echo "gcc-arm-linux-gnueabihf" > /.compiler
;;
esac
build.sh
[Fichier]
#!/bin/bash
# 2023-08-14
# ----------------------------------
# Pterodactyl Core Dockerfile Builder
# Environment: Java / GraalVM
# Minimum Panel Version: 1.7.0
# ----------------------------------
clear
cd \"\$\(dirname \"\$0\")\" \|| exit 1
# Docker image name and version
IMAGE_BASE=zogg/graalvm
IMAGE_NAME_LATEST=${IMAGE_BASE}:latest
# Prepare for cross compile
export DOCKER_DEFAULT_PLATFORM=linux/amd64
export DOCKER_CLI_EXPERIMENTAL=enabled
docker run --privileged --rm tonistiigi/binfmt --install all
# Build
docker buildx build \
--pull \
--output=type=docker \
--compress \
--network host \
\
--platform=linux/amd64 \
\
--build-arg TZ=Europe/Paris \
--build-arg CONCURRENCY=$(nproc) \
\
-t "${IMAGE_NAME_LATEST}" \
. 2>&1 | tee build.log
exit 0
Dockerfile
[Fichier]
# ----------------------------------
# Pterodactyl Core Dockerfile
# Environment: Java
# Minimum Panel Version: 1.7.0
# ----------------------------------
# Copyright (c) 2021 Matthew Penner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ----------------------------------
## HOWTO:
## debug image with local bash session:
##
## docker run -it --entrypoint bash zogg/graalvm:latest
##
# 2023-08-14
# --------------------------------------------------
# Intermediary Build
# --------------------------------------------------
FROM bitnami/minideb:latest AS build
# Arguments from Docker build
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
ARG BUILDPLATFORM
ARG BUILDOS
ARG BUILDARCH
ARG BUILDVARIANT
ARG TZ
ARG CONCURRENCY
# Arguments to build specified version
ARG GRAALVM_YEAR_VERSION=20
ARG GRAALVM_MAJOR_VERSION=0
ARG GRAALVM_MINOR_VERSION=2
ARG JAVA_VERSION=20
ARG SDKMAN=/root/.sdkman
# Define environement variables
ENV GRAALVERSION $GRAALVM_YEAR_VERSION.$GRAALVM_MAJOR_VERSION.$GRAALVM_MINOR_VERSION-graal
# Update & upgrade APK + Install required packages
USER root
WORKDIR /
RUN set -eux \
&& apt update \
&& apt-get install -y --no-install-recommends locales p11-kit openssl tar sqlite3 fontconfig tzdata bash wget curl zip unzip ca-certificates iproute2 \
&& echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen en_US.UTF-8 \
&& rm -rf /var/lib/apt/lists/* \
&& apt autoremove -y
# Set bash as default shell interpreter
SHELL ["/bin/bash", "-c"]
# Used by Docker build to set platform dependent variables
COPY platform.sh .
RUN chmod +x platform.sh \
&& ./platform.sh \
&& rm -rf platform.sh
# Copy Pterodactyl necessary files
COPY docker-java-home /usr/local/bin/docker-java-home
COPY bashrc /home/container/.bashrc
COPY entrypoint.sh /entrypoint.sh
# Setup execution rights
RUN chmod +x /usr/local/bin/docker-java-home \
&& chmod +x /entrypoint.sh
# Install SDKMAN! & GraalVM specified version
RUN curl -s \"https://get.sdkman.io\" | bash \
&& echo "sdkman_auto_answer=true" > $SDKMAN/etc/config \
&& echo "sdkman_auto_selfupdate=false" >> $SDKMAN/etc/config \
&& echo "sdkman_insecure_ssl=true" >> $SDKMAN/etc/config \
&& chmod +x $SDKMAN/bin/sdkman-init.sh
RUN source "$SDKMAN/bin/sdkman-init.sh" \
&& sdk install java $GRAALVERSION
# Copy GraalVM files to /usr/java/default
RUN mkdir -p /usr/java/default/ \
&& cp -rfLu $SDKMAN/candidates/java/$GRAALVERSION/. /usr/java/default/
# Uninstall SDKMAN!
RUN source "$SDKMAN/bin/sdkman-init.sh" \
&& sdk uninstall java $GRAALVERSION --force \
&& sdk flush \
&& rm -rf $SDKMAN
# Set GraalVM as default Java interpreter
RUN ln -sf /usr/java/default /usr/java/latest \
&& for bin in "/usr/java/default/bin/"*; do base="$(basename "$bin")"; \
[ ! -e "/usr/bin/$base" ]; \
update-alternatives --install "/usr/bin/$base" "$base" "$bin" 20000; \
done
# --------------------------------------------------
# Final Docker Image
# --------------------------------------------------
FROM bitnami/minideb:latest
USER root
WORKDIR /
# Copy from build
COPY --from=build /usr/bin /usr/bin/
COPY --from=build /usr/lib /usr/lib/
COPY --from=build /usr/lib64 /usr/lib64/
COPY --from=build /usr/java/default /usr/java/default/
COPY --from=build /usr/local/bin/docker-java-home /usr/local/bin/
COPY --from=build /home/container/.bashrc /home/container/
COPY --from=build /entrypoint.sh /
# Set bash as default shell interpreter
SHELL ["/bin/bash", "-c"]
# Setup 'container' user and group (with home folder)
RUN useradd -d /home/container -m container -s /bin/bash -p 'none' \
&& passwd -d container
# Disable Watchtower image check
LABEL com.centurylinklabs.watchtower.enable false
# Define author's informations
LABEL org.opencontainers.image.title "GraalVM Special Edition"
LABEL author "Olivier Le Bris"
LABEL maintainer "[email protected]"
LABEL org.opencontainers.image.source "https://zogg.fr"
LABEL org.opencontainers.image.licenses MIT
# Define environement variables
ENV LANG C.UTF-8
ENV TZ $TZ
ENV JAVA_HOME /usr/java/default
ENV GRAALVERSION $GRAALVM_YEAR_VERSION.$GRAALVM_MAJOR_VERSION.$GRAALVM_MINOR_VERSION-graal
ENV GRAALVM_YEAR_VERSION $GRAALVM_YEAR_VERSION
ENV GRAALVM_MAJOR_VERSION $GRAALVM_MAJOR_VERSION
ENV GRAALVM_MINOR_VERSION $GRAALVM_MINOR_VERSION
ENV JAVA_VERSION $JAVA_VERSION
ENV PATH $JAVA_HOME/bin:${PATH}
# Setup Pterodactyl necessary context
USER container
WORKDIR /home/container
ENV USER container
ENV HOME /home/container
# Run Pterodactyl entrypoint
CMD ["/bin/bash", "/entrypoint.sh"]
bashrc
[Fichier]
# 2023-08-14
export PS1='\n\[\e[32m\]\u@\h: \[\e[33m\]\w\[\e[0m\]\n> '
export BASH=/bin/bash
export SHELL=$BASH
export LS_OPTIONS='--color=auto'
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias la='ls $LS_OPTIONS -la'
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
docker-java-home
[Fichier]
#/bin/sh
# 2023-08-14
echo $JAVA_HOME
entrypoint.sh
[Fichier]
#!/bin/bash
# 2023-08-14
#
# Copyright (c) 2021 Matthew Penner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Default the TZ environment variable to UTC.
TZ=${TZ:-UTC}
export TZ
# Set environment variable that holds the Internal Docker IP
INTERNAL_IP=$(ip route get 1 | awk '{print $NF;exit}')
export INTERNAL_IP
# Switch to the container's working directory
cd /home/container \|| exit 1
# Display Specifics
printf ">>> [ Zogg GraalVM ] <<<\n"
# Print Java version
printf "\033[1m\033[33mcontainer@pterodactyl~ \033[0mjava -version\n"
java -version
# Convert all of the "{{VARIABLE}}" parts of the command into the expected shell
# variable format of "${VARIABLE}" before evaluating the string and automatically
# replacing the values.
PARSED=\$\(echo \"\${STARTUP}\" | sed -e 's/{{/${/g' -e 's/}}/}/g' | eval echo \"\$\(cat -)\")
# Display the command we're running in the output, and then execute it with the env
# from the container itself.
printf "\033[1m\033[33mcontainer@pterodactyl~ \033[0m%s\n" "$PARSED"
# shellcheck disable=SC2086
exec env ${PARSED}
Procédure
Pour lancer la construction de l’image, il suffit de donner au script shell build.sh les droits d’exécution puis de le lancer :
sudo chmod +x build.sh && sudo bash build.sh
Conclusion
Vous avez maintenant une image Docker compatible AMD64 à lancer sous Docker (ou avec Portainer :p).
NB : Vous pouvez l’utiliser directement sous Pterodactyl à condition d’avoir une Registry Docker sur laquelle vous pouvez déposer l’image pour qu’elle puisse être récupérée par Pterodactyl de la même façon que les images natives.
Changelog
2023-08-14
- Simplification du Dockerfile et de build.sh
- Tentative avorté de passer par Alpine (problèmes liés à libc)
- Utilisation d’un build multi-stage
- Passage de debian:stable-slim à bitnami/minideb:latest
- Réduction de la taille finale de l’image amd64 de 2 Go à 960 Mo
- Merci à Uzurka pour les debugs et les suggestions pour diminuer la taille de l’image finale