任何rake命令都需要等待几分钟,然后才能在Docker For Mac容器中运行 [英] Any rake command is waiting few minutes before running within Docker For Mac container

查看:114
本文介绍了任何rake命令都需要等待几分钟,然后才能在Docker For Mac容器中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用2个容器db和rails应用程序运行简单的docker体系结构。与db相关的任何rake命令都很慢。就像 rake db:create rake db:migrate

Run simple docker architecture with 2 containers db and rails application. Any rake command related to db is very slow. Like rake db:create, rake db:migrate

尝试通过iperf测试2个容器之间的速度。它显示26-27 Gbits /秒。所以看起来好像不是网络问题。

Tried to test speed between 2 containers by iperf. It shows 26-27 Gbits/sec. So it looks like not network problem. And it is working like charm in any linux host.

Docker For Mac specs
MacOS Mojave 10.14.3;
Engine: 18.09.1;
Compose: 1.23.2;
Machine 0.16.1;

这里是示例docker-compose.yml

Here is sample docker-compose.yml

version: '3.7'
services:
  postgres_10_5:
    image: postgres:10.5
    ports:
      - "5432"
    networks:
      - backend


  web_app:
    build:
      context: .
      dockerfile: Dockerfile-dev
    env_file:
      - ./.env
    ports:
      - "3000:3000"
      - "1080:1080"
    environment:
      - RAILS_ENV=development
    volumes:
      - .:/home/app
    networks:
      - backend


networks:
  backend:
    driver: bridge

期望不要等待5分钟左右的任何rake命令的结果。不知道在哪里挖掘。有任何提示吗?

Expect not wait for result of any rake command around 5 minutes. Don't know where to dig down. Any hints?

推荐答案

我也遇到了同样的问题。这与Docker在OSX上的性能很差以及您如何在Docker中设置卷/装载有关。

I had this exact same issue too. It's to do with the very poor performance of Docker on OSX, and how you've setup your volumes/mounts in docker.

我发现这篇文章很好地概述了如何设置 Dockerfile docker-compose.yml for Rails,并使其实际执行正常。

I found this article that has a good overview of how to setup a Dockerfile and docker-compose.yml for Rails, and have it actually perform OK.

要理解的主要内容:


要使Docker在MacOS上足够快,请遵循以下两个规则:使用:cached挂载源文件并使用卷来生成内容(资产,捆绑包等)。

To make Docker fast enough on MacOS follow these two rules: use :cached to mount source files and use volumes for generated content (assets, bundle, etc.).

您尚未正确设置红宝石的卷宝石,PostgreSQL数据(可能还有其他东西)。

You haven't setup your volumes properly for ruby gems, postgresql data (and possibly other things).

Dockerfile 中需要的关键语句:

...

# Configure bundler and PATH
ENV LANG=C.UTF-8 \
  GEM_HOME=/bundle \
  BUNDLE_JOBS=4 \
  BUNDLE_RETRY=3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \
  BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH

# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
    gem install bundler:$BUNDLER_VERSION

# Create a directory for the app code
RUN mkdir -p /app
...

在您的 docker-compose.yml

version: '3.7'
postgres_10_5:
    image: postgres:10.5
    volumes:
      - postgresql:/var/lib/postgresql/data
    ports:
      - "5432"
web_app:
    build:
      context: .
      dockerfile: Dockerfile-dev
    env_file:
      - ./.env
    stdin_open: true
    tty: true
    volumes:
      - .:/app:cached
      - rails_cache:/app/tmp/cache
      - bundle:/bundle
    environment:
      - RAILS_ENV=${RAILS_ENV:-development}
    depends_on:
      - postgres_10_5
volumes:
  postgres:
  bundle:
  rails_cache:

请参见文章,对它们的工作原理进行了更深入的讨论。

See the article for a more in-depth discussion on how it all works.

这篇关于任何rake命令都需要等待几分钟,然后才能在Docker For Mac容器中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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