减小docker镜像体积的方法
原始版本
dockerfile
FROM centos:centos7
# 配置resove.conf解决软件包获取不到的问题
RUN curl -O http://mirrors.aliyun.com/repo/Centos-7.repo
RUN mv -f Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum makecache
# 安装openssh-server和sudo软件包,并且将sshd的UsePAM参数设置成no
RUN yum install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
#安装openssh-clients
RUN yum install -y openssh-clients
# 添加测试用户root,密码3238,并且将此用户添加到sudoers里
RUN echo "root:3238" | chpasswd
RUN echo "root ALL=(ALL) ALL" >> /etc/sudoers
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# 启动sshd服务并且暴露22端口
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
ADD jdk-8u202-linux-x64.tar.gz /usr/local
RUN mv /usr/local/jdk1.8.0_202 /usr/local/jdk1.8
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
ADD hadoop-3.1.3.tar.gz /usr/local
RUN mv /usr/local/hadoop-3.1.3 /usr/local/hadoop
ENV HADOOP_HOME /usr/local/hadoop
ENV PATH $HADOOP_HOME/bin:$PATH
镜像大小:3.75G
减小层数
docker的构建中,Dockerfile中一个RUN会在镜像上构建一层,因此需要将多个RUN合并执行,减少镜像层数。
由于上一层的文件会留到下一层,还可以把一层的无用数据清除掉,例如使用yum clean all
。
dockerfile
FROM centos:centos7
RUN curl -O http://mirrors.aliyun.com/repo/Centos-7.repo && \
mv -f Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo && \
yum makecache && \
yum install -y openssh-server sudo && \
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && \
yum install -y openssh-clients && \
echo "root:3238" | chpasswd && \
echo "root ALL=(ALL) ALL" >> /etc/sudoers && \
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
mkdir /var/run/sshd && \
yum clean all && \
rm -rf /var/cache/yum/*
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
FROM centos7-ssh
ADD jdk-8u202-linux-x64.tar.gz /usr/local
RUN mv /usr/local/jdk1.8.0_202 /usr/local/jdk1.8
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
ADD hadoop-3.1.3.tar.gz /usr/local
RUN mv /usr/local/hadoop-3.1.3 /usr/local/hadoop
ENV HADOOP_HOME /usr/local/hadoop
ENV PATH $HADOOP_HOME/bin:$PATH
现在大小变为了:2.73G
多阶段构建
在此方法中,我们使用中间镜像(构建阶段)来编译代码、安装依赖项和打包文件。这样操作是消除镜像中不需要的图层。
例如下面第二步骤的构建hadoop
镜像的过程中,之前使用的构建命令有8句,然后现在变为了6句,相当于少了两层,这样就能使最终产生的镜像更加轻量化了。
第一步:首先构建centos7-ssh
dockerfile
FROM centos:centos7
RUN curl -O http://mirrors.aliyun.com/repo/Centos-7.repo && \
mv -f Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo && \
yum makecache && \
yum install -y openssh-server sudo && \
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config && \
yum install -y openssh-clients && \
echo "root:3238" | chpasswd && \
echo "root ALL=(ALL) ALL" >> /etc/sudoers && \
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
mkdir /var/run/sshd && \
yum clean all && \
rm -rf /var/cache/yum/*
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
第二步:然后以centos7-ssh
为基础使用多阶段方法来构建hadoop
dockerfile
FROM centos7-ssh AS base
ADD jdk-8u202-linux-x64.tar.gz /usr/local
RUN mv /usr/local/jdk1.8.0_202 /usr/local/jdk1.8
ADD hadoop-3.1.3.tar.gz /usr/local
RUN mv /usr/local/hadoop-3.1.3 /usr/local/hadoop
FROM centos7-ssh
COPY --from=base /usr/local/jdk1.8 /usr/local/
COPY --from=base /usr/local/hadoop /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
ENV HADOOP_HOME /usr/local/hadoop
ENV PATH $HADOOP_HOME/bin:$PATH
最终大小:1.48G