docker与纱

第一次试图让纱线和docker工作在一起。 每次运行docker build命令时,如何停止安装软件包?

我发现了一些解决scheme,如将node_modules存储在一个临时目录中,然后将其链接,但是安装了各种软件包,我得到的错误太多。 是否有一种方法可以将我的yarn.lock与Docker中存在的或者其他解决scheme进行比较?

Dockerfile:

 FROM node:8.9.1-alpine COPY package.json yarn.lock /usr/src/ RUN cd /usr/src \ && yarn install --pure-lockfile COPY . /usr/src EXPOSE 3005 

通过这个设置,我得到一个消息,说Sending build context to Docker daemon 375.2MB ,然后像往常一样运行yarn install ,每次获取包裹。

绝对要注意dockercaching。 基本上你想要比不太稳定的指令更早运行最稳定的指令。 可能导致与早期运行相同的图像更改的说明不应重新运行(不包括ENV / ARG指令的问题)。 但是一旦需要运行指令,无论caching中的内容如何,​​都将运行以下所有指令。

.dockerignore也会有所帮助,但事情很容易滑入。我已经采取了忽略所有内容的做法,然后指定应该复制的内容。

为了尽量减less从网上抓取,我喜欢使用纱线离线caching。 这存储已安装的依赖关系的压缩包,并将其用于将来的安装。 每次(如果高速caching已经失效),您都可以从npm rebuild全新安装中获得好处。 您可以使用.yarnrc文件configuration脱机caching,该文件可能位于您的主目录中,但为此,您需要在回放站中保存一个以及用于存储tarball的目录。

您可以select将您的镜像目录签入您的仓库。 一个典型的大型npm模块安装仍然只能在20兆的区域。

如果您使用下面的文件,根据您的需要进行定制,并在本地运行yarn ,yarn-offline-mirror将包含安装应用程序所需的tarball。

.dockerignore

 * !yarn-offline-mirror/ !src/ !package.json !yarn.lock !.yarnrc 

.yarnrc

 # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 yarn-offline-mirror "./yarn-offline-mirror" 

Dockerfile

 ENV HOME /usr/src/ WORKDIR $HOME # copy the tarballs COPY ["yarn-offline-mirror", "$HOME/yarn-offline-mirror/"] # copy files needed for the install COPY ["package.json", "yarn.lock", ".yarnrc", "$HOME/"] # the offline flag will mean that an error is raised if any # module needs to be fetched remotely. It can be removed to allow # yarn to fetch any missing modules if that was to happen. RUN yarn --offline --frozen-lockfile --link-duplicates # copy the rest.. could be further broken up into multiple instructions # for cache optimisation COPY . $HOME CMD npm start 

你应该更好地使用dockercaching。

如果你有你的Dockerfile准备如下:

 FROM node:carbon COPY package.json yarn.lock /app RUN cd /app \ && yarn install --pure-lockfile COPY . /app CMD doStuff 

docker build不会触摸package.json除非它已经改变。 除非caching在前面的步骤中失效,否则下一个RUN命令将不会执行。

注:保持您的.dockerignore文件中的.dockerignore