如何加快Docker容器中的node.js反应启动 [英] How can I speed up node.js react startup in a Docker container

查看:105
本文介绍了如何加快Docker容器中的node.js反应启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Docker容器中运行node js官方映像,并且我注意到npm start命令的启动时间比在Docker外部时要长得多.

I am running node js official image inside Docker container and I noticed that the npm start command takes a lot longer to start than when it's outside of Docker.

是否可以更改一些设置以使其运行更快?也许会为容器分配更多的内存?

Are there settings that I can change to make it run faster? Perhaps allocating more memory to the container?

作为参考,我将在下面粘贴相关文件.

For reference I will paste relevant files below.

Dockerfile:

Dockerfile:

FROM node:8.1

WORKDIR var/www/app

# Global install yarn package manager
RUN apt-get update && apt-get install -y curl apt-transport-https && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && apt-get install -y yarn

RUN npm install -g create-react-app

我用来启动容器的命令:

The command I use to start my container:

docker run --rm -ti \
--link api-container:api \
--name my-container -p 3000:3000 \
-v $(pwd):/var/www/app nxmohamad/my-container \
bash

,开始脚本只是NODE_PATH=. react-scripts start

推荐答案

使用 osxfs 比正常文件访问速度慢. Linux文件缓存受到影响,以实现主机和容器之间的一致性".某些依赖文件高速缓存来提高速度的应用程序可能会减慢速度.带有框架的PHP Web应用程序在每次请求加载所有文件时尤其受到打击.

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.

一些已将缓存选项添加到安装在Docker 17.06 中使用,因此用户可以控制超出默认一致"级别的安装,在默认一致"级别上,所有读取都将从容器传递到OSX.

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.

node_modules目录很可能是速度慢的主要原因,它也是启用缓存的最安全的位置,因为它不经常更改.

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

正在同步

另一种选择是使用rsyncunison之类的工具来保持本地卷同步,而不是依靠OSX或Windows的绑定挂载.

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.

专门为此编写了一个名为 docker-sync 的工具.获得有效的配置可能会有些困难,并且有时可能使自己陷入混乱(如果我让它运行在挂起状态,则可能会导致几次内核崩溃),但最终它仍然可以工作.

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.

这篇关于如何加快Docker容器中的node.js反应启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆