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
相关推荐
weixin_3077791316 分钟前
面向高性能保密计算的定制 Linux 系统构建与自动部署方案
linux·安全·网络安全·性能优化·系统安全
着迷不白18 分钟前
五、文本处理工具+正则表达式
linux·运维·服务器
Elastic 中国社区官方博客21 分钟前
每次操作一个 API 调用:Elastic Cloud Hosted 如何让大规模部署管理变得可行
大数据·运维·数据库·elasticsearch·搜索引擎·serverless
载数而行52027 分钟前
Linux 4常用指令(文件/时间/搜索查找/压缩解压指令)
linux
不做无法实现的梦~1 小时前
MAVLink 协议教程
linux·stm32·嵌入式硬件·算法
江华森1 小时前
Zabbix 6.4 全栈部署与运维完全指南
运维·zabbix
实心儿儿1 小时前
Linux —— 线程控制(2)
linux·运维·服务器
烛衔溟1 小时前
TypeScript 模块与声明文件全解
linux·ubuntu·typescript
量子炒饭大师2 小时前
【Linux系统编程:进程概念】——【从 冯诺依曼系统体系结构 到 操作系统】
linux·运维·服务器·操作系统·冯诺依曼
2023自学中2 小时前
imx6ull 开发板,手机,MQTT 物联网通信实验。
linux·服务器·物联网·嵌入式·开发板·应用编程