V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
JeffreyRSmith
V2EX  ›  Docker

求大佬们优化个 Dockerfile

  •  
  •   JeffreyRSmith · Apr 27, 2023 · 2338 views
    This topic created in 1097 days ago, the information mentioned may be changed or developed.

    这个构建出来的镜像有 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 replies    2023-04-27 15:22:35 +08:00
    cdyrhh
        1
    cdyrhh  
       Apr 27, 2023 via iPhone
    试试多阶段构建?
    T0m008
        2
    T0m008  
       Apr 27, 2023
    安装时所需的包安装完都删掉啊
    Yourshell
        3
    Yourshell  
       Apr 27, 2023
    有没有可能你的 requirements.txt 中确实需要安装这么多东西呢?
    piku
        4
    piku  
       Apr 27, 2023 via Android
    qt 本身就很大。。。非要说的话可以尝试静态链接 make 后将 bin 移动到另一个干净的基础镜像中
    dongsoso
        5
    dongsoso  
       Apr 27, 2023
    区分一下编译镜像和运行镜像
    https://docs.docker.com/build/building/multi-stage/
    night98
        6
    night98  
       Apr 27, 2023
    # build 镜像时运行的命令,安装所需的包
    构建镜像安装的包,放在最前面,docker 会进行分层优化,最终编译命令单独拉出来放到代码 copy 后面

    可以尝试多阶段构建,代码 add 进去构建完之后把构建产物单独放到末尾重新构建一个新的镜像,可以有效减少镜像大小。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1155 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 23:13 · PVG 07:13 · LAX 16:13 · JFK 19:13
    ♥ Do have faith in what you're doing.