使用docker-compose锁定Docker容器中的package.json文件

问题描述:

我将Docker for Mac和Docker Compose用于Node.js应用程序的开发,并且 package.json 文件被锁定时遇到错误.在正在运行的容器中运行npm install --save <package>之后的具体错误是:

I'm using Docker for Mac and Docker Compose for development of a Node.js application and I'm experiencing an error with the package.json file being locked. The specific error after running npm install --save <package> within a running container is:

npm WARN saveError EBUSY: resource busy or locked, rename
'/Example/package.json.1647356251' -> '/Example/package.json'

简化的包装结构为:

▾ docker/
    Dockerfile
  docker-compose.yaml
  package.json

Dockerfile 包含:

FROM node:9.5
ENV SOURCE_CODE /Example
COPY package.json $SOURCE_CODE/package.json
RUN npm --prefix $SOURCE_CODE install $SOURCE_CODE
WORKDIR $SOURCE_CODE

docker-compose.yaml 文件包含:

version: "3"

services:
  node:
    build:
      context: ./
      dockerfile: ./docker/Dockerfile
    volumes:
      - ./node_modules/:/Example/node_modules/
      - ./package.json:/Example/package.json

package.json 文件包含:

{
  "name": "example",
  "version": "1.0.0",
  "description": "example",
  "license": "UNLICENSED"
}

运行docker-compose run --entrypoint bash node以启动bash,然后在容器内运行npm install --save redux会产生有关 package.json 文件被锁定的警告,但是可以将文件写入主机上的node_modules 目录.如何避免使用这种结构锁定 package.json 文件?

Running docker-compose run --entrypoint bash node to start bash, then running npm install --save redux inside the container yields the warning about the package.json file being locked, however files are able to be written in the node_modules directory on the host. How can I avoid locks on package.json file using this structure?

我遇到了相同的问题,我通过安装 package.json 所在的文件夹而不是 package来解决此问题. json 本身.

I encountered the same issue and I solved this by mounting the folder where package.json is located instead of package.json itself.

version: "3"

services:
  node:
    build:
      context: ./
      dockerfile: ./docker/Dockerfile
    volumes:
      - .:/Example

直接安装 package.json 是因为卷似乎锁定了文件.

Mounting package.json directly as a volume seems to lock the file.