基于Alpine 制作JDK的多架构镜像 —— 筑梦之路

安装 JDK 的话,因为 Alpine是基于MUSL libc(mini libc),所以需要安装glibc的库

官方文档:GitHub - sgerrand/alpine-pkg-glibc: A glibc compatibility layer package for Alpine Linux

GitHub - sgerrand/docker-glibc-builder: A glibc binary package builder in Docker

 官方仓库镜像:GitHub - gliderlabs/docker-alpine: Alpine Linux Docker image. Win at minimalism!

官方镜像:Docker 

官方仓库:Alpine Linux · GitHub

为什么要选择使用alpine安装JDK?

1. alpine系统非常小,几M的大小,用来作为基础镜像,可以降低构建出来的镜像尺寸大小,创建容器就很快,也节省磁盘空间

2. alpine支持多种cpu架构,适合构建多架构镜像,适配更多硬件平台。

OpenJDK

# 这里以openjdk8版本为例

#编写Dockerfile

FROM alpine

ARG VER_GLIBC=2.35-r0
ARG GLIBC_DOWNLOAD_URL=https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${VER_GLIBC}

WORKDIR /root

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && apk upgrade --update-cache && \
    wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    wget ${GLIBC_DOWNLOAD_URL}/glibc-${VER_GLIBC}.apk && \
    wget ${GLIBC_DOWNLOAD_URL}/glibc-bin-${VER_GLIBC}.apk && \
    wget ${GLIBC_DOWNLOAD_URL}/glibc-i18n-${VER_GLIBC}.apk && \
    apk  add glibc-${VER_GLIBC}.apk glibc-bin-${VER_GLIBC}.apk glibc-i18n-${VER_GLIBC}.apk bash wget curl tzdata fping vim net-tools && \
    sleep 2 && \
    apk add openjdk8 && \
    rm -rf /tmp/* /var/cache/apk/* /root/* && \
    /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8 && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ln -sf /bin/sh /bin/bash

ENV LANG=C.UTF-8 \
    TZ=Asia/Shanghai


如果需要安装openjdk11 修改为apk add openjdk11

构建多架构镜像参考:Docker 利用buildx插件构建多平台镜像——筑梦之路_筑梦之路的博客-CSDN博客_buildkitd.toml https

docker run --privileged --rm tonistiigi/binfmt --install all

docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master

docker buildx build --platform=linux/arm64,linux/amd64 -t jdk:openjdk8-alpine -f Dockerfile . --push

docker buildx rm mybilder-cn

OracleJDK(glibc没处理好,还存在问题)

#OracleJDK 

FROM alpine

ARG VER_GLIBC=2.35-r0
ARG GLIBC_DOWNLOAD_URL=https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${VER_GLIBC}

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk update && \
    apk add --no-cache bash curl wget vim fping tzdata net-tools ca-certificates && \
    wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    wget --no-check-certificate --timeout=900 ${GLIBC_DOWNLOAD_URL}/glibc-${VER_GLIBC}.apk && \
    wget --no-check-certificate --timeout=900 ${GLIBC_DOWNLOAD_URL}/glibc-bin-${VER_GLIBC}.apk && \
    wget --no-check-certificate --timeout=900 ${GLIBC_DOWNLOAD_URL}/glibc-i18n-${VER_GLIBC}.apk && \
    apk add glibc-${VER_GLIBC}.apk glibc-bin-${VER_GLIBC}.apk glibc-i18n-${VER_GLIBC}.apk --force && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && ln -sf /bin/sh /bin/bash && \
    echo "Asia/Shanghai" > /etc/timezone && \
    rm -rf /tmp/* /var/cache/apk/* glibc-${VER_GLIBC}.apk glibc-bin-${VER_GLIBC}.apk glibc-i18n-${VER_GLIBC}.apk
ADD jdk-8u351-linux-x64.tar.gz /usr/local/
ENV TZ=Asia/Shanghai \
    LANG=C.UTF-8
ENV JAVA_HOME=/usr/local/jdk1.8.0_351
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH

你可能感兴趣的:(linux系统运维,虚拟化,云计算,linux,运维,服务器)