**方法****1.**缩减镜像层
下载nginx安装包
cpp
[root@docker1 ~]# mkdir docker
[root@docker1 ~]# cd docker/
[root@docker1 ~]# wget http://nginx.org/download/nginx-1.23.4.tar.gz
编写Dockerfile文件
cpp
[root@docker1 docker]# vim Dockerfile
FROM centos-7:repo
LABEL Creater=xu
ADD nginx-1.23.4.tar.gz /root
RUN yum install gcc make pcre-devel openssl-devel -y
WORKDIR /root/nginx-1.23.4
RUN ./configure --with-http_ssl_module --with-http_stub_status_module
RUN make
RUN make install
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
构建镜像并查看镜像内存
此时webservers是507MB
cpp
[root@docker1 docker]# docker build -t webservers:v1 .
[root@docker1 docker]# docker images
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
webservers:v1 eb995ce40600 507MB 132MB U
开始缩减镜像层
编写Dockerfile文件
cpp
[root@docker1 docker]# vim Dockerfile
FROM centos-7:repo
LABEL Creater=lee
ADD nginx-1.23.4.tar.gz /root
WORKDIR /root/nginx-1.23.4
RUN yum install gcc make pcre-devel openssl-devel -y && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && rm -fr /root/nginx-1.23.4 && yum clean all
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
构建镜像并查看镜像内存
此时镜像缩减了内存
cpp
[root@docker1 docker]# docker build -t webservers:v2 .
[root@docker1 docker]# docker images
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
webservers:v1 eb995ce40600 507MB 132MB U
webservers:v2 70f821210f3f 425MB 109MB
**方法****2.**多阶段构建
cpp
[root@docker1 docker]# docker build -t webservers:v3 .
FROM centos-7:repo AS xu
LABEL Creater=xu
# 这里改成了 nginx-1.23.4.tar.gz
ADD nginx-1.23.4.tar.gz /root
WORKDIR /root/nginx-1.23.4
RUN yum install gcc make pcre-devel openssl-devel -y && ./configure --with-http_ssl_module --with-http_stub_status_module && make && make install && rm -fr /root/nginx-1.23.4 && yum clean all
FROM centos-7:repo
COPY --from=xu /usr/local/nginx /usr/local/nginx
EXPOSE 80
VOLUME ["/usr/local/nginx/html"]
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
查看镜像内存
cpp
[root@docker1 docker]# docker images
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
webservers:v2 70f821210f3f 425MB 109MB
webservers:v3 3eea800fee90 308MB 79MB #此时镜像内存再一次减小
**方法****3.**使用最精简镜像
使用google提供的最精简镜像
**下载地址:**https://github.com/GoogleContainerTools/distroless
https://nginx.org/download/nginx-1.23.4.tar.gz
**下载镜像:**docker pull gcr.io/distroless/base
效果展示,下载完之后记得引入镜像

cpp
[root@docker1 ~]# docker load -i nginx-1.23.tar.gz
[root@docker1 ~]# docker load -i debian11.tar.gz
cpp
[root@docker1 docker]# docker images
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
nginx:1.23 a087ed751769 301MB 147MB
编写Dockerfile文件
cpp
[root@docker1 docker]# vim Dockerfile
FROM nginx:1.23 AS xu
ARG TIME_ZONE
RUN mkdir -p /opt/var/cache/nginx && \
cp -a --parents /usr/lib/nginx /opt && \
cp -a --parents /usr/share/nginx /opt && \
cp -a --parents /var/log/nginx /opt && \
cp -aL --parents /var/run /opt && \
cp -a --parents /etc/nginx /opt && \
cp -a --parents /etc/passwd /opt && \
cp -a --parents /etc/group /opt && \
cp -a --parents /usr/sbin/nginx /opt && \
cp -a --parents /usr/sbin/nginx-debug /opt && \
cp -a --parents /lib/x86_64-linux-gnu/ld-* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libpcre* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libz.so.* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libc* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libdl* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libpthread* /opt && \
cp -a --parents /lib/x86_64-linux-gnu/libcrypt* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libssl.so.* /opt && \
cp -a --parents /usr/lib/x86_64-linux-gnu/libcrypto.so.* /opt && \
cp /usr/share/zoneinfo/${TIME_ZONE:-ROC} /opt/etc/localtime
FROM gcr.io/distroless/base-debian11
COPY --from=xu /opt /
EXPOSE 80 443
ENTRYPOINT ["nginx", "-g", "daemon off;"]
构建镜像并查看镜像内存
cpp
[root@docker1 docker]# docker build -t webservers:v4 .
[root@docker1 docker]# docker images
i Info → U In Use
IMAGE ID DISK USAGE CONTENT SIZE EXTRA
nginx:1.23 a087ed751769 301MB 147MB
webservers:v4 1cbeee625c56 67.9MB 28.4MB