How can I speed up node.js react startup in a Docker container

时光毁灭记忆、已成空白 提交于 2019-12-03 17:09:39

Bind mounting from a Host > VM > Container with osxfs will be slower than normal file access. The Linux file cache is impacted to achieve "consistency" between the host and container. Some applications that depend on the file cache for speed can slow down. PHP web apps with frameworks are hit in particular as they load all files on each request.

React is likely in a slightly better position as the file reads only happens once on startup, but those reads will still be slow each startup.

Anything that actively writes to a directory is just going to be slower.

Workarounds

Caching

Some caching options were added to mounts in Docker 17.06 so users can control the mounts beyond the default 'consistent' level where all reads are passed out to OSX from the container.

It's likely the node_modules directory is the main cause of slowness, it's also the safest place to enable caching on as it doesn't change often.

This setup can get verbose depending on your directory structure as you have to mount each item in your app directory independently:

docker run --rm -ti \
  --link api-container:api \
  --name my-container -p 3000:3000 \
  -v $(pwd)/index.js:/var/www/app/index.js \
  -v $(pwd)/package.json:/var/www/app/package.json \
  -v $(pwd)/src:/var/www/app/src \
  -v $(pwd)/node_modules:/var/www/app/node_modules:cached \
  nxmohamad/my-container \
  bash

Syncing

The other option is using a tool like rsync or unison to keep a local volume in sync rather than relying on bind mounts from OSX or Windows.

A tool called docker-sync was written specifically for this. Getting a working configuration can be a bit difficult and it can get itself in a tangle sometimes (it's caused a couple of kernel oopses if I leave it running over a suspend) but it works in the end.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!