Docker volumes and package.json not found - node.js

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.
Open Docker for Windows settings by right clicking the icon in the taskbar
Go to the Shared Drives tab
Unselect the drive you need to get working and hit apply.
After it applies, select the drive again and hit apply.
Enter your (Windows user account) password when it asks.
When done you should be able to mount volumes normally.

Related

Jenkins build issue - npm ERR! Your cache folder contains root-owned files

I am trying to build a small node app on my Jenkins pipeline, which is running in a virtual machine. cross this error:
+ npm install
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /.npm
npm ERR! errno EACCES
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 111:120 "/.npm"
Running sudo chown -R 111:120 "/.npm" doesn`t help since it says:
chown: cannot access '/.npm': No such file or directory
And, as per my understanding, runs in a local context, when the problem is actually from the container perspective. I`ve tried to add the command above on my Docker and Jenkinsfile as well, to no avail. Below is my public repo:
Node app deploy on github
npm install --cache=".YourCustomCacheDirectoryName"
works perfectly fine, reason for this is your docker user isn't allowed to write in / ( root directory )
its not that a directory already exist at /.npm its that, your script is trying to create a directory at / which is not accessible for your user
you can either put
agent {
docker {
image 'node:latest'
args '-u root:root'
}
}
or just tell npm to use your custom cache directory
I had the same issue and fixed it by setting the npm cache directory to ENV variable in Dockerfile.
Add this to Dockerfile:
ENV npm_config_cache /home/node/app/.npm
As far as I can remember ,just updating npm version and deleting the whole project did the trick.

NPM not working on Virtualbox shared folder under Ubuntu on a Windows 10 host

I'm trying to set-up a development environment on a Virtualbox machine using Vagrant. I'm using Windows 10 as a host machine, and ubuntu/bionic64 as a guest machine.
My package.json file is located under a shared directory between the host machine and the guest machine. However whenever I run npm install --no-bin-links I do get the following error:
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/vagrant/www/wp-content/plugins/my-plugin/node_modules/array-initial/node_modules/is-number/package.json.1640612897
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/vagrant/www/wp-content/plugins/my-plugin/node_modules/array-initial/node_modules/is-number
/package.json.1640612897'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vagrant/.npm/_logs/2021-01-19T05_31_47_272Z-debug.log
Inside the guest machine, if I copy the package.json and package-lock.json into another directory that is not shared, and then run npm install everything works perfectly.
mkdir /tmp/test/
cp package.json package-lock.json /tmp/test/
cd /tmp/test/ && npm install
# NPM Packages are installed successfully!
I think this error is specific to Windows, as I've been a user of Ubuntu for years, and never had a similar issue. I only started using Windows a week ago.
The strange thing is, I also have another development environment on the same machine, and NPM works just fine, without any issues on shared folders!
I used the --no-bin-links, so I don't think the issue has anything to do with the symbolic links.
Looking into the strace output, I can't see when this file node_modules/array-initial/node_modules/is-number /package.json.1640612897 was created.
NPM log
Strace log

Is it possible to mount host machine folder when run process in docker container on the remote machine

I'm trying to launch my cypress tests on a remote machine with docker. So from host I do:
docker -H ssh://me#host run -v /home/jenkins/agent/workspace/TEST:/e2e -w /e2e --entrypoint=npm cypress/included:5.2.0 run test
The container on remote machine starting well, but then I get such errors:
npm ERR! syscall open
npm ERR! path /e2e/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/e2e/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
The folder from host wasn't mounted into container on remote machine.
Could you please advise, is it possible to mount in such configuration? And if so - how to do that correctly?
Thanks!

npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'

A bit stuck on this one. Taking a Grider course on Docker through Udemy.com.
I just keep getting the following error when I do:
docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app 5932996c40dc
npm ERR! path /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 '/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2019-01-30T23_04_17_890Z-debug.log
I echo the command out because I want to see exactly what it is trying to execute and comes back as:
docker run -p 3000:3000 -v /app/node_modules -v /mnt/c/projects/courses/docker_kubernetes/frontend:/app 5932996c40dc
I run the image without the -v so that I can start the container and exec -it into it to see what is on the container. It does show everything as it should inside the container. It copied everything into /app in the container.
The command I used to build the image was docker build -f Dockerfile.dev . and remove the node_modules from the local version.
Here is a copy of my repo that has the Dockerfile and such in it: https://github.com/ishraqiyun77/docker_kubernetes.
Running in Windows Subsystem for Linux (WSL) on Windows 10 Pro, docker-client is running in WSL, docker-daemon is running in VMWare Workstation Player 15. I did make the .env for good measure with CHOKIDAR_USEPOLLING=true. Not convinced that this config is the issue yet, but who knows, it could be.
Also, there is no /root/.npm/_logs/2019-01-30T23_04_17_890Z-debug.log in either the container or the local file system.
I've also changed WSL to instead of having the path /mnt/c/projects... to just be /c/projects.
I am also taking the Grider course on Docker through Udemy.com. And I met exactly the same problem as you. Since I am using Windows 10 Home Edition, I can't use Docker Desktop (Windows), instead, I have to use Docker Toolbox.
In my case, I am trying to run:
xwy52#JFC-DELL MINGW64 /d/Git/LearnDevOps/docker/images/frontend (master)
$ docker run -p 3000:3000 -v /app/node_modules -v $(pwd):/app 5d83ae77af14
And I met your problem, the solution for me is to configure another shared folder for the default virtual machine in VirtualBox, as seen in below screenshot:
Configure Shared Folder for Default VM
I tried adding the location where my code is to the Shared Folder via Virtual Box as mentioned by Wenyan but it did not work.
What worked for me is I moved my code to C://Users//{username} as C Drive is mentioned as the storage location in Virtual Box. I had no problems in running docker commands after moving the code.

Can't run sample express app in node container on Windows

I'm watching a Docker course from Pluralsight and I need to run a Docker source on a Docker container I just downloaded.
Even though I have package.json file in the current path, it is not recognized.
Usuario#RE MINGW64 /d/node/ExpressSite
$ docker run -p 8080:3000 -v /$(pwd):/var/www -w "/var/www" node:4.4.5 npm start
npm info it worked if it ends with ok
npm info using npm#2.15.5
npm info using node#v4.4.5
npm ERR! Linux 4.4.12-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v4.4.5
npm ERR! npm v2.15.5
npm ERR! path /var/www/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/var/www/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! /var/www/npm-debug.log
Why the Pluralsight does the same and it works? What's wrong with my version?
From official docs https://docs.docker.com/engine/userguide/containers/dockervolumes/:
If you are using Docker Machine on Mac or Windows, your Engine daemon has only limited access to your OS X or Windows filesystem. Docker Machine tries to auto-share your /Users (OS X) or C:\Users (Windows) directory. So, you can mount files or directories on OS X using.
docker run -v /Users/<path>:/<container path> ...
On Windows, mount directories using:
docker run -v /c/Users/<path>:/<container path> ...
So, you can create a directory inside C:\Users\<your_username> (code for example) with your code and mount it inside the container like so:
docker run -p 8080:3000 -v /c/Users/<your_username>/code:/var/www -w "/var/www" node:4.4.5 npm start
Please note that your code will be available inside the container in /var/www/ directory
pwd returns /c/Users/<my_username>. Try it yourself in Docker terminal.
You can use pwd for convenience sake:
docker run -p 8080:3000 -v $(pwd)/code:/var/www -w "/var/www" node:4.4.5 npm start
Good luck with the course and Dockerize all the things!

Resources