Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locked package.json files in Docker container using docker-compose

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?

like image 625
sal17 Avatar asked Feb 24 '18 07:02

sal17


People also ask

Can we use json in Docker-compose?

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.

Can I manually change package-lock json?

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.

Why is json package locked?

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.


1 Answers

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.

like image 52
Tom Van Rompaey Avatar answered Oct 20 '22 01:10

Tom Van Rompaey