【问题标题】:How to reduce time required by docker image to install dependencies?如何减少 docker image 安装依赖项所需的时间?
【发布时间】:2014-08-19 16:57:08
【问题描述】:

我有一些节点 docker-containers 基本上看起来像:

# core nodejs just installs node and git on archlinux
FROM core/nodejs

# clones directory into current working dir
RUN git clone https://github.com/bodokaiser/nearby .

# installs all dependencies
RUN npm install

# lets node execute the source code
CMD ["node", "index.js"]

当我现在重建映像以收集新更新时,它会从 npm 下载所有依赖项。这总是需要大约 5 分钟。

我现在想知道如何避免重新安装所有依赖项。

到目前为止,我的一个想法是使用VOLUME,然后与主机共享代码存储库,这会使其他主机上的图像难以使用。

更新: 我的另一个想法是创建一个包含 git repo 并与运行时容器共享的卷容器。但是 repo 容器必须能够以某种方式重建另一个容器?

【问题讨论】:

  • 为什么不设置一个本地 docker 存储库并将您构建的映像推送到那里?
  • 这是什么意思?
  • 可以运行您自己的 docker 注册表,这是一种本地服务,其运行方式类似于 docker hub 提供的服务。这将是构建和推送图像的最佳位置,然后它们只需要在运行时下载。见 (github.com/docker/docker-registry)
  • 这意味着我在我的开发机器上本地构建了一个 docker 文件并将其推送到注册表。然后我可以从注册表中提取它。注册表不会占用很多资源吗?我想把所有东西都放在一台小机器上。
  • 注册表可以像在您的机器上运行的另一个容器一样简单。当然,我在想你有几台机器可以使用。如果您只有一台开发机器,请按照以下 Chris 的建议创建基础映像。如果您必须为每张图片下载,这将减少数量。

标签: deployment build dependencies docker


【解决方案1】:

听起来您所追求的是拥有一个构建依赖项的基础映像和一个扩展它的本地映像,以便您可以快速构建/运行。

类似:

基础/Dockerfile

#core nodejs just installs node and git on archlinux
FROM core/nodejs

# installs all dependencies
RUN npm install

然后你可以做一个:

cd base
docker build -t your-image-name-base:your-tag .

本地/Dockerfile

FROM your-image-name-base:your-tag

# clones directory into current working dir
RUN git clone https://github.com/bodokaiser/nearby .

# lets node execute the source code
CMD ["node", "index.js"]

然后构建你的本地镜像:

cd local
docker build -t your-image-name-local:your-tag .

然后像这样运行它:

docker run your-image-name-local:your-tag

现在您的本地映像将非常快速地构建,因为它扩展了您的基础映像,它已经完成了所有繁重的依赖安装和提升。

作为在容器中执行 git clone 的替代方法,您可以将代码目录挂载到 docker 容器中,这样当您对主机上的代码进行更改时,它们会立即反映在容器中:

本地/Dockerfile

FROM your-image-name-base:your-tag

# lets node execute the source code
CMD ["node", "index.js"]

然后你会运行:

docker run -v /path/to/your/code:/path/inside/container your-image-name-local:your-tag

这会将目录挂载到你的容器中,然后执行你的CMD

【讨论】:

  • 我还发现了另一种方法,这可能是对您的一个很好的补充:在 nodejs 容器中,我们可以将 node_modules 导出为安装在数据容器中的卷。此数据容器可用于所有节点构建,这将大大减少下载时间。
猜你喜欢
  • 1970-01-01
  • 2020-05-13
  • 2020-02-19
  • 2019-07-09
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多