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
相关推荐
浅时光_c1 天前
3 shell脚本编程
linux·开发语言·bash
Lucis__1 天前
一文读懂TCP通信机制:基于相关API构建可靠性连接
linux·网络·tcp/ip
_深海凉_1 天前
LeetCode热题100-有效的括号
linux·算法·leetcode
AI服务老曹1 天前
异构计算时代的安防底座:基于 Docker 的 X86/ARM 双模部署与 NPU 资源池化实战
arm开发·docker·容器
鹿鸣天涯1 天前
Xftp传输文件时,解决“无法显示远程文件夹”方法
运维·服务器·计算机
unDl IONA1 天前
服务器部署,用 nginx 部署后页面刷新 404 问题,宝塔面板修改(修改 nginx.conf 配置文件)
运维·服务器·nginx
零号全栈寒江独钓1 天前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
Web极客码1 天前
WordPress管理员角色详解及注意事项
运维·服务器·wordpress
左手厨刀右手茼蒿1 天前
Linux 内核中的进程管理:从创建到终止
linux·嵌入式·系统内核
geinvse_seg1 天前
中小团队如何低成本搭建项目管理系统?基于 Ubuntu 的 Dootask 私有化部署实战
linux·运维·ubuntu