[英]Machine Learning Tools Docker Image Size Issue
我需要一個 docker 容器,上面安裝了以下軟件包以進行某種計算分析。 下面列出的包在 requirements.txt 文件中。
boto3 = "*"
nltk ="*"
pandas = "*"
scikit-learn = "*"
sentence_transformers = "*"
spacy = {extras = ["lookups"],version = "*"}
streamlit = "*"
tensorflow = "*"
unidecode = "*"
我已經為這個東西寫了一個 Dockerfile,我面臨的問題是 Docker 圖像的大小約為 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
您在該映像中安裝了很多東西,因此無論如何它都會變得很大,但是您可能可以做一些事情。
次要的 - 在通過 apt 安裝完這些東西后刪除/var/lib/apt/lists/*
。
RUN rm -rf /var/lib/apt/lists/*
主要的 - 從 Dockerfile 的內容來看,我猜它用於訓練需要訓練數據的 model,這可能會占用大量空間,因為您要將所有內容復制到圖像中。 這些數據不需要存在於鏡像中,而是需要加載到從鏡像構建的容器中。
與其將所有內容復制到映像中,不如復制僅在運行邏輯時需要但以其他方式加載數據的文件。 一種這樣的方法是將數據綁定到圖像中。 您可以將數據存儲在一個單獨的文件夾中,比如說./data
並將這個文件夾包含在您的.dockerignore
文件中(這樣它就不會被復制過來)。 然后,根據您啟動容器的方式,您可以指定綁定掛載,例如
docker container run -v ./data:/<path-inside-image> ...
將<path-inside-image>
替換為數據所在的路徑,但注意不要掛載到已經包含一些重要文件的目錄,因為這些文件會被掛載的文件夾遮住。
如果使用綁定掛載對您來說不是一個可行的解決方案,那么您將需要找到一種更好的方法將數據加載到容器中,例如,一旦容器運行,就從互聯網或其他網絡連接存儲中提取它們。
運行 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,只在安裝某些軟件時使用,安裝完成后我們可以將其刪除。
蛋:
RUN pip install --no-cache-dir -r requirements.txt
我不確定。從其他的Dockerfile,他們下載文件,最后在一次RUN
中使用后將其刪除,而不是在其中復制文件。
If you use tensorflow or other AI application,you may have some model data(size is a few G),better way is download it when run in container or by ftp,object storage,or others way —— not in image,just mount或下載。
就我的經驗而言。 如果您使用 git 來控制代碼。 .git
文件夾可能很大很大。 命令COPY. /XXX
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
。
# 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 緩存.
希望能幫到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.