Zacard's Notes

Docker学习系列五:构建oracle-jdk8镜像

前言

由于docker官方的jdk镜像都是openjdk,与我们实际开发上线的jdk环境不符。因此需要构建一个oracle-jdk8的基础镜像。

参考的是dockerhub中一个oracle-jdk高star的配置:传送门

Dockerfile描述

# AlpineLinux with a glibc-2.21 and Oracle Java 8
FROM alpine:3.3

MAINTAINER zacard <[email protected]>
# Java Version and other ENV
ENV JAVA_VERSION_MAJOR=8 \
    JAVA_VERSION_MINOR=92 \
    JAVA_VERSION_BUILD=14 \
    JAVA_PACKAGE=jdk \
    JAVA_HOME=/opt/jdk \
    PATH=${PATH}:/opt/jdk/bin \
    LANG=C.UTF-8

# do all in one step
RUN apk upgrade --update && \
    apk add --update curl ca-certificates bash && \
    for pkg in glibc-2.23-r1 glibc-bin-2.23-r1 glibc-i18n-2.23-r1; do curl -sSL https://github.com/andyshinn/alpine-pkg-glibc/releases/download/2.23-r1/${pkg}.apk -o /tmp/${pkg}.apk; done && \
    apk add --allow-untrusted /tmp/*.apk && \
    rm -v /tmp/*.apk && \
    ( /usr/glibc-compat/bin/localedef --force --inputfile POSIX --charmap UTF-8 C.UTF-8 || true ) && \
    echo "export LANG=C.UTF-8" > /etc/profile.d/locale.sh && \
    /usr/glibc-compat/sbin/ldconfig /lib /usr/glibc-compat/lib && \
    mkdir /opt && curl -jksSLH "Cookie: oraclelicense=accept-securebackup-cookie" -o /tmp/java.tar.gz \
    http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-b${JAVA_VERSION_BUILD}/${JAVA_PACKAGE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-linux-x64.tar.gz && \
    gunzip /tmp/java.tar.gz && \
    tar -C /opt -xf /tmp/java.tar && \
    apk del curl glibc-i18n && \
    ln -s /opt/jdk1.${JAVA_VERSION_MAJOR}.0_${JAVA_VERSION_MINOR} /opt/jdk && \
    rm -rf /opt/jdk/*src.zip \
        /opt/jdk/lib/missioncontrol \
        /opt/jdk/lib/visualvm \
        /opt/jdk/lib/*javafx* \
        /opt/jdk/jre/plugin \
        /opt/jdk/jre/bin/javaws \
        /opt/jdk/jre/bin/jjs \
        /opt/jdk/jre/bin/keytool \
        /opt/jdk/jre/bin/orbd \
        /opt/jdk/jre/bin/pack200 \
        /opt/jdk/jre/bin/policytool \
        /opt/jdk/jre/bin/rmid \
        /opt/jdk/jre/bin/rmiregistry \
        /opt/jdk/jre/bin/servertool \
        /opt/jdk/jre/bin/tnameserv \
        /opt/jdk/jre/bin/unpack200 \
        /opt/jdk/jre/lib/javaws.jar \
        /opt/jdk/jre/lib/deploy* \
        /opt/jdk/jre/lib/desktop \
        /opt/jdk/jre/lib/*javafx* \
        /opt/jdk/jre/lib/*jfx* \
        /opt/jdk/jre/lib/amd64/libdecora_sse.so \
        /opt/jdk/jre/lib/amd64/libprism_*.so \
        /opt/jdk/jre/lib/amd64/libfxplugins.so \
        /opt/jdk/jre/lib/amd64/libglass.so \
        /opt/jdk/jre/lib/amd64/libgstreamer-lite.so \
        /opt/jdk/jre/lib/amd64/libjavafx*.so \
        /opt/jdk/jre/lib/amd64/libjfx*.so \
        /opt/jdk/jre/lib/ext/jfxrt.jar \
        /opt/jdk/jre/lib/ext/nashorn.jar \
        /opt/jdk/jre/lib/oblique-fonts \
        /opt/jdk/jre/lib/plugin.jar \
        /tmp/* /var/cache/apk/* && \
    echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf

# EOF

说明

为什么要基于alpine:3.3

看考官方java镜像中有关java:alpine的说明:传送门

摘录其中一段:

This variant is highly recommended when final image size being as small as possible is desired

官方推荐使用基于alpine制作的镜像。

build镜像

docker build -t oraclejdk8 .

p.s:由于此镜像需要下载一些基础包和jdk,可能需要比较长的build时间,同时可能需要翻墙~

测试镜像

docker run -ti --rm oraclejdk8 java -version

保存镜像

docker save -o jdk8.tar oraclejdk8

加载镜像

docker load -i jdk8.tar
坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章