How can avoid symlink problems with npm running in Docker on a Windows host?
Asked Answered
R

2

6

I am using the latest Docker public beta on Windows 10.

I am developing a Node.js app and I am using Docker with my src directory mounted as a volume in a container.

I cannot run npm install from inside the container because it will cause the creation of symbolic links (in the node_modules directory), that apparently are not supported in volumes if the host is Windows.

Is there anything I can do to solve this problem?

I have no issues on running this in a Linux environment.

After some research I found many solutions involving Virtualbox, being for the "old" version of Docker (Docker Toolbox).

Rothberg answered 1/7, 2016 at 10:5 Comment(1)
What's the project you're trying to build? Alternatively, can you provide a simple example that reproduces the problem?Efrenefron
R
11

I solved in the following way.

I'll use /usr/src as the directory to mount to in the container in this example:

  1. Mount the src directory of your app on /usr/src: -v /path/to/src:/usr/src
  2. Define a data volume for node_modules: -v /usr/src/node_modules

In this way you will have that /path/to/src will be mounted to /usr/src and /usr/src/node_modules will be mounted as a data volume.

The final result is that the even though a node_modules directory is created on the host, it will stay empty.

This solution exploits Docker Data Volumes.

This is applicable every time you want to avoid that changes in a subdirectory of a mounted directory to be reflected on the host, not just for node_modules.

Rothberg answered 1/7, 2016 at 14:53 Comment(1)
This worked for me. Not the greatest, but it works. We just use docker-compose for local development environments anyway.Thornhill
C
5

To avoid symlink error with NPM you can use command:

npm install --no-bin-links

However, to absolutely avoid symlink issue, you should not set your nodejs project in the mounted directory (mounted from Window)

Cantilever answered 1/7, 2016 at 10:29 Comment(3)
I tried with that already, but it seems I cannot run my gulp tasks :( Those packages should be installed as global ones, right?Rothberg
With Front-end code, you should not build it in Docker Container. I think you build in host and mount it into Docker by COPY. But If you are trying to use Docker for Development Environment. I think that it is not good way.Cantilever
The second suggestion is the real one! Dude, you saved my life! What a brilliant idea. To elaborate a bit more. I was battling an issue between Docker + Windows + Magento + Grunt and I wasn't able to compile the themes as grunt was trying to create symlinks in the pub folder and somehow they turned corrupted. When I used your suggestion i.e. create a master symlink to a local (for Docker's vm) folder - it worked like a charm!Distributor

© 2022 - 2024 — McMap. All rights reserved.