简体   繁体   中英

Docker volumes and package.json not found

I m learning docker and I m having some troubles dealing with volume on a nodejs application.

Actually, I have a simple application that I want to test each time I restart my container.

This way, I have the following dockerfile :

FROM node:4-onbuild

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

CMD [ "npm", "test" ]

Now, I have built the image using :

docker build -t myapp .

And I tried to run it using the followings scripts :

docker run -v //c/Project/nodejs/my-app:/usr/src/app my-app

or

docker run -v /c/Project/nodejs/my-app:/usr/src/app my-app

or even

docker run -v c:/Project/nodejs/my-app:/usr/src/app my-app

I have the following error that tells me that I don't have a package.json file inside of /usr/src/app (but it's where my volume is located, it should be right no ?)

npm info it worked if it ends with ok
npm info using npm@2.15.9
npm info using node@v4.5.0
npm ERR! Linux 4.4.15-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "test"
npm ERR! node v4.5.0
npm ERR! npm  v2.15.9
npm ERR! path /usr/src/app/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! Please include the following file with any support request:
npm ERR!     /usr/src/app/npm-debug.log

NB : if I use the COPY command instead of volumes, it works great, and I can see my nodejs tests inside of the docker container

NB2 : I m on windows 10, docker v1.12.0

Docker for Windows runs in a VM that runs your containers inside (it still needs a Linux Host). When you mount a host volume, that volume is mounted on the Linux host. The only directory that is mounted from the Linux VM to the parent Windows OS is c:/Users, which is visible as /c/Users in the VM (see docker's volume tutorial ). Move your project to a directory under Users and mount that.

The reason for the empty folder/missing file is that this is the default when you mount a non existent host folder/file into a container. And in your case, /c/Project does not exist in the VM. The COPY works because doing a build sends over the current folder (with the exception of .dockerignore entries) to the Docker engine (running in the VM) before the build begins. This is transmitted over the API rather than doing a volume mount.

Sometimes Docker for Windows is unable to access the files on your filesystem. It fails silently, so it will start the container and run your command and npm will be unable to find the files it needs.

You can fix this by resharing the drives you need.

  1. Open Docker for Windows settings by right clicking the icon in the taskbar
  2. Go to the Shared Drives tab
  3. Unselect the drive you need to get working and hit apply.
  4. After it applies, select the drive again and hit apply.
  5. Enter your (Windows user account) password when it asks.

When done you should be able to mount volumes normally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM