deployment - 如何减少 docker image 安装依赖项所需的时间?

标签 deployment build dependencies docker

我有一些节点 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 容器必须能够以某种方式重建另一个容器?

最佳答案

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

就像是:

基础/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 容器中,这样当您对主机上的代码进行更改时,它们将立即反射(reflect)在容器中:

本地/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 .

关于deployment - 如何减少 docker image 安装依赖项所需的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377270/

相关文章:

java.lang.NoClassDefFoundError : org. apache.commons.lang3.StringUtils

scala - "unresolved dependency"用于 SBT 上的 Spark 2.0.1

iOS 代码签名 FaSTLane 匹配错误

maven - 如何使用uploadArchives推送到本地和远程Maven存储库?

deployment - 项目已发布,但未在AppStore中显示更新的版本

java - 将所有依赖的jar文件存储在目录中

android - 在 Windows 上使用 Ant for Android 将新构建文件输出到新版本号文件夹

angular - Tomcat 重写规则 Angular 5 应用程序?

java - 如何使用Gradle配置JCompilo

delphi - 如何更改依赖项的文件扩展名