0

I am trying to share my local space with my docker container with Volume. BTW I am working on a nest app. The problem is whenever I am trying to enable the volume feature I am getting some weird error!

api_1  | npm ERR! code ENOENT
api_1  | npm ERR! syscall open
api_1  | npm ERR! path /usr/app/package.json
api_1  | npm ERR! errno -2
api_1  | npm ERR! enoent ENOENT: no such file or directory, open '/usr/app/package.json'
api_1  | npm ERR! enoent This is related to npm not being able to find a file.
api_1  | npm ERR! enoent 
api_1  | 
api_1  | npm ERR! A complete log of this run can be found in:
api_1  | npm ERR!     /root/.npm/_logs/2020-05-24T19_44_36_593Z-debug.log

I am calling this weird because I haven't found any proper explanation about this error. although these links are quite close to my problem prob1, prob2

My docker file -

# Download base image
FROM node:alpine

# Define Base Directory
WORKDIR /usr/app

# Copy and restore packages
COPY ./package*.json ./
RUN npm install

# Copy all other directories
COPY ./ ./

# Setup base command
CMD [ "npm", "run", "start" ]

and my docker-compose file

version: '3'
services: 
   api:
     build:
        context: .
        dockerfile: Dockerfile.dev
    ports : 
        - "4200:4500"
    volumes:
        - /usr/app/node_modules
        - /:/usr/app

as far as I can understand whenever I tried to use volume means /:/usr/app replacing docker directory with local project directory. But on my project root directory, I have package.json file. Also in my docker file, I have copied package.json file.

can anyone help me with this issue? I am new with docker, I am doing this just for learning purpose.

Amjad
  • 330
  • 7
  • 22
  • In your `docker-compose`file, you are mounting your host `/` to `/usr/app`? Are you really sure that it is what you intended? Didn't you intended to do `- ./:/usr/app` rather? – β.εηοιτ.βε May 24 '20 at 20:43
  • This said, mind that when you bind mount (via `volumes`), a host folder to a container folder, you are totally overwriting the full container folder. So any file you would have copied at the build time is just gone. – β.εηοιτ.βε May 24 '20 at 20:45
  • Whenever i tried to add `.` or `./` i got another error. That's why i add `/`. – Amjad May 24 '20 at 20:47
  • What's the need of mounting `/:/usr/app`? As you are already `COPY ./ ./` in the container? – nischay goyal May 24 '20 at 20:50
  • I also tried to just map `SRC` folder but then got `typescript` config missing error – Amjad May 24 '20 at 20:50
  • Because as my intention to work as dev env. I mean when i will change anything to project dir, i want that to also be reflected to container – Amjad May 24 '20 at 20:52

2 Answers2

0

You should change your service name api to app. The name you pick for your service in docker compose will be your directory name within the container. So the docker engine creates a directory for your app in src/usr/api and puts your package.json in it. But -based on the Dockerfile- it searches for package.json in src/usr/app so throw this err.

-1

When you mount a host path to a path inside docker the file that existed in that path will become inaccessible. You copied the files to a path /usr/app. Then when you run the container you mount an empty host path on top of that. This is why the files are missing. What you should do is copy everything to the bind mount and then mount it.

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 22 '22 at 07:03