npm install 4x slower in docker container compared to host machine

Jan*_*ter 5 performance node.js npm docker docker-compose

I'm trying to provision a project locally that's using NodeJs with NPM.

I'm running npm install on my host machine (MacBook Pro Retina, 15-inch, Mid 2015) using nvm with node version 10.19:

added 2335 packages from 985 contributors and audited 916010 packages in 61.736s
Run Code Online (Sandbox Code Playgroud)

When I run the same setup in Docker, the result is much slower. This is my docker-compose.yml file:

version: '3.4'
services:
  node:
    image: node:10.19-alpine
    container_name: node
    volumes:
      - .:/app/
      - npm-cache:/root/.npm
    working_dir: /app
    command: ["tail", "-f", "/dev/null"]

volumes:
  npm-cache:
    external: false
Run Code Online (Sandbox Code Playgroud)

Then I execute:

added 2335 packages from 985 contributors and audited 916010 packages in 61.736s
Run Code Online (Sandbox Code Playgroud)

And the result is:

added 2265 packages from 975 contributors and audited 916010 packages in 259.895s
Run Code Online (Sandbox Code Playgroud)

(I'm assuming the number of resulting packages is different due to a different platform).

I thought the speedy installation was achieved by having a local cache (that's why there is an extra volume for caching in the docker-compose) but then I ran:

$ npm cache clean --force && rm -rf ~/.npm && rm -rf node_modules
Run Code Online (Sandbox Code Playgroud)

and the result for installation on the host machine is still consistently ~60 seconds.

When it comes to resources allocated to the Docker VM, it shouldn't be a problem, here's my Docker VM configuration:

泊坞窗设置 码头工人版本

I don't know where else to look, any help would be greatly appreciated.

Thanks

fal*_*sky 2

这种速度减慢是由容器和主机之间共享文件引起的。为了应对这个问题,你可以尝试一下docker-sync

该工具支持主机和容器(包括rsync)之间自动同步的不同策略。

但是,请注意它有自己的问题,例如偶尔的同步冻结。