【问题标题】:Rails view rendering is slow in DockerRails 视图渲染在 Docker 中很慢
【发布时间】:2019-12-18 08:20:41
【问题描述】:

我正在对 Rails 应用程序进行 dockerizing,但我不确定为什么视图呈现如此缓慢。在任何两个页面之间导航需要将近 2 秒,有些页面需要更长的时间。当我在 Docker 之外运行应用程序时,不存在此问题。

我在 2015 MBP 上使用 Docker Desktop,内存为 16GB。

这是我的 Dockerfile:

FROM ruby:2.5.3

# replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

RUN apt-get update -qq && apt-get install -y build-essential checkinstall \
    libpq-dev libvips-dev libvips-tools

# python dependencies
RUN apt-get install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

WORKDIR /tmp
RUN wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21.tar.gz && \
    tar xzf ghostscript-9.21.tar.gz && cd ghostscript-9.21 && \
    ./configure && make && make install

RUN wget https://nodejs.org/download/release/v10.8.0/node-v10.8.0.tar.gz && \
    tar xzf node-v10.8.0.tar.gz && cd node-v10.8.0 && \
    ./configure && make -j4 && make install

WORKDIR /app

RUN gem install bundler -v 1.17.3
RUN gem install foreman -v 0.85.0

RUN npm install -g yarn

这是我的 docker-compose.yml:

version: "3"

services:
  web:
    build: 
      context: .
      dockerfile: Dockerfile
    command: bash -c "rm -f /app/tmp/pids/server.pid && foreman start -f Procfile.dev"
    volumes:
      - .:/app
      - npm_packages:/app/node_modules
      - bundler_gems:/usr/local/bundle/
    ports:
      - 3000:3000
      - 8888:8888
    depends_on:
      - postgres
      - redis
      - mailcatcher
    environment:
      PGHOST: postgres
      PGUSER: postgres
      PGPASSWORD: "password"
      RAILS_ENV: development
      RACK_ENV: development
      NODE_ENV: development

  mailcatcher:
    build:
      context: .
      dockerfile: Dockerfile
    command: bash -c "gem install mailcatcher && mailcatcher --ip 0.0.0.0 --foreground"
    volumes:
      - .:/app
      - npm_packages:/app/node_modules
      - bundler_gems:/usr/local/bundle/
    ports:
      - 1080:1080
      - 1025:1025

  redis:
    image: redis
    ports:
      - 6379
    volumes:
      - redis:/data

  postgres:
    image: postgres:9.6
    ports:
      - 54320:5432
    restart: always
    volumes:
      - postgres:/var/lib/postgresql/data

    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: "password"

volumes:
  postgres:
  redis:
  npm_packages:
  bundler_gems:

一切正常,只是速度很慢。

duration=1623.74 view=1622.89

【问题讨论】:

  • Docker for Mac 中的绑定挂载为known to be very slow。如果您删除所有volumes:,应用程序会运行得更好吗? (除了/app 之外的两个还有一个副作用,就是阻止 Docker 看到已安装包的更新;在生产环境中你不希望这些更新。)
  • 我今天试试这个。我不在生产环境中运行 docker,所以没问题。当然,我很欣赏这些信息。老实说,我的 Dockerfile 中有一些其他的东西,而我没有挂载中的 gems/modules,但后来我发现了一篇博客文章,字面意思是“所有其他示例都不好,这是一个真实世界的示例来自实际用户”,并展示了使用坐骑进行操作。我只是在了解它……所以它的措辞似乎很权威。
  • 您能否指出您推荐的处理 Gemfile 和 package.json 文件的方法的方向?如果有的话,我很想走“黄金之路”。
  • 我发现 evilmartians.com/chronicles/… 效果很好。看来:cached 选项是解决方案。

标签: ruby-on-rails docker docker-compose


【解决方案1】:

这可能是在 Mac OS 上运行的 docker-compose 的一个已知问题。

尝试将行 127.0.0.1 localunixsocket.local 添加到文件 /etc/hosts

发现于:https://github.com/docker/compose/issues/3419#issuecomment-221793401

【讨论】:

  • 不幸的是,这并没有什么不同。不过谢谢!
猜你喜欢
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-25
  • 2013-05-19
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
相关资源
最近更新 更多