【问题标题】:Docker (rails, postgresql) hanging when started, rather than connecting to DBDocker(rails,postgresql)在启动时挂起,而不是连接到数据库
【发布时间】:2019-12-07 09:48:13
【问题描述】:

我是 docker 新手,正在尝试了解为什么我的 Docker 设置挂起并且没有像我期望的那样连接。

我在跑步

  • Docker 版本 18.09.2,内部版本 6247962
  • docker-compose 版本 1.23.2,构建 1110ad01
  • OSX 10.14.5

我的设置基于我找到的 Gist

为了更好地展示问题,我已经稍微减少了它。

Dockerfile

FROM ruby:2.4

ARG DEBIAN_FRONTEND=noninteractive

RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" >> /etc/apt/sources.list.d/postgeresql.list \
 && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
 && apt-get update                                     \
 && apt-get update                                     \
 && apt-get install -y --no-install-recommends apt-utils \
 && apt-get install -y build-essential \
 && apt-get install -y nodejs \
 && apt-get install -y --no-install-recommends         \
   postgresql-client-9.6 pv ack-grep ccze unp htop vim \
 && apt-get install -y libxml2-dev libxslt1-dev \
 && rm -rf /var/lib/apt/lists/*                        \
 && apt-get purge -y --auto-remove

# Set environment
ENV APP_HOME /usr/src/app
ENV BUNDLER_VERSION 2.0.2

# Setup bundler
RUN gem install bundler -v $BUNDLER_VERSION

WORKDIR $APP_HOME

EXPOSE 7051

CMD ["bundle", "exec", "puma", "-p", "7051", "-C", "config/puma.rb"]

docker_compose.yml

version: '3.1'
services:
  app: &app_base
    build: .
    working_dir: /usr/src/app
    volumes:
      - .:/usr/src/app
      # to be able to forward ssh-agent to github through capistrano (bundle on server)
      - "~/.ssh/id_rsa:/root/.ssh/id_rsa"
      - $SSH_AUTH_SOCK:$SSH_AUTH_SOCK
    environment: &app_environment
      # to keep bundle effect between container restarts (without rebuild):
      BUNDLE_PATH: /usr/src/app/.bundle
      BUNDLE_APP_CONFIG: /usr/src/app/.bundle
      DATABASE_HOST: db
      SSH_AUTH_SOCK: # this left empty copies from outside env
    env_file: '.env'
    ports:
      - "7051:7051"
    depends_on:
      - db

  db:
    image: postgres:9.5.17
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: my_project_development
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root

config/database.yml

development:
  adapter: postgresql
  encoding: unicode
  pool: 5
  database: my_project_development
  username: root
  password: root
  host: db

config/puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests, default is 3000.
#
port        ENV.fetch("PORT") { 7051 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

所以我正在做的是:

  1. 运行 docker-compose build 以首先构建映像和容器
  2. 运行 docker-compose run --rm app bundle install 来安装 gems
  3. 运行 docker-compose run --rm app bundle exec rake db:create db:migrate db:seed 以创建/迁移/种子数据库

第 3 步是我坚持的步骤。它只是挂在那里没有反馈:

docker-compose run --rm app bundle exec rake db:create db:migrate db:seed
Starting my_project_db_1 ... done

我知道数据库正在运行,因为我可以在本地连接到它。

我也可以登录app容器,通过psql连接,所以我知道app容器可以和db容器对话:

docker exec -it f6d6edadaed4 /bin/bash                                                                                       (52s 699ms)
root@f6d6edadaed4:/usr/src/app# psql "postgresql://root:root@db:5432/my_project_development"
psql (9.6.14, server 9.5.17)
Type "help" for help.

my_project_development=# \dt
No relations found.

如果我尝试使用docker-compose up 启动应用程序,那么它也会挂起:

app_1  | Puma starting in single mode...
app_1  | * Version 3.11.4 (ruby 2.4.6-p354), codename: Love Song
app_1  | * Min threads: 5, max threads: 5
app_1  | * Environment: ci

即puma 通常会在连接后显示“正在侦听”消息:

* Listening on tcp://0.0.0.0:7051
Use Ctrl-C to stop

但它没有达到那个地步,它只是挂起。

会发生什么?为什么我的 Rails 容器不能直接连接到 PostgreSQL 容器并让 puma 正常启动?

更多信息:

我现在知道了,如果我等待 10 多分钟,它最终会启动!

在那 10 分钟内,我的 CPU 粉丝都在疯狂地旋转,所以它真的在思考什么。

但是当它完成时,CPU 风扇关闭,puma 已启动,我可以在本地访问它,地址为 http://127.0.0.1:7051,就像我期望的那样。

为什么启动这么慢?我的机器在其他方面非常快。

【问题讨论】:

    标签: ruby-on-rails postgresql docker puma


    【解决方案1】:

    我认为 OSX 上的 Docker 非常慢。从那以后我读到了一些性能问题here

    向卷添加cached 选项似乎已将启动时间缩短至约 2 分钟

    version: '3.1'
    services:
      app: &app_base
        build: .
        working_dir: /usr/src/app
        volumes:
          - .:/usr/src/app:cached
        ...
    

    在我看来仍然不是很可接受。想知道还有什么可以做的吗?

    【讨论】:

      【解决方案2】:

      我找到了一个实际可行的答案,我也在此处发布:https://stackoverflow.com/a/58603025/172973

      基本上,请参阅 the article here 了解如何正确设置 Dockerfiledocker-compose.yml,使其在 OSX 上运行良好。

      要了解的主要内容:

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

      因此,如果其他人遇到此问题,请按照文章或查看我的其他答案。

      【讨论】:

        猜你喜欢
        • 2020-07-11
        • 2012-07-21
        • 1970-01-01
        • 1970-01-01
        • 2017-07-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多