git hubにあるrailsアプリを、dockerで構築したrails環境で動かしたい
Q&A
Closed
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
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"]
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
#!/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 "$@"
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コマンド実行
$ 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
データベースは Postgresql
の 9.4.1
以上