bod*_*ser 0 deployment dependencies build 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"]
Run Code Online (Sandbox Code Playgroud)
当我现在重建映像以收集新更新时,它会从 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
Run Code Online (Sandbox Code Playgroud)
然后你可以做一个:
cd base
docker build -t your-image-name-base:your-tag .
Run Code Online (Sandbox Code Playgroud)
本地/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"]
Run Code Online (Sandbox Code Playgroud)
然后构建您的本地映像:
cd local
docker build -t your-image-name-local:your-tag .
Run Code Online (Sandbox Code Playgroud)
并像这样运行它:
docker run your-image-name-local:your-tag
Run Code Online (Sandbox Code Playgroud)
现在,您的本地映像将构建得非常快,因为它扩展了您的基本映像,该映像已经完成了所有繁重的依赖项安装和提升工作。
作为在容器内执行 git clone 的替代方法,您可以将代码目录挂载到 docker 容器中,这样当您对主机上的代码进行更改时,它们将立即反映在容器内:
本地/Dockerfile
FROM your-image-name-base:your-tag
# lets node execute the source code
CMD ["node", "index.js"]
Run Code Online (Sandbox Code Playgroud)
然后你会运行:
docker run -v /path/to/your/code:/path/inside/container your-image-name-local:your-tag
Run Code Online (Sandbox Code Playgroud)
这将挂载您的容器内的目录,然后执行您的CMD
.