alpine Linux简介
Alpine Linux是一个轻型Linux发行版
,它不同于通常的Linux发行版,Alpine采用了musl libc 和 BusyBox以减少系统的体积和运行时的资源消耗。
制作alpine基础镜像
下载最新版本镜像文件https://mirrors.aliyun.com/alpine/v3.19/releases/x86_64/alpine-minirootfs-3.19.1-x86_64.tar.gz
官方地址:
https://github.com/alpinelinux/docker-alpine/blob/v3.19/x86_64/Dockerfile
dockerfile内容:
FROM scratch
ADD alpine-minirootfs-3.19.1-x86_64.tar.gz /
CMD ["/bin/sh"]
构建alpine基础镜像:
docker build -t alpine:v3.19.1 .
alpine制作jdk镜像
alpine能够如此小巧是因为它没有集成一些普通的Linux库,比如跑Java应用必须依赖的glibc相关的包,alpine linux构建oracle jdk时,需要先安装一些依赖库
glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
glibc库下载:glibc-2.35-r1.apk glibc-bin-2.35-r1.apk glibc-dev-2.35-r1.apk glibc-i18n-2.35-r1.apk
https://github.com/sgerrand/alpine-pkg-glibc/releases/
秘钥下载:
wget https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub
dockerfile内容:
FROM alpine:v3.19.1
MAINTAINER lyx
WORKDIR /opt
#更换aline源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
#update apk
RUN apk update && apk upgrade
RUN apk --no-cache --upgrade add ca-certificates
ADD sgerrand.rsa.pub /etc/apk/keys/sgerrand.rsa.pub
# bash wget curl net-tools
RUN apk add --no-cache bash bash-doc bash-completion
RUN apk add --no-cache wget curl net-tools
RUN /bin/bash
#setup glibc
COPY glibc-2.35-r1.apk /opt
COPY glibc-bin-2.35-r1.apk /opt
COPY glibc-dev-2.35-r1.apk /opt
COPY glibc-i18n-2.35-r1.apk /opt
RUN apk add --no-cache --force-overwrite glibc-2.35-r1.apk glibc-bin-2.35-r1.apk glibc-dev-2.35-r1.apk glibc-i18n-2.35-r1.apk
#delete apk
RUN rm -f glibc-2.35-r1.apk glibc-bin-2.35-r1.apk glibc-dev-2.35-r1.apk glibc-i18n-2.35-r1.apk
#setup date
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# apk add fontconfig alpine linux版本大于3.15后,若java应用中包含生成图形验证码相关代码,java应用报底层错误,导致应用停止验证码无法生成的问题
RUN apk add --no-cache fontconfig
RUN rm -rf /var/cache/apk/*
#setup language 解决中文乱码
RUN /usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8
ENV LANG=en_US.UTF-8
#copy jdk
ADD jdk-8u401-linux-x64.tar.gz /opt
#setup java env
ENV JAVA_HOME=/opt/jdk1.8.0_401
ENV JRE_HOME=/opt/jdk1.8.0_401/jre
ENV CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
ENV PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
#软链接解决库文件报错
RUN ln -sf /usr/glibc-compat/lib/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
CMD ["java","-version"]
执行dockerfile创建镜像 :
docker build -t alpinejdk:v1 .
启动进入容器:
docker run -it --name alpinejdk alpinejdk:v1 /bin/sh
测试jdk:
java -version