简体   繁体   English

我应该将“package-lock.json”复制到 Dockerfile 中的容器映像中吗?

[英]Should I copy `package-lock.json` to the container image in Dockerfile?

Here is my Dockerfile :这是我的Dockerfile

FROM node:12-slim

ENV NODE_ENV=production

WORKDIR /

# COPY . . # COPY ENTIRE FOLDER ?

COPY ./package.json ./package.json
COPY ./dist ./dist

RUN npm install --only=production

EXPOSE 8080

ENTRYPOINT npm start

Here is my .dockerignore file:这是我的.dockerignore文件:

node_modules

You see that I'm just copying package.json and not package-lock.json .你看我只是在复制package.json而不是package-lock.json I guessed that, since I'll be running RUN npm install to build the image, I thought that it should create its own package-lock.json .我猜到了,因为我将运行RUN npm install来构建映像,我认为它应该创建自己的package-lock.json

But I got this warning during the build:但是我在构建过程中收到了这个警告:

> Step #0: > protobufjs@6.10.2 postinstall /node_modules/protobufjs
> Step #0: > node scripts/postinstall
> Step #0:
> Step #0: npm notice created a lockfile as package-lock.json. You should commit this file.
> Step #0: npm WARN knative-serving-helloworld@1.0.0 No repository field.    
> Step #0: 
> Step #0: added 304 packages from 217 contributors and audited 312 packages in 15.27s

So, should I add this to my Dockerfile ?那么,我应该将此添加到我的Dockerfile吗?

COPY ./package-lock.json ./package-lock.json

You should absolutely copy the package-lock.json file in. It has a slightly different role from the package.json file: package.json can declare "I'm pretty sure my application works with version 17 of the react package", where package-lock.json says "I have built and tested with exactly version 17.0.1 of that package". You should absolutely copy the package-lock.json file in. It has a slightly different role from the package.json file: package.json can declare "I'm pretty sure my application works with version 17 of the react package", where package-lock.json说“我已经使用该软件包的 17.0.1 版本进行构建和测试”。

Once you have both files, there is a separate npm ci command that's optimized for this case.一旦你拥有这两个文件,就会有一个单独的npm ci命令针对这种情况进行了优化。

COPY package.json package-lock.json .
# Run `npm ci` _before_ copying the application in
RUN NODE_ENV=production npm ci
# If any file in `dist` changes, this will stop Docker layer caching
COPY ./dist ./dist

It depends if you want to have exactly the same env everywhere.这取决于您是否想在任何地方都拥有完全相同的环境。 If yes, package-lock.json is needed.如果是,则需要 package-lock.json。 There is a nice post about it here: https://stackoverflow.com/a/64014814/4925213这里有一个很好的帖子: https://stackoverflow.com/a/64014814/4925213

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 docker 容器中安装依赖项后如何复制回主机 package-lock.json/yarn.lock? - How to copy back to host package-lock.json/yarn.lock after install dependencies inside docker container? 如何将容器内部生成的package-lock.json文件传递给源代码? - How can I pass to my source code the package-lock.json file generated inside my container? 为什么在 `npm install` 时`package-lock.json` 会导致 docker 容器构建失败? - Why does `package-lock.json` causes a failure in a docker container build when `npm install`? 应该如何为Node.js Docker应用程序生成package-lock.json文件? - How should the package-lock.json file be generated for Node.js docker apps? 在使用Docker构建节点应用程序映像时,如何摆脱package-lock.json警告? - How to get rid of the package-lock.json warning when building a node app image with Docker? COPY package.json - Dockerfile - COPY package.json - Dockerfile Docker compose 仅在 app 文件夹中生成 package-lock.json - Docker compose only makes package-lock.json in app folder 如何在 Dockerfile 中复制 package.json? - How to copy package.json in Dockerfile? npm WARN old lockfile The package-lock.json file was created with an old version of npm - npm WARN old lockfile The package-lock.json file was created with an old version of npm `npm install` 在 node:10.21.0-jessie-slim Dockerimage 上出现 package-lock.json 失败 - `npm install` fails with package-lock.json on node:10.21.0-jessie-slim Dockerimage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM