Windows Container: fs.realpathSync() is broken on shared volumes
- Version: 4.6.1, 6.9.1, 7.0.0
- Platform: Windows 10 1607 or Windows Server 2016 with Docker + Windows Containers
- Subsystem: fs
The function fs.realpathSync() and/or pathModule._makeLong() is broken using it on shared Docker volumes mapped from host into a Windows Container. It seems to interpret the junction point incorrectly which is an UNC path and interprets it as c:\ContainerMappedDirectories.
Steps to reproduce
You need Windows 10 Anniversary update with Containers feature and Docker installed. Or an Windows Server 2016 with Containers feature and Docker installed, eg. an Azure VM.
Create a test directory with a minimal Dockerfile to just put node.exe into a Windows container.
PS C:\> mkdir test
PS C:\> cd test
PS C:\test> notepad Dockerfile.
This is the content of the Dockerfile
FROM microsoft/nanoserver
ARG NODE_VERSION=4.6.1
RUN powershell -Command \
Invoke-WebRequest https://nodejs.org/dist/v$env:NODE_VERSION/win-x64/node.exe \
-OutFile node.exe -UseBasicParsing
CMD [ "node.exe" ]
Now build a Docker image eg. for Node.js 4.6.1 using a build argument:
PS C:\test> docker build -t nodeexe:4.6.1 --build-arg NODE_VERSION=4.6.1 .
We can check that the binary works inside the Windows container by showing the version
PS C:\test> docker run nodeexe:4.6.1 node --version
v4.6.1
Now check that fs.realpathSync() works with an existing directory inside the container.
PS C:\test> docker run nodeexe:4.6.1 node -p "const fs=require('fs'); fs.realpathSync('c:/windows')"
c:\windows
OK. Now we mount the current directory as shared volume into the container and have a look at it with the dir command:
PS C:\test> docker run -v "$(pwd):C:\test" nodeexe:4.6.1 cmd /c dir c:\
Volume in drive C has no label.
Volume Serial Number is B097-15C5
Directory of c:\
10/09/2016 10:57 PM 1,894 License.txt
11/04/2016 12:50 PM 14,259,744 node.exe
07/16/2016 05:20 AM <DIR> Program Files
07/16/2016 05:09 AM <DIR> Program Files (x86)
11/04/2016 01:43 PM <SYMLINKD> test [\\?\ContainerMappedDirectories\6C4D3D41-7E32-4992-BC03-44E1FAC23C0B]
10/09/2016 10:58 PM <DIR> Users
11/04/2016 12:50 PM <DIR> Windows
2 File(s) 14,261,638 bytes
5 Dir(s) 21,210,304,512 bytes free
OK, the directory C:\test is there, but you can see that it is a symlink to an UNC path. Let's check that this directory is accessible.
PS C:\test> docker run -v "$(pwd):C:\test" nodeexe:4.6.1 cmd /c dir c:\test
Volume in drive C has no label.
Volume Serial Number is B097-15C5
Directory of c:\test
11/04/2016 01:44 PM <DIR> .
11/04/2016 01:44 PM <DIR> ..
11/04/2016 01:46 PM 231 Dockerfile
1 File(s) 231 bytes
2 Dir(s) 21,837,713,408 bytes free
OK, we can see the Dockerfile from the host. So let's now check fs.realpathSync with that directory.
PS C:\test> docker run -v "$(pwd):C:\test" nodeexe:4.6.1 node -p "const fs=require('fs'); fs.realpathSync('c:/test')"
fs.js:839
return binding.lstat(pathModule._makeLong(path));
^
Error: ENOENT: no such file or directory, lstat 'c:\ContainerMappedDirectories'
at Error (native)
at Object.fs.lstatSync (fs.js:839:18)
at Object.realpathSync (fs.js:1439:21)
at [eval]:1:28
at Object.exports.runInThisContext (vm.js:54:17)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:409:26)
at node.js:579:27
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
Boom! As you can see the UNC path is misinterpreted as c:\ContainerMappedDirectories and then of course this directory does not exist inside the Windows container.
This error breaks eg. using npm install on such a shared volume or even running simple Node.js code from it.
Let's create a small hello.js, map the current folder into a container and try to run it.
PS C:\test> "console.log('hello');" | Out-File hello.js -Encoding Ascii
PS C:\test> docker run -v "$(pwd):C:\test" nodeexe:4.6.1 node c:\test\hello.js
fs.js:839
return binding.lstat(pathModule._makeLong(path));
^
Error: ENOENT: no such file or directory, lstat 'c:\ContainerMappedDirectories'
at Error (native)
at Object.fs.lstatSync (fs.js:839:18)
at Object.realpathSync (fs.js:1439:21)
at toRealPath (module.js:112:13)
at Function.Module._findPath (module.js:151:20)
at Function.Module._resolveFilename (module.js:323:25)
at Function.Module._load (module.js:276:25)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
When we copy the file from the shared volume into a local directory of the Windows container Node.exe is able to run it:
PS C:\test> docker run -v "$(pwd):C:\test" nodeexe:4.6.1 cmd "/c copy c:\test\hello.js . & node hello.js"
1 file(s) copied.
hello
Another problem of this bug is that running npm install also does not work in a shared volume.
I'm on node 4.6.0 and getting the same problem with gulp. Every time I run gulp -v, it only returns the cli version. No gulp version. All the gulp files are in the file system (./node_modules/gulp). If I force gulp to run, I get this exception:
fs.js:839
return binding.lstat(pathModule._makeLong(path));
^
Error: ENOENT: no such file or directory, lstat 'C:\ContainerMappedDirectories'
at Error (native)
at Object.fs.lstatSync (fs.js:839:18)
at Object.realpathSync (fs.js:1439:21)
at toRealPath (module.js:112:13)
at Function.Module._findPath (module.js:151:20)
at Function.Module._resolveFilename (module.js:323:25)
at Function.Module._load (module.js:276:25)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
Any ideas?
Here's what I have installed (notice gulp -v)
PS C:\app\test> node -v
v4.6.0
PS C:\app\test> npm -v
2.15.9
PS C:\app\test> gulp -v
[20:41:59] CLI version 1.3.0
PS C:\app\test> ls node_modules
Directory: C:\app\test\node_modules
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 5/2/2017 8:32 PM .bin
d----- 5/2/2017 8:32 PM gulp
@dealdiane At the moment you can use the following workaround using a mapped drive in the container.
Let's say your code is in C:\code then mapping that to a drive, eg. G: and then changing working to that drive works.
Add these lines until Node.js itself has a fix for the realPath problem in folders mounted into Windows containers.
VOLUME C:/code
RUN set-itemproperty -path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices' -Name 'G:' -Value '\??\C:\code' -Type String
WORKDIR 'G:\\'
@StefanScherer that works. Thanks!
I did saw your node-example branch before adding that comment but thought I'd stay clear of that because it involves hard-coding the mapped drive which isn't ideal. Anyway, looks like that's the only workaround for now so I'll have to live with that until fixed. Cheers. :)
@StefanScherer I am not sure this solution works if you are not doing it as part of the container image build. I used this command as well as just
New-PSDrive -Name "K" -PSProvider FileSystem -Root "??\C:\source"
While the drive is mounted, NodeJS does not seem to start the app at all. The registry entry won't take effect immediately will it?
@vikalyan The New-PSDrive command doesn't seem to be enough as I cannot start any program from that drive. The Set-ItemProperty trick has to be done in a Dockerfile with a separate RUN before you run a real Windows container with it.
@StefanScherer Makes sense. That's what I suspected. Will that work well when the bind mount is dynamic? That is, the bind-mount is done at the time of docker run instead of being part of the Docker file itself? Maybe doing the trick as a first step using docker exec before doing further docker exec commands in the container will work?
Ran into this issue as well the last two days. Basically exactly what @StefanScherer reports.
@vikalyan Yes, mounting a data volume using docker run --volume C:\code-on-host:C:\code works using the workaround (https://github.com/nodejs/node/issues/8897#issuecomment-298662512) @StefanScherer provided
@VolleMelk Thank you. Yes, we had to rebuild our container images and make some code changes to take advantage of @StefanScherer 's workaround. It works.
I just recalled, we did implement this a little bit different:
Dockerfile:
RUN mkdir C:/target
RUN Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices' -Name 'K:' -Value '\??\C:\target' -Type String
and we run with docker run --volume c:\my-host-dir:c:\target mycontainer
The main difference is that we are not using VOLUME, but just creating an empty directory for Set-ItemProperty to use. It feels 'cleaner', since there's no use for a data volume
Good news. Beginning with Windows Server Insider Preview 17046 (I've tested 17074 today) the mounted volumes just look like normal folders inside the Windows container.
So Node.js just works fine with it.
Even my more complex scenario VSCode on Mac with sourcecode -> mapped into a VMware shared folder into the Win 17074 VM -> mapped into a 17074 windowsservercore-insider container worked in conjunction with nodemon --inspect. This means I can develop native Windows apps on a Mac with the help of Windows containers (and the helper VM running Docker).
@StefanScherer Cool!
@jhowardmsft, @jstarks, @patricklang - Does this work with Hyper-V containers as well?
@vikalyan Yes, also Hyper-V containers 😍
# on my Mac
$ ls
Dockerfile app.js index.html package-lock.json
README.md docker-compose.yml node_modules package.json
# in a Windows Hyper-V container
$ docker run -v C:$(pwd):C:/spec --isolation hyperv microsoft/nanoserver-insider cmd /c dir spec
Volume in drive C has no label.
Volume Serial Number is 2C0C-0420
Directory of C:\spec
01/17/2018 03:40 PM <DIR> .
01/16/2018 11:09 PM <DIR> ..
10/05/2016 08:36 AM 482 index.html
01/17/2018 03:40 PM 301 Dockerfile
01/17/2018 01:58 PM <DIR> node_modules
01/17/2018 03:40 PM 744 README.md
01/17/2018 03:40 PM 106,091 package-lock.json
01/17/2018 03:40 PM 236 package.json
01/17/2018 03:40 PM 376 docker-compose.yml
01/17/2018 03:40 PM 808 app.js
7 File(s) 119,790 bytes
3 Dir(s) 127,956,414,464 bytes free
@StefanScherer - Very cool. This should unblock a lot of scenarios that were blocked for us.
@StefanScherer probably a dumb question but what do I need to run Windows Server Insider Preview >= 17074? Do I need to use that as my OS or can I just docker run it?
I've been docker running it but have been getting this error:
failed to register layer: re-exec error: exit status 1: output: ProcessUtilityVMImage C:\ProgramData\Docker\windowsfilte
r\xxxx\UtilityVM: The system cannot find the path specified.
That's when I run it inside Server 2016. No bitlocker nor any AV running.
@dealdiane That‘s ‚normal‘, you can‘t run containers newer than the host kernel version.
To run insider containers you need a Server Insider.
You can register to the Windows Server Insider Preview program and download the VHD or ISO from https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewserver
I have a Packer template to automate the creation of a Vagrant box file to have a complete Docker VM with the Insider base images installed.
@StefanScherer Thanks. That's what I was worried about.
This is for a build server. I was hoping that there's a way to use it without having to setup another build server. Based on your comment, I guess there isn't.
I'm currently stuck with something where even the mapped drive trick doesn't work.
I am not able to use the workaround @StefanScherer posted right now on windows 10. Could it be it's related to using the stefanscherer/node-windows container on windows 10 instead of server 1709?
I can do the set-itemproperty command in my dockerfile. I can start a container with no volumes and the working direct is G:\ and I can see files. If however I put an volume statement in the dockerfile before the set-itemproperty command as shown in the workaround, I get this error.
Error response from daemon: container 191662a7b37115c4cace466370f23540db542103eee1d6ea765bca78d2197f86 encountered an error during Start: failure in a Windows system call: The compute system exited unexpectedly. (0xc0370106).
similarly, if I leave the volume statements out of the dockerfile and specify them on the docker run command with -v I get the same error.
@VolleMelk yeah, an empty directory does the trick, no more error. thanks!