node icon indicating copy to clipboard operation
node copied to clipboard

Windows Container: fs.realpathSync() is broken on shared volumes

Open StefanScherer opened this issue 10 years ago β€’ 68 comments

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

StefanScherer avatar Oct 02 '16 13:10 StefanScherer

After searching hours with the exception , I got to know this is an existing issue.

I have a asp.net core api application running under a windows docker container. In my application I invoke node js using INodeService . It is working fine in local mode without docker and also working well in any cloud hosting without docker. But when I am running my application inside docker windows container, I am getting following exception in my InodeServices.InvokeAsync() method where I am giving the location of the js file and other parameters.

Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: ENOENT: no such file or directory, lstat 'C:\ContainerMappedDirectories' Error: ENOENT: no such file or directory, lstat 'C:\ContainerMappedDirectories' at Object.patchedLStat [as lstat] (C:\Users\ContainerAdministrator\AppData\Local\Temp\32zx2v0u.50k:155:27) at Object.realpathSync (fs.js:1649:15) at toRealPath (module.js:158:13) at tryFile (module.js:154:22) at tryExtensions (module.js:166:22) at Function.Module._findPath (module.js:219:20) at Function.Module._resolveFilename (module.js:536:25) at Function.Module._load (module.js:468:25) at Module.require (module.js:587:17) at require (internal/module.js:11:18) at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.d__71.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance.d__131.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.d__101.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.d__101.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at WebApplication1.Implementation.AssetReportMgr.d__10.MoveNext() in C:\sourceTree\connected assets\253\careports\HON.SPS.SIOT.CA.REPORT.API\HON.SPS.SIOT.CA.REPORT.API\Implementation\AssetReportMgr.cs:line 143

Here is my invoking code and docker file details:

var report = await _nodeServices.InvokeAsync<byte[]>("./pdf", html, data);

the pdf.js file is under my project root directory.

dockerFile:

FROM microsoft/aspnetcore:2.0-nanoserver-sac2016 AS base

ENV NODE_VERSION 8.9.4 ENV NODE_DOWNLOAD_SHA 48946e99ac4484e071df25741d2300f3a656f476c5ff3f8116a4746c07ebe3b7

Retrieve node and git https://nodejs.org/dist/v8.9.4/ ENV NODE_VERSION 8.9.4 ENV NODE_DOWNLOAD_SHA 48946e99ac4484e071df25741d2300f3a656f476c5ff3f8116a4746c07ebe3b7

RUN Invoke-WebRequest -UseBasicParsing https://nodejs.org/dist/v${env:NODE_VERSION}/node-v${env:NODE_VERSION}-win-x64.zip -outfile node.zip; if ((Get-FileHash node.zip -Algorithm sha256).Hash -ne $env:NODE_DOWNLOAD_SHA) { Write-Host 'NODEJS CHECKSUM VERIFICATION FAILED!'; exit 1; };

Expand-Archive node.zip -DestinationPath nodejs-tmp; Move-Item nodejs-tmp/node-v${env:NODE_VERSION}-win-x64 "${Env:ProgramFiles}/nodejs"; Remove-Item -Force node.zip; Remove-Item -Force nodejs-tmp

RUN setx /M PATH $($Env:PATH + ';' + $Env:ProgramFiles + '/nodejs')

WORKDIR /app EXPOSE 80

FROM microsoft/aspnetcore-build:2.0-nanoserver-sac2016 AS build WORKDIR /src COPY *.sln ./ COPY HON.SPS.SIOT.CA.REPORT.API/HON.SPS.SIOT.CA.REPORT.API.csproj HON.SPS.SIOT.CA.REPORT.API/ COPY HON.SPS.SIOT.CA.REPORT.Azure.Blob/HON.SPS.SIOT.CA.REPORT.Azure.Blob.csproj HON.SPS.SIOT.CA.REPORT.Azure.Blob/ RUN dotnet restore COPY . . WORKDIR /src/HON.SPS.SIOT.CA.REPORT.API RUN dotnet build -c Release -o /app

FROM build AS publish RUN dotnet publish -c Release -o /app

FROM base AS final WORKDIR /app COPY --from=publish /app . RUN npm install ENTRYPOINT ["dotnet", "HON.SPS.SIOT.CA.REPORT.API.dll"]

Dockercompose File:

version: '3'

services: hon.sps.siot.ca.report.api: image: hon.sps.siot.ca.report.api build: context: . dockerfile: HON.SPS.SIOT.CA.REPORT.API\Dockerfile

Node Version: 8.9.4 Platform: Docker Windows Container

If will be great help if some one tell me how to provide the relative path for pdf.js because /pdf is not working here.

arupbaroi avatar Mar 27 '18 09:03 arupbaroi

@arupbaroi: What command are you using to start the docker container?

tfrijsewijk avatar Mar 27 '18 11:03 tfrijsewijk

@VolleMelk docker run -d -p 8080:80 --name myrunningapp newimage

arupbaroi avatar Mar 27 '18 12:03 arupbaroi

I know this is not the issue being discussed here, but i wanted to point that although the latest windows version (Win10 April Update) and latests nano-insider builds solve this issue, i get stuck running npm or other node programs that use symbolic links on mounted volumes.

Looks like symbolic lynking is not supported on mounted volumes, even running mklink manually does not work, returning an Access Denied error, see:

https://github.com/docker/for-win/issues/1997

ielcoro avatar May 04 '18 07:05 ielcoro

Symlinks don't work on mounted volumes for containers with 'hyper-v' isolation, but they do work for containers with 'process' isolation.

On Fri, May 4, 2018 at 8:26 AM, IΓ±aki Elcoro [email protected] wrote:

I know this is not the issue being discussed here, but i wanted to point that although the latest windows version (Win10 April Update) and latests nano-insider builds solve this issue, i get stuck running npm or other node programas that use symbolic links on mounted volumes.

Looks like symbolic lynking is not supported on mounted volumes, even running mklink manually does not work, returning an Access Denied error, see:

docker/for-win#1997 https://github.com/docker/for-win/issues/1997

β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nodejs/node/issues/8897#issuecomment-386524057, or mute the thread https://github.com/notifications/unsubscribe-auth/ABV6Uo-PJMVnIWbC8lk1nAtqZYm55kLpks5tvAKigaJpZM4KMAbs .

jan11011977 avatar May 04 '18 07:05 jan11011977

Good news, as @ielcoro mentioned. Mounted volumes work also with --isolation hyperv (the only available mode in Windows 10) with Windows 10 1803, the Windows Server 17623.1002 and upcoming Windows Server 1803 and next LTSC Windows Server 2019.

StefanScherer avatar May 04 '18 07:05 StefanScherer

@jan11011977 That is dissapointing, one useful (maybe the most useful) use case for mounting volumes in Docker is for live editing during development and isolating development tools and dependency versions between projects, resulting in a clean host environment and making very easy to share the same development environment between team members. And development is usually done in Windows 10 client operating system which does not support process isolation.

This affects tools like npm or git, and makes for us Docker unusable as development platform on Windows. :-(

ielcoro avatar May 04 '18 09:05 ielcoro

When Windows Server 2016, Version 1803 arrives in a few days/weeks we should have new base images instead of the latest insider images. Then it should work with these base images. Looking forward to this.

StefanScherer avatar May 04 '18 10:05 StefanScherer

@StefanScherer Yes, the new base images are much better and will make Docker on Windows more approachable to a lot of people.

Still us will be blocked by the symlink error on hyperv isolation mode aka "the only Windows 10 mode for containers"

ielcoro avatar May 04 '18 10:05 ielcoro

Hi, thanks for your resolution tracks but --isolation hyperv did not solved the issue in my case. So,... is there a way to be able to develop locally nodejs files and build thems in a nodejs container ?

Fredoid avatar May 20 '18 22:05 Fredoid

I finaly followed this solution. Problem with symlinks are now fixed but I now facing another issue. Access to drive G:\ seems to be forbiden :

webapp.cdn.build_1  | .\combine.ps1 : AuthorizationManager check failed.
webapp.cdn.build_1  | At line:1 char:34
webapp.cdn.build_1  | + $ErrorActionPreference = 'Stop'; .\combine.ps1
webapp.cdn.build_1  | +                                  ~~~~~~~~~~~~~
webapp.cdn.build_1  |     + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecord
webapp.cdn.build_1  |    Exception
webapp.cdn.build_1  |     + FullyQualifiedErrorId : UnauthorizedAccess

Thanks

Fredoid avatar May 21 '18 16:05 Fredoid

My mistake, problem was related to PS's Execution Policies. I switched from Unrestricted to Bypass and the issue disappeared.

Thank so much for your work and helping us.

Fredoid avatar May 21 '18 16:05 Fredoid

I am getting below error

  •   InnerException	{Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: 
    

Webpack dev middleware failed because of an error while loading 'aspnet-webpack'. Error was: Error: ENOENT: no such file or directory, lstat 'C:\ContainerMappedDirectories' at Object.realpathSync (fs.js:1430:7) at toRealPath (internal/modules/cjs/loader.js:202:13) at tryFile (internal/modules/cjs/loader.js:198:22) at tryPackage (internal/modules/cjs/loader.js:179:10) at Function.Module._findPath (internal/modules/cjs/loader.js:284:18) at Function.Module._resolveFilename (internal/modules/cjs/loader.js:578:25) at Function.Module._load (internal/modules/cjs/loader.js:507:25) at Module.require (internal/modules/cjs/loader.js:637:17) at require (internal/modules/cjs/helpers.js:20:18) at Object. (C:\Users\ContainerAdministrator\AppData\Local\Temp\rzvqlxpj.bl4:83:19) Current directory is: C:\app

at Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance.d__71.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance.d__131.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.d__101.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.NodeServices.NodeServicesImpl.<InvokeExportWithPossibleRetryAsync>d__101.MoveNext()} System.Exception {Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException}

shubhrajitb avatar Aug 07 '18 12:08 shubhrajitb

Running into this issue using microsoft/dotnet:4.7.2-sdk trying to use NPM Will try microsoft/dotnet-framework:4.7.2-sdk-20180814-windowsservercore-1803 and see if the problem still persists.

Francommit avatar Sep 07 '18 07:09 Francommit

So after reading this article is appears -- https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility

It appears I can't use the version that's based off windowservercore-1803, while I could try it, it really wouldn't solve my problem to begin with.

Francommit avatar Sep 09 '18 23:09 Francommit

@Francommit if itβ€˜s hard to have a Windows Server 1803 semi-annual release watch out for the next long-term supported Windows Server 2019 which will be available soon.

StefanScherer avatar Sep 10 '18 05:09 StefanScherer

Yeah unfortunately I'm going to have to wait for WS2019.

Given I'm building node-packages in my container as a workaround I've just robocopied everything from a mount-point to a directory in the container then copied the artifacts back to the mount point.

Not the most elegant solution or quickest but it proves my code will build for now!|

EDIT: Why robocopy and not xcopy you might ask? Try and run xcopy in a Windows Server 2016 based windowsservercore image and see what happens πŸ‘Ž

Francommit avatar Sep 10 '18 05:09 Francommit

@StefanScherer I still have this issue with your latest nanoserver nodejs image, so I assume this is still not resolved. Do you have a new workaround? The powershell-trick is not working, I still have the error from your first post.

EDIT: Got a brainwave, I decided to copy the mount to a folder inside the container before attempting to run. It's not ideal, technically takes double the size, but it works...

Dockerfile

FROM stefanscherer/node-windows:latest

RUN mkdir C:\app
RUN mkdir C:\app\mount
RUN mkdir C:\app\user

COPY startup.bat C:/app/startup.bat

CMD ["C:\app\startup.bat"]

startup.bat

copy C:\app\mount\* C:\app\user
cd C:\app\user
node app.js

Would very much still like an actual fix or workaround :S

EDIT EDIT: I'm an idiot, I didn't switch to the new volume... The workaround still works fine.

LennardF1989 avatar Oct 13 '18 21:10 LennardF1989

@LennardF1989 The problem isn't fixed with the Windows 2016 base images. We have seen progress and fixes in the 1803 images, but people have to run Windows Server Core version 1803 which is quite rare.

Now Windows Server 2019 / Windows 10 1809 Update is around the corner with two new Windows base images

mcr.microsoft.com/windows/servercore:ltsc2019
mcr.microsoft.com/windows/nanoserver:1809

And with these images bind mounting a folder into the container works fine in combination with Node.js.

Edit: I also just tried to run a Windows 2016 based Node image on a Windows Server 2019 in HyperV isolation mode, but the problem still exists. This is because the Windows 2016 base image brings its old Windows kernel where the bind mount isn't fixed for Node.js. So the best experience will be only with the new 1809/ltsc2019 base images running on the latest Windows OS.

StefanScherer avatar Oct 14 '18 07:10 StefanScherer

I can confirm this is fixed in 1803 Core. Got it working today!

Francommit avatar Oct 16 '18 06:10 Francommit