基础Java镜像Dockerfile(Alpine)

本篇主要记录Dockerfile优化笔记。

前言

之前公司Docker镜像都是以CentOS7为基础镜像进行一系列的添加和构建,后来发现镜像大小问题(一个应用镜像800M),导致部署时间过长,所以考虑对基础镜像进行优化。Alpine Linux Docker 镜像也继承了 Alpine Linux 发行版的这些优势。相比于其他 Docker 镜像,它的容量非常小,仅仅只有 5M,且拥有非常友好的包管理器,最终采用alpine为基础镜像。

Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#Mini Alpine Image Dockerfile For java :
FROM alpine:3.8
MAINTAINER Dictator 917043292@qq.com
ARG TZ="Asia/Shanghai"
ENV TZ ${TZ}
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8
ENV LANGUAGE en_US:en
ENV CATALINA_HOME /home/admin/taobao-tomcat


# modify the mirror && Install cURL
RUN sed -i -e 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories && \
apk --update add bash curl ca-certificates tar iproute2 bind-tools tzdata && \
ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone && \
curl -Ls https://xxxxx.oss-cn-hangzhou.aliyuncs.com/glibc-2.28-r0.apk > /tmp/glibc-2.28-r0.apk && \
curl -Ls https://xxxxx.oss-cn-hangzhou.aliyuncs.com/glibc-bin-2.28-r0.apk > /tmp/glibc-bin-2.28-r0.apk && \
curl -Ls https://xxxxx.oss-cn-hangzhou.aliyuncs.com/glibc-i18n-2.28-r0.apk > /tmp/glibc-i18n-2.28-r0.apk && \
apk add --allow-untrusted /tmp/glibc-2.28-r0.apk && \
apk add --allow-untrusted /tmp/glibc-bin-2.28-r0.apk && \
apk add --allow-untrusted /tmp/glibc-i18n-2.28-r0.apk && \
rm -rf /tmp/glibc-2.28-r0.apk && \
rm -rf /tmp/glibc-bin-2.28-r0.apk && \
rm -rf /tmp/glibc-i18n-2.28-r0.apk && \
/usr/glibc-compat/bin/localedef -i en_US -f UTF-8 en_US.UTF-8 && \
/usr/glibc-compat/bin/localedef -i zh_CN -f UTF-8 zh_CN.UTF-8 && \
mkdir -p /home/admin/app ${CATALINA_HOME}/deploy

# Java Version
ENV JAVA_VERSION_MAJOR 8
ENV JAVA_VERSION_MINOR 191
ENV JAVA_PACKAGE jdk
ENV EDAS_CONTAINER_VERSION V3.5.0

# Download and unarchive Java
#并且删除一些不必要的包
RUN mkdir /opt && \
curl -sL \
https://xxxxx.oss-cn-hangzhou.aliyuncs.com/jdk-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-linux-x64.tar.gz \
| tar -xzf - -C /opt && \
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/lib/plugin.jar \
/opt/jdk/jre/lib/ext/jfxrt.jar \
/opt/jdk/jre/bin/javaws \
/opt/jdk/jre/lib/javaws.jar \
/opt/jdk/jre/lib/desktop \
/opt/jdk/jre/plugin \
/opt/jdk/jre/lib/deploy* \
/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


#Download the jce
RUN cd /opt/jdk/jre/lib/security && curl -sL https://xxxxx.oss-cn-hangzhou.aliyuncs.com/jce_policy-8.zip > jce_policy-8.zip && \
unzip -j jce_policy-8.zip && \
wget http://xxxxx.oss-cn-hangzhou.aliyuncs.com/gdDumpAll -O /usr/bin/gdDumpAll && chmod +x /usr/bin/gdDumpAll && \
wget http://xxxxx.oss-cn-hangzhou.aliyuncs.com/gdDumpMem -O /usr/bin/gdDumpMem && chmod +x /usr/bin/gdDumpMem && \
wget http://xxxxx.oss-cn-hangzhou.aliyuncs.com/gdDumpJstack -O /usr/bin/gdDumpJstack && chmod +x /usr/bin/gdDumpJstack

RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/edas-plugins/edas.sar.${EDAS_CONTAINER_VERSION}/taobao-hsf.tgz -O /tmp/taobao-hsf.tgz && \
tar -xvf /tmp/taobao-hsf.tgz -C ${CATALINA_HOME}/deploy/ && \
rm -rf /tmp/taobao-hsf.tgz

ENV JAVA_HOME /opt/jdk
ENV PATH ${PATH}:${JAVA_HOME}/bin
ENV PATH=.:${JAVA_HOME}/bin:$PATH


CMD ${JAVA_HOME}/bin/java -jar $JVM_OPTS $CATALINA_OPTS $OPTS -Dfile.encoding=UTF-8 -Dspring.profiles.active=$PROFILE -Djava.security.egd=file:/dev/./urandom -Dcatalina.logs=$CATALINA_HOME/logs -Dpandora.location=$CATALINA_HOME/deploy/taobao-hsf.sar /home/admin/app/$APP_NAME.jar --server.context-path=/ --server.port=8080 --server.tomcat.uri-encoding=UTF-8 --server.tomcat.max-threads=400

-------------本文结束感谢您的阅读-------------