尝试基于Alpine构建Jenkins Docker镜像时出现AWT错误 -

时间:2016-03-28 19:08:03

标签: jenkins docker openjdk alpine

我正在尝试构建自己的Jenkins Docker镜像。我使用Alpine-based Jdk Docker image。我为Alpine-Linux改编了Jenkins Docker image。 我必须安装一些软件包才能使其正常构建,但是一旦我运行它,我就会遇到以下错误:

hudson.util.AWTProblem: java.lang.NullPointerException
    at hudson.WebAppMain.contextInitialized(WebAppMain.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:782)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:424)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:774)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:249)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1242)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:717)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:494)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
    at org.eclipse.jetty.server.Server.doStart(Server.java:282)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
    at winstone.Launcher.<init>(Launcher.java:156)
    at winstone.Launcher.main(Launcher.java:356)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at Main._main(Main.java:293)
    at Main.main(Main.java:98)
Caused by: java.lang.NullPointerException
    at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
    at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
    at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
    at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
    at sun.font.SunFontManager$2.run(SunFontManager.java:431)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.SunFontManager.<init>(SunFontManager.java:376)
    at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
    at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
    at java.awt.Font.getFont2D(Font.java:491)
    at java.awt.Font.getFamily(Font.java:1220)
    at java.awt.Font.getFamily_NoClientCode(Font.java:1194)
    at java.awt.Font.getFamily(Font.java:1186)
    at java.awt.Font.toString(Font.java:1683)
    at hudson.util.ChartUtil.<clinit>(ChartUtil.java:255)
    at hudson.WebAppMain.contextInitialized(WebAppMain.java:184)
    ... 19 more

我在我的朋友Google之后尝试了不同的解决方案,即安装 fontconfig 包或添加Java参数

  

JAVA_OPTS = “ - Djava.awt.headless =真”

或将JDK版本更改为7的事件,但似乎没有任何效果。 这是我的dockerfile

FROM java:8-jdk-alpine

RUN apk update && apk add wget git curl zip bash fontconfig && rm -rf /var/lib/apt/lists/*

ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT 50000

# Jenkins is run with user `jenkins`, uid = 1000
# If you bind mount a volume from the host or a data container,
# ensure you use the same uid
RUN adduser -S -h "$JENKINS_HOME" -u 1000 -s /bin/bash jenkins

# Jenkins home directory is a volume, so configuration and build history
# can be persisted and survive image upgrades
VOLUME /var/jenkins_home

# `/usr/share/jenkins/ref/` contains all reference configuration we want
# to set on a fresh new installation. Use it to bundle additional plugins
# or config file with your custom jenkins Docker image.
RUN mkdir -p /usr/share/jenkins/ref/init.groovy.d

ENV TINI_SHA 066ad710107dc7ee05d3aa6e4974f01dc98f3888

# Use tini as subreaper in Docker container to adopt zombie processes
RUN curl -fL https://github.com/krallin/tini/releases/download/v0.5.0/tini-static -o /bin/tini \
  && chmod +x /bin/tini \
  && echo "$TINI_SHA  /bin/tini" | sha1sum -c -

COPY init.groovy /usr/share/jenkins/ref/init.groovy.d/tcp-slave-agent-port.groovy

ENV JENKINS_VERSION 1.642.2
ENV JENKINS_SHA e72e06e64d23eefb13090459f517b0697aad7be0


# could use ADD but this one does not check Last-Modified header
# see https://github.com/docker/docker/issues/8331
RUN curl -fL http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war -o /usr/share/jenkins/jenkins.war \
  && echo "$JENKINS_SHA  /usr/share/jenkins/jenkins.war" | sha1sum -c -

ENV JENKINS_UC https://updates.jenkins-ci.org
RUN chown -R jenkins "$JENKINS_HOME" /usr/share/jenkins/ref

# for main web interface:
EXPOSE 8080

# will be used by attached slave agents:
EXPOSE 50000

ENV COPY_REFERENCE_FILE_LOG $JENKINS_HOME/copy_reference_file.log

USER jenkins

COPY jenkins.sh /usr/local/bin/jenkins.sh
ENTRYPOINT ["/bin/tini", "--", "/usr/local/bin/jenkins.sh"]

# from a derived Dockerfile, can use `RUN plugins.sh active.txt` to setup /usr/share/jenkins/ref/plugins from a support bundle
COPY plugins.sh /usr/local/bin/plugins.sh
希望你们中的一个人可以提供帮助,或者我会选择更重的詹金斯官方形象。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

也许你正在努力完成我所做的事:https://hub.docker.com/r/serverking/jenkinsdsl/

我还尝试在dockerfile下添加不同的依赖项来解决问题:

FROM alpine:latest

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk/jre
ENV JENKINS_HOME /var/jenkins_home
ENV JENKINS_SLAVE_AGENT_PORT 50000
ENV JENKINS_VERSION 2.19.4

# Add scripts and plugin list
ADD src /

# Packages
RUN set -x && \
    apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/main && \
    apk add --no-cache --repository  http://dl-cdn.alpinelinux.org/alpine/edge/community && \
    apk update && \
    apk upgrade && \
    apk add --no-cache ca-certificates supervisor openjdk8 bash git curl zip wget docker ttf-dejavu jq coreutils openssh py2-pip && \
    echo "*** fix key permissions ***" && \
    chmod 600 /root/.ssh/id_rsa && \
    echo "*** Installing docker-compose ***" && \
    pip install --upgrade pip && \
    pip install docker-compose

# Install Jenkins and plugins from plugins.txt
RUN set -x && \
    echo "*** Installing jenkins ***" && \
    curl -sSL --create-dirs --retry 1 http://repo.jenkins-ci.org/public/org/jenkins-ci/main/jenkins-war/${JENKINS_VERSION}/jenkins-war-${JENKINS_VERSION}.war -o /usr/share/jenkins/jenkins.war && \
    echo "*** Recursive solve and reduce plugin dependencies ***" && \
    bash -c 'curl -sSO https://updates.jenkins-ci.org/current/update-center.actual.json && \
    function solve { \
        for dependency in $(cat update-center.actual.json | jq --arg p "${1%:*}" -r '"'"'.plugins[] | select(.name == $p) | .dependencies[] | select(.optional == false) | .name + ":" + .version'"'"');do \
            echo $dependency >> /var/jenkins_home/plugins.txt; \
            solve $dependency; \
        done \
    } && \
    for plugin in $(tr '"'"'\n'"'"' '"'"' '"'"' < /var/jenkins_home/plugins.txt);do solve $plugin; done && \
    sort -Vr /var/jenkins_home/plugins.txt | sort -u -t: -k1,1 -o /var/jenkins_home/plugins.txt' && \
    echo "*** Jenkins install plugins from plugins.txt *** " && \
    while read plugin; do \
    echo "*** Downloading ${plugin} ***" && \
    curl -sSL --create-dirs --retry 3 https://updates.jenkins-ci.org/download/plugins/${plugin%:*}/${plugin#*:}/${plugin%:*}.hpi -o /var/jenkins_home/plugins/${plugin%:*}.jpi && \
    touch /var/jenkins_home/plugins/${plugin%:*}.jpi.pinned; \
    done < /var/jenkins_home/plugins.txt

EXPOSE 8080 8443 50000

ENTRYPOINT ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

为什么我自己做而不是使用官方的JenkinsCI Dockerfile?

  • 插件也必须是不可变的,否则构建不是 确定性的。

  • 官方的不处理XML和不变性。没有人 实际上想要编辑XLM文件。

  • 存储无法应对docker不可变概念,因此执行者 制动器。

  • 构建必须是一次性的,如果存在,必须重新连接 仍然存在的工作。

现在这一切都解决了。