好的,所以我发现了这篇有关写docker文件时效率的精彩文章。
这是一个错误的docker文件在运行
RUN npm install指令之前添加应用程序代码的示例:
FROM ubuntuRUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.listRUN apt-get updateRUN apt-get -y install python-software-properties git build-essentialRUN add-apt-repository -y ppa:chris-lea/node.jsRUN apt-get updateRUN apt-get -y install nodejsWORKDIR /opt/appCOPY . /opt/appRUN npm installEXPOSE 3001CMD ["node", "server.js"]
通过将应用程序的副本分成2个COPY指令(一个用于package.json文件,另一个用于其余文件)并在添加实际代码之前运行npm
install指令,任何代码更改都不会触发RUN npm install指令,只有package.json的更改才会触发它。更好地实践docker文件:
FROM ubuntuMAINTAINER David Weinstein <david@bitjudo.com># install our dependencies and nodejsRUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.listRUN apt-get updateRUN apt-get -y install python-software-properties git build-essentialRUN add-apt-repository -y ppa:chris-lea/node.jsRUN apt-get updateRUN apt-get -y install nodejs# use changes to package.json to force Docker not to use the cache# when we change our application's nodejs dependencies:COPY package.json /tmp/package.jsonRUN cd /tmp && npm installRUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/# From here we load our application's pre in, therefore the previous docker# "layer" thats been cached will be used if possibleWORKDIR /opt/appCOPY . /opt/appEXPOSE 3000CMD ["node", "server.js"]
这是package.json文件的添加位置,安装其依赖项并将其复制到应用程序所在的容器WORKDIR中:
ADD package.json /tmp/package.jsonRUN cd /tmp && npm installRUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
为了避免每个docker构建上的npm安装阶段,只需复制这些行并将^ / opt / app ^更改为您的应用在容器中的位置。



