Post

GraalVM 23

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]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/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]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# ----------------------------------
# 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                              "tech@zogg.fr"
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]

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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]

1
2
3
4
#/bin/sh
# 2023-08-14

echo $JAVA_HOME

entrypoint.sh

[Fichier]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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 :

1
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
Cet article est sous licence CC BY 4.0 par l'auteur.

© 2022- Olivier. Certains droits réservés.

Propulsé par τζ avec le thème Χ