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'
The simplified package structure is:
▾ docker/
Dockerfile
docker-compose.yaml
package.json
The Dockerfile contains:
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
The docker-compose.yaml file contains:
version: "3"
services:
node:
build:
context: ./
dockerfile: ./docker/Dockerfile
volumes:
- ./node_modules/:/Example/node_modules/
- ./package.json:/Example/package.json
The package.json file contains:
{
"name": "example",
"version": "1.0.0",
"description": "example",
"license": "UNLICENSED"
}
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?
Yaml is a superset of json so any JSON file should be valid Yaml. To use a JSON file with Compose, specify the filename to use. In this lab we are going to bringup our same nginx and mysql containers using docker-compose file in json format.
The `package-lock. json` file was introduced in npm version 5 to solve this problem. It is a generated file and is not designed to be manually edited. Its purpose is to track the entire tree of dependencies (including dependencies of dependencies) and the exact version of each dependency.
lock. json is created for locking the dependency with the installed version. It will install the exact latest version of that package in your application and save it in package.
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
Mounting package.json directly as a volume seems to lock the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With