gpt4 book ai didi

python - 机器学习工具 Docker 镜像大小问题

转载 作者:行者123 更新时间:2023-12-02 18:09:52 24 4
gpt4 key购买 nike

我需要一个安装了以下软件包的 docker 容器来进行某种计算分析。下面列出的包在 requirements.txt 文件中。

boto3 = "*"
nltk ="*"
pandas = "*"
scikit-learn = "*"
sentence_transformers = "*"
spacy = {extras = ["lookups"],version = "*"}
streamlit = "*"
tensorflow = "*"
unidecode = "*"

我已经为这个东西写了一个 Dockerfile,我现在面临的问题是 Docker Image 的大小大约是 6 GB(正好是 6.42)。谁能帮我解决这个问题,如何减小 Docker 镜像的大小。

这是DockerFile

FROM python:3.7-slim-buster as base

ENV PYTHONUNBUFFERED=TRUE
ENV PYTHONDONTWRITEBYTECODE=TRUE
ENV PATH="/opt/program:${PATH}"

COPY . /opt/program

WORKDIR /opt/program/

RUN chmod +x train

# Install dependencies
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get autoremove -y \
&& apt-get install -y \
gcc \
build-essential \
zlib1g-dev \
wget \
unzip \
cmake \
python3-dev \
gfortran \
libblas-dev \
liblapack-dev \
libatlas-base-dev \
&& apt-get clean

# Install Python packages
RUN pip install --upgrade pip \
&& pip install \
ipython[all] \
nose \
matplotlib \
pandas \
scipy \
sympy \
&& rm -fr /root/.cache

RUN pip install --install-option="--prefix=/install" -r requirements.txt

最佳答案

从别人的Dockerfile,或者文档中获取一些方法:

  • 删除apt缓存

在运行 apt-install 之后执行 rm -rf/var/lib/apt/lists/* ,例如

RUN apt-get update && apt-get install -y \
ca-certificates \
netbase \
&& rm -rf /var/lib/apt/lists/*

不是:

RUN apt-get update && apt-get install -y \
ca-certificates \
netbase
RUN rm -rf /var/lib/apt/lists/*
  • 不安装推荐
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates \
netbase \
&& rm -rf /var/lib/apt/lists/*

no-install-recommends 表示:不安装非必需的依赖包。

  • 删除中间软件

鸡蛋:

RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
g++ \
&& pip install cython && apt-get remove -y gcc g++ \
&& rm -rf /var/lib/apt/lists/*

有些软件,比如gcc,只在安装某些软件时使用,安装完成后我们可以删除它。

  • pip 不使用缓存

鸡蛋:


RUN pip install --no-cache-dir -r requirements.txt

  • 下载和删除比复制更好?

我不确定。他们从其他人的 Dockerfile 下载文件,并在一次 RUN 中使用后最终将其删除,而不是将文件复制到其中。

  • 不将模型数据 docker 到图像中。

如果你使用tensorflow或其他人工智能应用程序,你可能有一些模型数据(大小为几G),更好的方法是在容器中运行时下载它,或者通过ftp、对象存储或其他方式下载它——而不是在图像中, 只需挂载或下载即可。

  • 注意 .git 文件夹

根据我的经验。如果你使用 git 来控制代码。 .git 文件夹可能非常非常大。命令 COPY 。/XXX 会将 .git 复制到图像中。找到一种方法来过滤 .git。对于我的使用:


FROM apline:3.12 as MID
COPY XXX /XXX/
COPY ... /XXX/

FROM image:youneed
COPY --from=MID /XXX/ /XXX/
RUN apt-get update && xxxxx

CMD ["python","app.py"]

或使用.dockerignore

从上面获取:

在你的 Dockerfile 中

# Did wget,cmake and some on  is necessary?

COPY . /opt/program

WORKDIR /opt/program/

# Install dependencies
RUN chmod +x train && apt-get update \
&& apt-get upgrade -y \
&& apt-get autoremove -y \
&& apt-get install -y \
gcc \
build-essential \
zlib1g-dev \
wget \
unzip \
cmake \
python3-dev \
gfortran \
libblas-dev \
liblapack-dev \
libatlas-base-dev \
&& apt-get clean && pip install --upgrade pip \
&& pip install --no-cache-dir \
ipython[all] \
nose \
matplotlib \
pandas \
scipy \
sympy \
&& pip install --no-cache-dir --install-option="--prefix=/install" -r requirements.txt
&& apt-get remove -y gcc unzip cmake \ # just have a try,to find what software we can remove.
&& rm -rf /var/lib/apt/lists/*
&& rm -fr /root/.cache

当然,通过这种方式,你可能会得到一个更小尺寸的镜像,但是docker构建过程中,将不会使用docker的缓存。所以在你尝试寻找可以删除的软件时,拆分为两个或三个命令 RUN 以使用更多 docker 缓存。

希望能帮到你。

关于python - 机器学习工具 Docker 镜像大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63105274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com