docker镜像优化

**方法****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
相关推荐
kkkkkkkkkk_Z17 分钟前
学嵌入式和Linux应用编程|学习日记:UDP协议与Wireshark抓包学习笔记
linux·笔记·学习
Allstar_4331 分钟前
全星研发项目管理APQP系统支持本地/私有云,开放API对接ERP/MES/PLM/OA,分级权限、版本追溯、ECN闭环,低代码配置,便于研发质量一体化扩展
运维
嵌入式小能手44 分钟前
飞凌嵌入式ElfBoard-Shell编程应用实例-提取字符并设置rtc时间
linux
刃神太酷啦1 小时前
Redis 核心进阶:哨兵、集群、缓存问题与分布式锁详解----《Hello Redis!》(6)
linux·c语言·数据库·c++·redis·分布式·缓存
大卡片1 小时前
LCD相关知识
linux
colourmind1 小时前
K3S+Hami+Higress+Vip(nginx+keepalived)搭建云原生高可用的大模型部署平台
运维·nginx·云原生
闲云野鹤在人间1 小时前
Docker入门|第3章 镜像详解
linux·网络·docker·容器·centos·云计算·php
小卿噢1 小时前
malloc 成功 ≠ 你有内存:一次把 OOM 从头测到尾
linux·后端
团子股股东峥哥1 小时前
day38-RHEL-管理存储堆栈
linux·运维·服务器
工业HMI实战笔记2 小时前
【拯救HMI】:自动化对外输出新范式:从设备出口到 “技术 + 服务” 全链条交付
运维·自动化