Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 901
Marco Ferrari Avatar asked Jul 01 '16 10:07

Marco Ferrari


2 Answers

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.

like image 152
Marco Ferrari Avatar answered Sep 28 '22 05:09

Marco Ferrari


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)

like image 32
Nguyen Sy Thanh Son Avatar answered Sep 28 '22 06:09

Nguyen Sy Thanh Son