V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
JeffreyRSmith
V2EX  ›  Docker

求大佬们优化个 Dockerfile

  •  
  •   JeffreyRSmith · 363 天前 · 1382 次点击
    这是一个创建于 363 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这个构建出来的镜像有 2.11GB ,求大佬们给点优化建议,感谢!

    # 基于镜像基础
    FROM python:3.7.9-slim
    
    # 复制当前代码文件到容器中 /app
    ADD . /app1
    
    # 设置代码文件夹工作目录 /app
    WORKDIR /app1
    
    # build 镜像时运行的命令,安装所需的包
    RUN apt-get update && apt-get -f -y install \
        && apt-get install -yq gdebi \
        && apt-get install -y wget \ 
        && apt-get install -y libqt5core5a \
        && apt-get install -y gcc \
        && apt install -y make \
        && pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ \
        && mkdir -p /usr/share/man/man1/ \
        && TEMP_DEB="$(mktemp).deb" \
        && wget -O "$TEMP_DEB" 'https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.deb' \
        && apt install -yqf "$TEMP_DEB" \
        && rm -f "$TEMP_DEB" \
        #dpkg -i wkhtmltox_0.12.6-1.buster_amd64.deb
        && wget http://ftp.gnu.org/gnu/binutils/binutils-2.35.tar.gz \
        && tar -zxvf binutils-2.35.tar.gz \
        && cd binutils-2.35 \
        && ./configure \
        && make && make install \
        && strip --remove-section=.note.ABI-tag /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 \
        && apt install -y octave \
        && apt-get clean;
    
    
    6 条回复    2023-04-27 15:22:35 +08:00
    cdyrhh
        1
    cdyrhh  
       363 天前 via iPhone
    试试多阶段构建?
    T0m008
        2
    T0m008  
       363 天前
    安装时所需的包安装完都删掉啊
    Yourshell
        3
    Yourshell  
       363 天前
    有没有可能你的 requirements.txt 中确实需要安装这么多东西呢?
    piku
        4
    piku  
       363 天前 via Android
    qt 本身就很大。。。非要说的话可以尝试静态链接 make 后将 bin 移动到另一个干净的基础镜像中
    dongsoso
        5
    dongsoso  
       363 天前
    区分一下编译镜像和运行镜像
    https://docs.docker.com/build/building/multi-stage/
    night98
        6
    night98  
       363 天前
    # build 镜像时运行的命令,安装所需的包
    构建镜像安装的包,放在最前面,docker 会进行分层优化,最终编译命令单独拉出来放到代码 copy 后面

    可以尝试多阶段构建,代码 add 进去构建完之后把构建产物单独放到末尾重新构建一个新的镜像,可以有效减少镜像大小。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3218 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.