3

我创建了一个通用的 docker 构建文件,其中包括 kubectl 和 Helm 等实用程序,但现在我想将采用 openjdk 与 Maven 一起添加到图像中,但是当我测试它时,Java 会因核心转储而崩溃。

这是 Dockerfile:

# docker-build-test-image

FROM    docker:stable-git

ENV     KUBERNETES_VERSION 1.11.10
ENV     HELM_VERSION 2.14.0
ENV     JDK_FILE=OpenJDK11U-jdk_x64_linux_hotspot_11.0.4_11.tar.gz
ENV     JDK_DIR=/opt/jdk-11.0.4+11
ENV     LANG=C.UTF-8
ENV     MAVEN_FILE apache-maven-3.3.9-bin.tar.gz

RUN     apk update && \
        apk add --no-cache curl && \
        apk add --no-cache bash && \
        apk add --no-cache jq && \
        apk add --no-cache gettext && \
        apk add --no-cache wget

RUN     apk add -U openssl curl tar gzip bash ca-certificates git && \
        curl -L -o /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
        curl -L -O https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.28-r0/glibc-2.28-r0.apk && \
        apk add glibc-2.28-r0.apk && \
        rm glibc-2.28-r0.apk

RUN     curl "https://kubernetes-helm.storage.googleapis.com/helm-v${HELM_VERSION}-linux-amd64.tar.gz" | tar zx && \
        mv linux-amd64/helm /usr/bin/ && \
        mv linux-amd64/tiller /usr/bin/ && \
        helm version --client && \
        tiller -version

RUN     curl -L -o /usr/bin/kubectl "https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl" && \
        chmod +x /usr/bin/kubectl && \
        kubectl version --client

# https://github.com/frol/docker-alpine-glibc/blob/master/Dockerfile
# Here we install GNU libc (aka glibc) and set C.UTF-8 locale as default.

RUN ALPINE_GLIBC_BASE_URL="https://github.com/sgerrand/alpine-pkg-glibc/releases/download" && \
    ALPINE_GLIBC_PACKAGE_VERSION="2.28-r0" && \
    ALPINE_GLIBC_BASE_PACKAGE_FILENAME="glibc-$ALPINE_GLIBC_PACKAGE_VERSION.apk" && \
    ALPINE_GLIBC_BIN_PACKAGE_FILENAME="glibc-bin-$ALPINE_GLIBC_PACKAGE_VERSION.apk" && \
    ALPINE_GLIBC_I18N_PACKAGE_FILENAME="glibc-i18n-$ALPINE_GLIBC_PACKAGE_VERSION.apk" && \
    apk add --no-cache --virtual=.build-dependencies wget ca-certificates && \
    echo \
        "-----BEGIN PUBLIC KEY-----\
        MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApZ2u1KJKUu/fW4A25y9m\
        y70AGEa/J3Wi5ibNVGNn1gT1r0VfgeWd0pUybS4UmcHdiNzxJPgoWQhV2SSW1JYu\
        tOqKZF5QSN6X937PTUpNBjUvLtTQ1ve1fp39uf/lEXPpFpOPL88LKnDBgbh7wkCp\
        m2KzLVGChf83MS0ShL6G9EQIAUxLm99VpgRjwqTQ/KfzGtpke1wqws4au0Ab4qPY\
        KXvMLSPLUp7cfulWvhmZSegr5AdhNw5KNizPqCJT8ZrGvgHypXyiFvvAH5YRtSsc\
        Zvo9GI2e2MaZyo9/lvb+LbLEJZKEQckqRj4P26gmASrZEPStwc+yqy1ShHLA0j6m\
        1QIDAQAB\
        -----END PUBLIC KEY-----" | sed 's/   */\n/g' > "/etc/apk/keys/sgerrand.rsa.pub" && \
    wget \
        "$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_BASE_URL/$ALPINE_GLIBC_PACKAGE_VERSION/$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
    apk add --no-cache \
        "$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_I18N_PACKAGE_FILENAME" && \
    \
    rm "/etc/apk/keys/sgerrand.rsa.pub" && \
    /usr/glibc-compat/bin/localedef --force --inputfile POSIX --charmap UTF-8 "$LANG" || true && \
    echo "export LANG=$LANG" > /etc/profile.d/locale.sh && \
    \
    apk del glibc-i18n && \
    \
    rm "/root/.wget-hsts" && \
    apk del .build-dependencies && \
    rm \
        "$ALPINE_GLIBC_BASE_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_BIN_PACKAGE_FILENAME" \
        "$ALPINE_GLIBC_I18N_PACKAGE_FILENAME"

WORKDIR /tmp
RUN     chmod 755 /opt
COPY    $JDK_FILE /tmp/
RUN     tar -zvxf /tmp/$JDK_FILE -C /opt/ \
        && rm /tmp/$JDK_FILE
ENV     JAVA_HOME $JDK_DIR
ENV     PATH "$PATH:$JDK_DIR/bin"

RUN     wget --no-cookies --no-check-certificate "https://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/$MAVEN_FILE"
RUN     pwd
RUN     ls -ltr
RUN     tar -zvxf $MAVEN_FILE  -C /opt/
RUN     rm $MAVEN_FILE
ENV     PATH "$PATH:/opt/apache-maven-3.3.9/bin"

COPY    ./settings.xml /root/.m2/

JDK_FILE "OpenJDK11U-jdk_x64_linux_hotspot_11.0.4_11.tar.gz" 直接从https://adoptopenjdk.net/releases.html?variant=openjdk11&jvmVariant=hotspot下载。

一个非常简单的 Java 测试文件:

class HelloWorld 
{ 
    public static void main(String args[]) 
    { 
        System.out.println("Hello, World"); 
    } 
}

我正在通过 GitLab 管道构建它:

image: ${BUILD_URL}/tools/build/docker-build-test-image/add-jdk:20191129.33

stages:
  - build

build:
  stage: build
  script:
    - java -version
    - javac HelloWorld.java
    - java HelloWorld

这是输出:

$ java -version
openjdk version "11.0.4" 2019-07-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  [thread 20 also had an error]SIGSEGV
 (0xb) at pc=0x00007f0c0251bba1, pid=18, tid=28
#
# JRE version: OpenJDK Runtime Environment (11.0.4+11) (build 11.0.4+11)
# Java VM: OpenJDK 64-Bit Server VM (11.0.4+11, mixed mode, tiered, compressed oops, serial gc, linux-amd64)
# Problematic frame:
# C  [libc.so.6+0x117ba1]
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/libexec/abrt-hook-ccpp %s %c %p %u %g %t e %P %I %h" (or dumping to /builds/d4e/playground/revills/java_test/core.18)
#
# An error report file with more information is saved as:
# /builds/d4e/playground/revills/java_test/hs_err_pid18.log
[thread 19 also had an error]
#
# If you would like to submit a bug report, please visit:
#   https://github.com/AdoptOpenJDK/openjdk-build/issues
#
/bin/bash: line 111:    18 Aborted                 (core dumped) java -version

你能告诉我我做错了什么吗?

干杯,史蒂夫

4

1 回答 1

3

如果你在基本的 alpine 上构建,你可以添加 open-jdk 和 maven。它可能看起来像这样:

from alpine:3.10

run apk --no-cache add openjdk11 --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
run apk add bash vim curl wget jq docker git tar unzip bash-completion ca-certificates
run cd /opt && curl -sSl http://mirror.vorboss.net/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz | tar -xz

env PATH "$PATH:/opt/apache-maven-3.6.3/bin"
entrypoint ["/bin/bash"]

这将使用 java 11 和最新的 maven 设置一个 alpine 映像(截至撰写时)。通常我们会用一个运行命令来做到这一点,以最大限度地减少我们图像中的层数,但我会把这个优化留给你的想象。

我添加了一行显示如何以及在何处安装其他工具,如 Docker、bash、vim、curl 等。其中一些用于安装 maven(tar 解压缩),而另一些仅用于装饰。你真的只需要curl,tarunzip, 来回答这个问题,但是当你正在构建一个 devbox 时,我想我会添加更多用于演示。

当你决定为这样的项目使用哪个基础镜像时,通常最好从基础操作系统开始,比如 apline:3,而不是一些随机的镜像,恰好有一个你需要安装的东西(git 在你的例子)。像 OO 一样对待它——你继承了 git 映像,所以你也是一个 git 映像。在您的情况下,您并不是真正的 git 映像,您是瑞士军刀,并且您不知道底层操作系统的真正含义是另一种选择(如果您使用 :latest 作为标签,则您没有想一想未来会变成什么)。

于 2019-11-30T14:06:44.367 回答