Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

git hubにあるrailsアプリを、dockerで構築したrails環境で動かしたい

git hubにあるrailsアプリを、dockerで構築したrails環境で動かしたい

クローンするアプリ

ruby 2.2.2
rails 4.2.1
db postgresql

ローカル環境
$ docker -v
Docker version 20.10.7, build f0df350
$ docker-compose -v
docker-compose version 1.29.2, build 5becea4c

参考にしたこちらのドキュメント(Quickstart: Compose and Rails)では,ローカル環境にGemfile, Gemfile.lock, entrypoint.shを作り、Docker内にCOPYしていますが、私の場合git hubからクローンしたrails アプリを動かしたいので、そのアプリをdocker内にCOPYすればと考えました。

しかし、docker-compose build 後の、docker-compose up がうまくいかず行き詰っています。

FATAL: database "my_app_development" does not exist
とあることから、データベースの命名かどこか記述に誤りがあるかだと予想しております。

また、git hub上のアプリをdocker環境で動かす際の、正攻法をご存じでしたら教えていただけると幸いです。
まだdockerを触り始めて日が浅いため、稚拙なミスをしていると思いますが、なにとぞご教授願います。

ディレクトリー構造
my-app --- rails-app(git hubからクローンしたもの)
       |- Dockerfile
       |- docker-compose.yml
       |- entrypoint.sh  
Dockerfile
FROM ruby:2.2.2

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

COPY rails-app /
WORKDIR /rails-app
RUN bundle install
# RUN rake db:create && rake db:migrate

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: "3"
services:
  db:
    image: postgres
    restart: always
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    restart: always
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/rails_app
    ports:
      - "3000:3000"
    depends_on:
      - db
entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /rails-app/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
myapp/rails-app/config/database.yml
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: rails_app_development


test:
  <<: *default
  database: rails_app_test

ocker-compose upコマンド実行

terminal
$ docker-compose up
Creating network "my-app_default" with the default driver
Creating my-app_db_1 ...
Creating my-app_db_1 ... done
Creating my-app_web_1 ...
Creating my-app_web_1 ... done
Attaching to my-app_db_1, my-app_web_1
db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2021-07-02 07:11:03.078 UTC [1] LOG:  starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2021-07-02 07:11:03.079 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2021-07-02 07:11:03.079 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2021-07-02 07:11:03.086 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2021-07-02 07:11:03.127 UTC [26] LOG:  database system was shut down at 2021-07-02 07:03:48 UTC
db_1   | 2021-07-02 07:11:03.175 UTC [1] LOG:  database system is ready to accept connections
web_1  | => Booting Thin
web_1  | => Rails 4.2.1 application starting in development on http://0.0.0.0:3000
web_1  | => Run `rails server -h` for more startup options
web_1  | => Ctrl-C to shutdown server
db_1   | 2021-07-02 07:11:06.514 UTC [33] FATAL:  database "my_app_development" does not exist
web_1  | /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect': FATAL:  database "my_app_development" does not exist (ActiveRecord::NoDatabaseError)
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
web_1  |        from /usr/local/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
web_1  |        from /usr/local/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:567:in `retrieve_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/model_schema.rb:230:in `table_exists?'
web_1  |        from /usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:175:in `attribute_method?'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/acceptance.rb:18:in `block in setup!'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/acceptance.rb:18:in `reject'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/acceptance.rb:18:in `setup!'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/acceptance.rb:7:in `initialize'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/with.rb:92:in `new'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/with.rb:92:in `block in validates_with'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/with.rb:91:in `each'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/with.rb:91:in `validates_with'
web_1  |        from /usr/local/bundle/gems/activemodel-4.2.1/lib/active_model/validations/acceptance.rb:51:in `validates_acceptance_of'
web_1  |        from /app/models/user.rb:5:in `<class:User>'
web_1  |        from /app/models/user.rb:1:in `<top (required)>'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `load'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:457:in `block in load_file'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:647:in `new_constants_in'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:456:in `load_file'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:354:in `require_or_load'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:494:in `load_missing_constant'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:184:in `const_missing'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:261:in `const_get'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:261:in `block in constantize'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `each'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `inject'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/inflector/methods.rb:259:in `constantize'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:566:in `get'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:597:in `constantize'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise.rb:302:in `get'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/mapping.rb:80:in `to'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/mapping.rb:75:in `modules'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/mapping.rb:92:in `routes'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/mapping.rb:159:in `default_used_route'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/mapping.rb:69:in `initialize'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise.rb:336:in `new'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise.rb:336:in `add_mapping'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/rails/routes.rb:224:in `block in devise_for'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `each'
web_1  |        from /usr/local/bundle/gems/devise-3.4.1/lib/devise/rails/routes.rb:223:in `devise_for'
web_1  |        from /config/routes.rb:2:in `block in <top (required)>'
web_1  |        from /usr/local/bundle/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:432:in `instance_exec'
web_1  |        from /usr/local/bundle/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:432:in `eval_block'
web_1  |        from /usr/local/bundle/gems/actionpack-4.2.1/lib/action_dispatch/routing/route_set.rb:410:in `draw'
web_1  |        from /config/routes.rb:1:in `<top (required)>'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:40:in `each'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:16:in `reload!'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/file_update_checker.rb:75:in `call'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/file_update_checker.rb:75:in `execute'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:27:in `updater'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `instance_exec'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/initializable.rb:30:in `run'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/initializable.rb:55:in `block in run_initializers'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `call'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
web_1  |        from /usr/local/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/initializable.rb:54:in `run_initializers'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/application.rb:352:in `initialize!'
web_1  |        from /config/environment.rb:5:in `<top (required)>'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
web_1  |        from /usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
web_1  |        from /config.ru:3:in `block in <main>'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/builder.rb:55:in `instance_eval'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/builder.rb:55:in `initialize'
web_1  |        from /config.ru:in `new'
web_1  |        from /config.ru:in `<main>'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/builder.rb:49:in `eval'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/builder.rb:49:in `new_from_string'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/builder.rb:40:in `parse_file'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/server.rb:299:in `build_app_and_options_from_config'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/server.rb:208:in `app'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/server.rb:61:in `app'
web_1  |        from /usr/local/bundle/gems/rack-1.6.1/lib/rack/server.rb:336:in `wrapped_app'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/server.rb:139:in `log_to_stdout'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/server.rb:78:in `start'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:80:in `block in server'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:75:in `tap'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:75:in `server'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
web_1  |        from /usr/local/bundle/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
web_1  |        from bin/rails:8:in `require'
web_1  |        from bin/rails:8:in `<main>'
web_1  | Exiting

追記

Ruby のバージョンは 2.2.0
データベースは Postgresql9.4.1 以上

0

3Answer

RailsのActiveRecord::NoDatabaseErrorですので、rails db:createしてないのではないでしょうか?

0Like

Comments

  1. @YusukeYamazaki

    Questioner

    # 返信遅くなりすみません。

    ```:terminal
    $ docker-compose run web rake db:create
    Creating my-app_web_run ... done
    (in /)
    PG::InvalidParameterValue: ERROR: invalid value for parameter "client_min_messages": "panic"
    HINT: Available values: debug5, debug4, debug3, debug2, debug1, log, notice, warning, error.
    : SET client_min_messages TO 'panic'
    ```

    rake db:createを走らせると、上記のエラーではじかれてしまいます。

Comments

  1. @YusukeYamazaki

    Questioner

    コメントアウトを解除すると、下記のようなエラーが発生します。

    ```
    #10 [6/8] RUN rake db:create && rake db:migrate
    #10 sha256:cfb17ffbd1209e1b70307ac5e857ff99bf133444b8892f5e6ff323451ac6e2b4
    #10 0.480 (in /)
    #10 7.529 could not translate host name "db" to address: Name or service not known
    .
    .
    .
    #10 20.61 Tasks: TOP => db:migrate => environment
    #10 20.61 (See full trace by running task with --trace)
    #10 ERROR: executor failed running [/bin/sh -c rake db:create && rake db:migrate]: exit code: 1
    ------
    > [6/8] RUN rake db:create && rake db:migrate:
    ------
    executor failed running [/bin/sh -c rake db:create && rake db:migrate]: exit code: 1
    Service 'web' failed to build : Build failed
    ```
  2. あ、それはそうでした。

    build時点ではdbサービスは存在しないので、データベースにアクセスできるわけがなかった。返答コメントではmarkdownが機能してないので、再度コメントします。

以前も他の人に説明したんですが、Dockerfileを最初から全部完璧にしようとする必要はないと思いますし、全てを

$ docker-compose run web rake db:create

みたいにホスト側から行う必要もないと思います。

今回の場合なら最低限railsを動かせるようにして、

$ docker-compose run --rm -it web bash # これでコンテナ内に入る
$ rake db:create && rake db:migrate # コンテナ内からこれを実行

このrailsのコンテナ内に入ってデータベースの初期化をしておけば、次回起動以降からはそのコマンドは必要ないし、railsも素直に動くはずです。

PG::InvalidParameterValue: ERROR: invalid value for parameter "client_min_messages": "panic"

0Like

Comments

  1. @YusukeYamazaki

    Questioner

    全部、Dockerfileですべてのコマンドを実行する必要がないってことですね。
    最小構成で貴方にいただいたアドバイス通り、やってみます。
    ありがとうございました。

Your answer might help someone💌