# 1.Alpine Linux是一个轻型Linux发行版,它不同于通常的Linux发行版,Alpine采用了musl libc 和 BusyBox以减少系统的体积和运行时的资源消耗。
# 2.Alpine Linux提供了自己的包管理工具:apk(注意:ubuntu中是apt-get)
` https://pkgs.alpinelinux.org/packages 查询包信息
# 3.Alpine Docker镜像继承了Alpine Linux发行版的这些优势,相比于其他Linux Docker镜像,它的体积非常小 对比常用的、没有压缩过的基础镜像(查看当前的:latest标签):
Alpine - 4.8MB
centos - 124.8 MB
Debian - 125.1MB
Centos - 196MB
# 4.建议使用Alpine Linux 3.10.0版本,这也是 v3.10 稳定系列的首个版本
alpine:3.10
[root@test ~]# docker pull alpine
将提前下载好的安装包上传到服务器上
因为glibc安装包如果从网络下载速度实在是太慢了,可提前下载上传到服务器上
[root@test ~]# mkdir apline
[root@test ~]# cd apline/
[root@test apline]# vim Dockerfile
#1.指定基础镜像,并且必须是第一条指令
FROM alpine:latest
#FROM alpine:3.10
#2.指明该镜像的作者和其电子邮件
MAINTAINER cpc "[email protected]"
#3.在构建镜像时,指定镜像的工作目录,之后的命令都是基于此工作目录,如果不存在,则会创建目录
RUN mkdir -p /cpc_docker/jdk
WORKDIR /cpc_docker/jdk
#4.将一些安装包复制到镜像中,语法:ADD/COPY ...
## ADD与COPY的区别:ADD复制并解压,COPY仅复制
ADD jdk-8u211-linux-x64.tar.gz /cpc_docker/jdk/
## glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
COPY glibc-2.29-r0.apk /cpc_docker/jdk/
COPY glibc-bin-2.29-r0.apk /cpc_docker/jdk/
COPY glibc-i18n-2.29-r0.apk /cpc_docker/jdk/
#5.更新Alpine的软件源为阿里云,因为从默认官源拉取实在太慢了
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade
#6.运行指定的命令
## Alpine linux为了精简本身并没有安装太多的常用软件,apk类似于ubuntu的apt-get,
## 用来安装一些常用软V件,其语法如下:apk add bash wget curl git make vim docker
## wget是linux下的ftp/http传输工具,没安装会报错“/bin/sh: wget: not found”,网上例子少安装wget
## ca-certificates证书服务,是安装glibc前置依赖
RUN apk --no-cache add ca-certificates wget \
&& wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
&& rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk
#7.配置环境变量
ENV JAVA_HOME=/cpc_docker/jdk/jdk1.8.0_221
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
#容器启动时需要执行的命令
#CMD ["java","-version"]
[root@test apline]# ll
total 190428
-rw-r--r-- 1 root root 1981 Oct 10 2021 Dockerfile
-rw-r--r-- 1 root root 194990602 Oct 9 22:43 jdk-8u211-linux-x64.tar.gz
[root@test ~]# docker build -t jdk8:v1.0 .
[root@test ~]# docker create -it jdk8:v1.0
[root@test ~]# docker start 容器ID
注:最后是sh而非bash 也可直接 启动并进入容器 docker run -it --name myjdk 容器ID
[root@test ~]# docker exec -it 容器ID /bin/sh
注1:docker exec -it 容器ID /bin/sh 默认使用当前帐号(即root)登入,通过whoami命令可以查看当前用户名, 还可以通过如下命令切换到其它帐号docker exec -it --user root <容器ID> /bin/sh
注2:openjdk:8-jdk-alpine这个镜像就和我们自己创建的镜像是一样,只不过JDK瘦身过了,体积更小大家可以自己下来,创建一个容器试一下
注3:最后制作完成后的镜像大小为400M左右
[root@test ~]# java -version
Docker容器之最小JRE基础镜像
https://www.java.com/en/download/manual.jsp
# 1、解压
[root@test ~]# mkdir apline
[root@test ~]# cd apline/
[root@test apline]# ll -h
-rw-r--r-- 1 root root 186M Oct 9 22:43 jdk-8u211-linux-x64.tar.gz
[root@test apline]# tar -xf jre-8u221-linux-x64.tar.gz
[root@test apline]# ll -h
drwxr-xr-x 7 10 143 245 Apr 2 2019 jdk1.8.0_211
-rw-r--r-- 1 root root 186M Oct 9 22:43 jdk-8u211-linux-x64.tar.gz
# 2、查看jre大小(瘦身前230M)
[root@test apline]# du -sh jdk1.8.0_211/
392M jdk1.8.0_211/
# 3、进入jre目录,并执行瘦身命令
[root@test apline]# cd jre1.8.0_221
执行瘦身命令,这个命里太长单独贴出来(直接在 jre1.8.0_221 这个目录下 copy 命里回车运行就好了)
[root@test jdk1.8.0_211]# ls -l
total 25984
drwxr-xr-x 2 10 143 4096 Apr 2 2019 bin
-r--r--r-- 1 10 143 3244 Apr 2 2019 COPYRIGHT
drwxr-xr-x 3 10 143 132 Apr 2 2019 include
-rw-r--r-- 1 10 143 5213268 Mar 14 2019 javafx-src.zip
drwxr-xr-x 5 10 143 185 Apr 2 2019 jre
drwxr-xr-x 5 10 143 245 Apr 2 2019 lib
-r--r--r-- 1 10 143 44 Apr 2 2019 LICENSE
drwxr-xr-x 4 10 143 47 Apr 2 2019 man
-r--r--r-- 1 10 143 159 Apr 2 2019 README.html
-rw-r--r-- 1 10 143 424 Apr 2 2019 release
-rw-r--r-- 1 10 143 21105019 Apr 2 2019 src.zip
-rw-r--r-- 1 10 143 112748 Mar 14 2019 THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r-- 1 10 143 149725 Apr 2 2019 THIRDPARTYLICENSEREADME.tx
# 1、删除文本文件
[root@test jdk1.8.0_211]# rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txtTHIRDPARTYLICENSEREADME.txt Welcome.html \
# 2、删除其他无用文件
[root@test jdk1.8.0_211]# rm -rf lib/plugin.jar \
lib/ext/jfxrt.jar \
bin/javaws \
lib/javaws.jar \
lib/desktop \
plugin \
lib/deploy* \
lib/*javafx* \
lib/*jfx* \
lib/amd64/libdecora_sse.so \
lib/amd64/libprism_*.so \
lib/amd64/libfxplugins.so \
lib/amd64/libglass.so \
lib/amd64/libgstreamer-lite.so \
lib/amd64/libjavafx*.so \
lib/amd64/libjfx*.so
返回上级目录,重新打包jre
[root@test jdk1.8.0_211]# tar czf jre1.tar.gz ./
[root@test jdk1.8.0_211]# mv jre1.tar.gz ../
FROM alpine:latest
#FROM alpine:3.10
#2.指明该镜像的作者和其电子邮件
MAINTAINER cpc '[email protected]"
#3.在构建镜像时,指定镜像的工作目录,之后的命令都是基于此工作目录,如果不存在,则会创建目录
WORKDIR /cpc_docker/jdk
#4.将一些安装包复制到镜像中,语法:ADD/COPY ...
## ADD与COPY的区别:ADD复制并解压,COPY仅复制
## 注意~~~上传的瘦身后的jre
ADD jre1.tar.gz /cpc_docker/jdk/
## glibc安装包如果从网络下载速度实在是太慢了,先提前下载复制到镜像中
COPY glibc-2.29-r0.apk /cpc_docker/jdk/
COPY glibc-bin-2.29-r0.apk /cpc_docker/jdk/
COPY glibc-i18n-2.29-r0.apk /cpc_docker/jdk/
#5.更新Alpine的软件源为阿里云,因为从默认官源拉取实在太慢了
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade
#6.运行指定的命令
## Alpine linux为了精简本身并没有安装太多的常用软件,apk类似于ubuntu的apt-get,
## 用来安装一些常用软V件,其语法如下:apk add bash wget curl git make vim docker
## wget是linux下的ftp/http传输工具,没安装会报错“/bin/sh: wget: not found”,网上例子少安装wget
## ca-certificates证书服务,是安装glibc前置依赖
RUN apk --no-cache add ca-certificates wget \
&& wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub \
&& apk add glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk \
&& rm -rf /var/cache/apk/* glibc-2.29-r0.apk glibc-bin-2.29-r0.apk glibc-i18n-2.29-r0.apk
#7.配置环境变量
## 注意~~~没有jdk啦,直接指向jre
ENV JAVA_HOME=/cpc_docker/jdk/jre1.8.0_221
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH
#容器启动时需要执行的命令
#CMD ["java","-version"]
[root@test apline]# docker build -t jdk8:v2.0 .
[root@test apline]# docker create -it jdk8:v2.0
[root@test apline]# docker start 容器ID
[root@test apline]# docker exec -it 容器ID /bin/sh
[root@test apline]# java -version
注1:openjdk:8-jdk-alpine这个镜像就和我们自己创建的镜像是一样,只不过JDK瘦身过了,体积更小。另外,openjdk和jdk也是有一定区别的,详情请点我
注2:Alpine制作JDK8镜像是有时区问题的,可以使用时区查看命令看一下
三个版本的jdk镜像结果如下:
V0.0:centos7+jdk1.8制作而成
V1.0:alpine3.10+jdk1.8制作而成
V2.0:alpine3.10+jre1.8+瘦身命令制作而成
小巧:基于Musl libc和busybox,和busybox一样小巧,最小的Docker镜像只有5MB;
安全:面向安全的轻量发行版;
简单:提供APK包管理工具,软件的搜索、安装、删除、升级都非常方便。
适合容器使用:由于小巧、功能完备,非常适合作为容器的基础镜像
[root@test ~]# docker pull alpine
[root@test ~]# docker run -it --name myalpine alpine
以Alpine为基础镜像,创建一个MySQL容器
FROM alpine:3.6
RUN apk add --no-cache mysql-client
ENTRYPOINT ["mysql"]
[root@test ~]# /etc/network/interfaces
[root@test ~]# /etc/init.d/networking restart #重启网络
清华大学:https://mirror.tuna.tsinghua.edu.cn/alpine/
阿里云:https://mirrors.aliyun.com/alpine/3.
中科大:http://mirrors.ustc.edu.cn/alpine/
将 /etc/apk/repositories 文件中 Alpine 默认的源地址 http://dl-cdn.alpinelinux.org/ 替换为 http://mirrors.ustc.edu.cn/ 即可
[root@test ~]# sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
直接编辑 /etc/apk/repositories 文件:
https://mirrors.ustc.edu.cn/alpine/v3.5/main
https://mirrors.ustc.edu.cn/alpine/v3.5/community
使用 latest-stable 指向最新的稳定版本:
https://mirrors.ustc.edu.cn/alpine/latest-stable/main
https://mirrors.ustc.edu.cn/alpine/latest-stable/community
最后:
apk update
echo http://mirrors.ustc.edu.cn/alpine/v3.7/main >/etc/apk/repositories
echo http://mirrors.ustc.edu.cn/alpine/v3.7/community >>/etc/apk/repositories
echo http://mirrors.ustc.edu.cn/alpine/edge/main >>/etc/apk/repositories
echo http://mirrors.ustc.edu.cn/alpine/edge/community >>/etc/apk/repositories
echo http://mirrors.ustc.edu.cn/alpine/edge/testing >>/etc/apk/repositories
或者:
echo 'http://mirrors.ustc.edu.cn/alpine/v3.7/main
http://mirrors.ustc.edu.cn/alpine/v3.7/community
http://mirrors.ustc.edu.cn/alpine/edge/main
http://mirrors.ustc.edu.cn/alpine/edge/community
http://mirrors.ustc.edu.cn/alpine/edge/testing' >/etc/apk/repositories
Alpine使用apk进行包管理
[root@test ~]# apk update #更新最新镜像源列表
apk search #查找所以可用软件包
apk search -v #查找所以可用软件包及其描述内容
apk search -v 'acf*' #通过软件包名称查找软件包
apk search -v -d 'docker' #通过描述文件查找特定的软件包
apk add openssh #安装一个软件
apk add openssh openntp vim #安装多个软件
apk add --no-cache mysql-client #不使用本地镜像源缓存,相当于先执行update,再执行add
apk add -u #升级指定软件包
apk info #列出所有已安装的软件包
apk info -a zlib #显示完整的软件包信息
apk info -r zlib #列出所有包的依赖
apk info -s zlib #显示软件包大小
apk info -d zlib #显示软件包的描述
apk info --who-owns /sbin/lbu #显示指定文件属于的包
apk info -L zlib #列出软件包的内容
apk info -e zlib #检测软件包的安装
apk info -R zlib #列出指定包的依赖
apk upgrade #升级所有软件
apk upgrade openssh #升级指定软件
apk upgrade openssh openntp vim #升级多个软件
apk add --upgrade busybox #指定升级部分软件包
apk del openssh #删除一个软件
使用的是RC系列命令进行服务管理.
用于不同运行级增加或者删除服务
rc-update --help
Usage: rc-update [options] add [...]
or: rc-update [options] del [...]
or: rc-update [options] [show [...]]
Options: [ asuChqVv ]
-a, --all Process all runlevels
-s, --stack Stack a runlevel instead of a service
-u, --update Force an update of the dependency tree
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
用于运行级的状态管理
rc-status --help
Usage: rc-status [options] ...
or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]
Options: [ aclmrsuChqVv ]
-a, --all Show services from all run levels
-c, --crashed Show crashed services
-l, --list Show list of run levels
-m, --manual Show manually started services
-r, --runlevel Show the name of the current runlevel
-s, --servicelist Show service list
-u, --unused Show services not assigned to any runlevel
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
用于管理服务的状态
rc-service --help
Usage: rc-service [options] [-i] ...
or: rc-service [options] -e
or: rc-service [options] -l
or: rc-service [options] -r
Options: [ ce:ilr:INChqVv ]
-e, --exists tests if the service exists or not
-c, --ifcrashed if the service is crashed then run the command
- -i, --ifexists if the service exists then run the command
-I, --ifinactive if the service is inactive then run the command
-N, --ifnotstarted if the service is not started then run the command
-l, --list list all available services
-r, --resolve resolve the service name to an init script
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
管理不同的运行级
openrc --help
Usage: openrc [options] []
Options: [ a:no:s:SChqVv ]
-n, --no-stop do not stop any services
-o, --override override the next runlevel to change into
when leaving single user or boot runlevels
-s, --service runs the service specified with the rest
of the arguments
-S, --sys output the RC system type, if any
-h, --help Display this help output
-C, --nocolor Disable color output
-V, --version Display software version
-v, --verbose Run verbosely
-q, --quiet Run quietly (repeat to suppress errors)
rc-update add docker boot #增加docker服务到系统启动时运行
rc-service networking restart #重启网络服务
rc-status -a #列出所有服务
apk add iproute2 #查看网络连接的协议端口的命令ss,可以替代netstat命令
# ss -ta ##查看建立的TCP连接
apk add drill #替代dig和nslookup DNS查询命令
# drill blog.csdn.net @8.8.8.8 ##查询域名的信息
# drill -x 114.114.114.114 @10.61.191.221 ##根据IP地址,反向查找域名
apk add openssh-server #开启SSH服务
/etc/init.d/sshd restart
rc-update add sshd
http://wiki.alpinelinux.org
# AlpineLinux with a glibc-2.29-r0 and Oracle Java 8
FROM alpine:latest
MAINTAINER Anastas Dancha
# thanks to Vladimir Krivosheev aka @develar for smaller image
# and Victor Palma aka @devx for pointing it out
# Java Version and other ENV
ENV JAVA_VERSION_MAJOR=8 \
JAVA_VERSION_MINOR=202 \
JAVA_VERSION_BUILD=08 \
JAVA_PACKAGE=jdk \
JAVA_JCE=standard \
JAVA_HOME=/opt/jdk \
PATH=${PATH}:/opt/jdk/bin \
GLIBC_REPO=https://github.com/sgerrand/alpine-pkg-glibc \
GLIBC_VERSION=2.29-r0 \
LANG=C.UTF-8
# do all in one step
RUN set -ex && \
[[ ${JAVA_VERSION_MAJOR} != 7 ]] || ( echo >&2 'Oracle no longer publishes JAVA7 packages' && exit 1 ) && \
apk -U upgrade && \
apk add tzdata libstdc++ curl ca-certificates bash && \
cp -r -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
for pkg in glibc-${GLIBC_VERSION} glibc-bin-${GLIBC_VERSION} glibc-i18n-${GLIBC_VERSION}; do curl -sSL ${GLIBC_REPO}/releases/download/${GLIBC_VERSION}/${pkg}.apk -o /tmp/${pkg}.apk; done && \
apk add /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}/1961070e4c9b4e26a04e7f5a083f551e/${JAVA_PACKAGE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-linux-x64.tar.gz && \
JAVA_PACKAGE_SHA256=$(curl -sSL https://www.oracle.com/webfolder/s/digest/${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}checksum.html | grep -E "${JAVA_PACKAGE}-${JAVA_VERSION_MAJOR}u${JAVA_VERSION_MINOR}-linux-x64\.tar\.gz" | grep -Eo '(sha256: )[^<]+' | cut -d: -f2 | xargs) && \
echo "${JAVA_PACKAGE_SHA256} /tmp/java.tar.gz" > /tmp/java.tar.gz.sha256 && \
sha256sum -c /tmp/java.tar.gz.sha256 && \
gunzip /tmp/java.tar.gz && \
tar -C /opt -xf /tmp/java.tar && \
ln -s /opt/jdk1.${JAVA_VERSION_MAJOR}.0_${JAVA_VERSION_MINOR} /opt/jdk && \
if [ "${JAVA_JCE}" == "unlimited" ]; then echo "Installing Unlimited JCE policy" >&2 && \
curl -jksSLH "Cookie: oraclelicense=accept-securebackup-cookie" -o /tmp/jce_policy-${JAVA_VERSION_MAJOR}.zip \
http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION_MAJOR}/jce_policy-${JAVA_VERSION_MAJOR}.zip && \
cd /tmp && unzip /tmp/jce_policy-${JAVA_VERSION_MAJOR}.zip && \
cp -v /tmp/UnlimitedJCEPolicyJDK8/*.jar /opt/jdk/jre/lib/security; \
fi && \
sed -i s/#networkaddress.cache.ttl=-1/networkaddress.cache.ttl=10/ $JAVA_HOME/jre/lib/security/java.security && \
apk del curl glibc-i18n && \
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/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
[root@test ~]# docker build -t hub.linuxeye.com/library/alpine-java:latest . #进入行dockerfile目录