【问题标题】:Filesize of dockerimage doubles with different python version as baseimagedocker镜像的文件大小加倍,不同的python版本作为基础镜像
【发布时间】:2022-01-19 12:15:59
【问题描述】:

我用一些 apt 和 pip 包为 jupyterlab 构建 dockerimages。

docker image ls:
jupyterlab    3.7.12     9d4cc5c15853   27 minutes ago   2.3GB
jupyterlab    3.9.9      1962a1eb6bff   25 hours ago     1.12GB

图片的文件大小差别很大,我只拿了另一个python版本。

以下是用于比较的 dockerfile: 这个 dockerfile 是用 python 3.7.12 作为基础镜像构建的:

FROM python:3.7.12-slim-buster

WORKDIR /

COPY wheels/scipy-1.7.3-cp37-cp37m-linux_armv7l.whl ./wheels/scipy-1.7.3-cp37-cp37m-linux_armv7l.whl

## INSTALL WITH APK
RUN apt-get update && apt-get install -y \
    g++ \
    gcc \
    python3-dev \ 
    libjpeg-dev \
    zlib1g-dev \
    make \
    wget \
    libatlas-base-dev \
    libffi-dev 

## INSTALL WITH PIP
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir pillow && \
    pip install --no-cache-dir matplotlib && \
    pip install --no-cache-dir pandas && \
    pip install --no-cache-dir setuptools && \
    pip install --no-cache-dir cffi && \
    pip install --no-cache-dir GLIBC && \
    pip install --no-cache-dir numpy && \
    pip install --no-cache-dir /wheels/scipy-1.7.3-cp37-cp37m-linux_armv7l.whl && \
    pip install --no-cache-dir jupyterlab
    # pip install --no-cache-dir /wheels/numpy-1.21.4-cp39-cp39-linux_armv7l.whl && \
    # pip install --no-cache-dir /wheels/scipy-1.7.2-cp39-cp39-linux_armv7l.whl && \
    # pip install --no-cache-dir /wheels/jupyterlab-4.0.0a15-py3-none-any.whl 
    ...    

这是以python 3.9.9为基础镜像的那个:

FROM python:3.9.9-slim-buster

WORKDIR /

COPY wheels ./wheels

## INSTALL WITH APK
RUN apt-get update && apt-get install -y \
    g++ \
    gcc \
    python3-dev \ 
    libjpeg-dev \
    zlib1g-dev \
    make \
    wget \
    libatlas-base-dev \
    libffi-dev 

## INSTALL WITH PIP
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir pillow && \
    pip install --no-cache-dir matplotlib && \
    pip install --no-cache-dir pandas && \
    pip install --no-cache-dir setuptools && \
    pip install --no-cache-dir cffi && \
    pip install --no-cache-dir GLIBC && \
    pip install --no-cache-dir /wheels/numpy-1.21.4-cp39-cp39-linux_armv7l.whl && \
    pip install --no-cache-dir /wheels/scipy-1.7.2-cp39-cp39-linux_armv7l.whl && \
    pip install --no-cache-dir /wheels/jupyterlab-4.0.0a15-py3-none-any.whl 
    ...

wheels 文件夹和其他一些文件在这两种情况下都会被删除。 为什么文件大小差别这么大???

【问题讨论】:

  • 3.7.12 安装了一个未命名的 jupyterLab 版本,但 3.9.9 安装了一个命名版本(4.0.0),所以我怀疑这有什么区别?除此之外......安装的版本在两者中都不同如果您想知道大小差异的真正原因,那么您应该能够自己解决。从每个基础镜像开始,然后单独添加层(或包),看看每个层有什么不同。
  • 问题是,它在我的机器上构建了 2 个小时,所以尝试使用不同的 dockerfile 需要很长时间。如果不是通过下载的 whl 安装,我认为 Jupyterlab 不会多占用 1GB 的空间。
  • 对于每个 Dockerfile,您可以将每个 pip install 命令作为单独的 RUN 语句运行,以便它们各自获得一个层,然后比较层大小,这应该可以让您很好地了解差异来自哪里.在构建的图像上使用docker history 命令查看图层大小。
  • 我不想使用太多层,但对于测试这应该足够了。谢谢你的建议。

标签: python docker pip apt


【解决方案1】:

我通过删除“apt-get python3-dev”解决了这个问题,它是 3.7.2 版。我的baseimage是python 3.7.12所以可能有冲突,我无法解释。

图片大小:

#docker image ls

REPOSITORY      TAG            IMAGE ID        CREATED             SIZE
jupyterlab      3.7.12_nodev   6112bcaff54d    17 hours ago        1.18GB
jupyterlab      3.9.9          1962a1eb6bff    2 days ago          1.12GB

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多