【发布时间】: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
所以我正在做的是:
- 运行
docker-compose build以首先构建映像和容器 - 运行
docker-compose run --rm app bundle install来安装 gems - 运行
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