首页 > 解决方案 > 避免在每个 Docker 构建上重新安装包

问题描述

当我构建需要 TensorFlow(导入 tensorflow)的新 python 应用程序的图像时,每次 docker 安装 520 MB 的 TensorFlow。

如何避免这种情况?意味着只下载一次 tensorflow 并在构建许多图像时使用它?

Dockerfile

FROM python:3

WORKDIR /usr/src/app

COPY model.py .
COPY model_08015_07680.h5 .
COPY requirements.txt .
COPY images .
COPY labels.txt .
COPY test_run.py .

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

CMD ["python","./test_run.py"]

要求.txt

numpy
opencv-python
tensorflow

标签: dockerdocker-build

解决方案


您不需要单独复制每个文件,这不是最佳选择。

另外,请记住 docker 是由层构建的,因此每条看起来可能会发生变化的行都位于底部。

FROM python:3

WORKDIR /usr/src/app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
#Copy everything
COPY . .    
CMD ["python","./test_run.py"]

推荐阅读