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

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!

Related

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!

Error running Docker node container on Windows 10

I am new to Docker and attempting to run through a few simple examples on both my Windows 10 PC and my Mac simulteneously. The example below works fine on Mac but not on the PC.
Docker Toolbox is installed and I've downloaded the official Node image from DockerHub. I used Express to create a simple Javascript-based site using Handlebars that works fine if I execute npm start locally from the host.
When I attempt to start the container with the following command I get an error on the Windows machine:
$ docker run -p 8080:3000 -v $(pwd):/var/www -w "/var/www" node npm start
npm info it worked if it ends with ok
npm info using npm#3.10.9
npm info using node#v7.2.0
npm ERR! Linux 4.4.27-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "start"
npm ERR! node v7.2.0
npm ERR! npm v3.10.9
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 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
The same command works fine on the Mac and I can hit http://host-ip:8080 no problem.
Again, this is a simple example that should work on both platforms, wondering if the error may be a known difference in the way Docker Toolbox works on each platform, or perhaps a syntax error in the command.
Any help is appreciated.
With Docker Toolbox on Windows (involving VirtualBox), make sure your $(pwd) is a path starting with C:\Users\<myLogin>: only this folder is pre-shared and auto-mounted by VirtualBox.
You can try a docker run with a ls /var/www as CMD to check if /var/www does reflect your $(pwd) content.
docker run -p 8080:3000 -v $(pwd):/var/www -w "/var/www" node ls

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.
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.

Docker running test with nodejs and volume

I'm learning docker and I'm having some troubles with the volume in a development environment with Nodejs.
I having the following simple dockerfile that aims to start my unit tests from a NodeJS parent image:
FROM node:4-onbuild
VOLUME ["/usr/src/app"]
CMD [ "npm", "test" ]
I'm running my container this way:
docker run -v /C/Users/myUser/dockertest:/usr/src/app notest
But I keep receiving the following error:
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
I don't understand why I'm not able to find the package.json file inside of the /usr/src/app folder inside of my container.
The fact is that if I'm using the following docker file :
FROM node:4-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
CMD [ "npm", "test" ]
My tests will be running in the container, but only after building step (so I have to build each time I make a modification...) . I need to play these tests any time I want, by launching a docker command (restart ? exec ?) quickly
Thanks for your help
You want to COPY everything build relevant and link your codebase on containerstart.
FROM node:4.4
# Enviroment variables
ENV HOMEDIR /data
RUN mkdir -p ${HOMEDIR}
WORKDIR ${HOMEDIR}
# install all dependencies
COPY package.json ./
RUN npm install
# add node content initially
COPY . .
CMD ["npm", "test"]
And then link it on start:
version: '2'
services:
myApp:
build:
context: .
dockerfile: Dockerfile
image: "myApp"
volumes:
- .:/data
You should now be able to change your codebase without restarting/rebuilding your container.

Performing a npm install via Docker on a windows host

I'm trying to create a docker dev tools container for a devlopement environment on a windows host via docker toolbox but I have some trouble running the npm install command.
It worked fine on a linux host but on the windows host I got the following error :
npm ERR! Linux 4.1.13-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.5.0
npm ERR! npm v3.3.12
npm ERR! path /var/www/site/.npm/gulp/3.9.0/package.tgz.e87c24357cd6065ee71ce44c6f23673b
npm ERR! code ETXTBSY
npm ERR! errno -26
npm ERR! syscall rename
npm ERR! ETXTBSY: text file is busy, rename '/var/www/site/.npm/gulp/3.9.0/package.tgz.e87c24357cd6065ee71ce44c6f23673b' -> '/var/www/site/.npm/gulp/3.9.0/package.tgz'
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Linux 4.1.13-boot2docker
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.5.0
npm ERR! npm v3.3.12
npm ERR! path npm-debug.log.39d944b679d410e5293d6721cbc8287a
npm ERR! code ETXTBSY
npm ERR! errno -26
npm ERR! syscall rename
npm ERR! ETXTBSY: text file is busy, rename 'npm-debug.log.39d944b679d410e5293d6721cbc8287a' -> 'npm-debug.log'
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /var/www/site/npm-debug.log
Here is my Dockerfile :
FROM node:latest
RUN apt-get update
RUN apt-get install vim -y
RUN useradd -ms /bin/bash node
RUN echo "fs.inotify.max_user_watches=100000" > /etc/sysctl.conf
ADD . /var/www/site
RUN chown -R node:node /var/www/site
RUN chown -R node:node /usr/local/lib/node_modules
RUN chown -R node:node /usr/local/bin
USER node
ENV HOME /var/www/site
WORKDIR /var/www/site
RUN npm install -g bower
RUN npm install --global gulp -y
EXPOSE 80 8080 35729
In Docker quickstart terminal, I use the following commands :
Building the image (works fine)
docker build -t dev_tools .
Building the container (works fine)
docker run --name=dev_tools_container -t --rm -v "//c/Users/Public/site:/var/www/site" --net=host dev_tools
Trying to install npm dependencies (shoots the error):
docker exec -it dev_tools_container npm install
Thank you for your time !
Instead of
RUN npm install --global gulp -y
use
RUN sudo npm install --global gulp -y
You try to install gulp as a global package from user node (not superuser).
Or install gulp before switch user to node.
USER node
RUN npm install --global gulp -y
EDIT:
boot2docker is based on VirtualBox. Virtualbox does not allow symlinks on shared folders for security reasons.
To enable symlinks You must set VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME to 1. (Here is link to description how to do it on Vargrant: Symbolic links and synced folders in Vagrant)
VBoxManage setextradata VM_NAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME 1
Replace VM_NAME and SHARE_NAME and restart VirtualBox.
Another solution is add --no-bin-link to npm:
RUN npm install -g bower --no-bin-link
RUN npm install --global gulp -y --no-bin-link
EDIT 2
By default Windows 7 security policy does not allow creating symlinks as it's a potential security threat. If user is not in Administrators group run secpol.msc and navigate to Local Policies-User Rights Assignments and add your user to Create symbolic links.
If your user belongs to Administrators group then start VirtualBox with Run as Administrator.
You can mount node_modules as a volume, so it will be a Linux filesystem inside the Docker container. Add this to your Dockerfile:
VOLUME /var/www/site/node_modules
You will see the directory at C:Users/Public/site/node_modules because it is necessary for a mount point, but you will not see any contents unless you are inside the container.

Resources