使用 2 个容器数据库和 Rails 应用程序运行简单的 docker 架构。任何与 db 相关的 rake 命令都非常慢。如rake db:create
、rake db:migrate
尝试通过 iperf 测试 2 个容器之间的速度。它显示 26-27 Gbits/秒。所以看起来不是网络问题。它在任何 Linux 主机上都可以正常工作。
Docker For Mac specs
MacOS Mojave 10.14.3;
Engine: 18.09.1;
Compose: 1.23.2;
Machine 0.16.1;
这是示例 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 命令的结果。不知道该往哪里挖。有什么提示吗?
最佳答案
我也遇到了同样的问题。这与 Docker 在 OSX 上的性能非常差以及您在 docker 中设置卷/挂载的方式有关。
我找到了this article它很好地概述了如何为 Rails 设置 Dockerfile
和 docker-compose.yml
,并使其实际正常运行。
要理解的主要内容:
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.).
您还没有为 ruby gems、postgresql 数据(以及可能的其他数据)正确设置卷。
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:
参见the article更深入地讨论这一切是如何运作的。
关于ruby-on-rails - 任何 rake 命令在 Docker For Mac 容器中运行之前都会等待几分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54638073/