如何减lessdocker image安装依赖关系所需的时间?
我有一些节点docker集装箱,基本上看起来像:
# 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"]
当我现在重build图像以便收集新的更新时,它会从npm下载所有依赖关系。 这总是需要大约5分钟。
我现在想知道如何避免重新安装所有的依赖关系。
我到目前为止的一个想法是使用VOLUME
,然后与主机共享代码存储库,这将使其很难在其他主机上使用该映像。
更新:我有另一个想法是创build一个容器容器,其中包含一个git仓库,并与运行时容器共享。 但是,回购容器必须能够以某种方式重build其他容器?
这听起来像是你有一个基础的形象 ,build立你的依赖和一个本地图像 ,扩展它,以便您可以快速构build/运行。
就像是:
碱/ 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"]
然后build立你的本地形象:
cd local docker build -t your-image-name-local:your-tag .
并运行它像:
docker run your-image-name-local:your-tag
现在你的本地图像将会很快build立起来,因为它扩展了你的基本图像,这已经完成了所有的重要的依赖安装和提升。
作为在你的容器中做一个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
。