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

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

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

Then I execute:

 docker-compose up -d node; docker exec -t node npm install

And the result is:

added 2265 packages from 975 contributors and audited 916010 packages in 259.895s

(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

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:

docker settings
docker version

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

Thanks

Source: StackOverflow