PF作成中の初学者です。
現状
開発環境において既存のRailsアプリにDockerを導入する方法(Rails、nginx、mysql)
こちらのサイトを参考に、Webサーバーにnginx、アプリケーションサーバーにpumaを使うようなDocker開発環境を構築しました。
これを、新しくRDSも利用しつつAWSに本番環境でデプロイすべく(たとえdocker-compose stopしてもブラウザで開けるようになりたい)主に以下の記事を参考に模索している状況です。
無料!かつ最短?で Ruby on Rails on Docker on AWS のアプリを公開するぞ。
問題
ec2
1docker-compose exec app rails db:create RAILS_ENV=production
を実行すると以下のエラーが出て失敗します。
ec2
1Couldn't create 'home-study-mysql' database. Please check your configuration. 2rails aborted! 3Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'home-study-mysql.cwljcv2jcmtr.ap-northeast-1.rds.amazonaws.com' (110 "Connection timed out") 4/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' 5/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' 6/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' 7/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' 8/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' 9/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' 10/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection' 11/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' 12/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' 13/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' 14/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' 15/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection' 16/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection' 17/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection' 18/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create' 19/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:119:in `create' 20/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' 21/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' 22/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:313:in `each' 23/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' 24/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:138:in `create_current' 25/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>' 26/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' 27/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' 28/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke' 29/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>' 30/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' 31/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' 32/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 33/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' 34/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' 35/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require' 36/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency' 37/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require' 38/myapp/bin/rails:9:in `<top (required)>' 39/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' 40/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' 41/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' 42/usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' 43/usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' 44/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' 45/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' 46/myapp/bin/spring:15:in `<top (required)>' 47bin/rails:3:in `load' 48bin/rails:3:in `<main>' 49Tasks: TOP => db:create 50(See full trace by running task with --trace)
関連するファイル
Dockerfile
1FROM ruby:2.5.3 2 3RUN apt-get update -qq && \ 4 apt-get install -y build-essential \ 5 libpq-dev \ 6 nodejs \ 7 vim 8RUN apt-get update && apt-get install -y unzip && \ 9 CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \ 10 wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \ 11 unzip ~/chromedriver_linux64.zip -d ~/ && \ 12 rm ~/chromedriver_linux64.zip && \ 13 chown root:root ~/chromedriver && \ 14 chmod 755 ~/chromedriver && \ 15 mv ~/chromedriver /usr/bin/chromedriver && \ 16 sh -c 'wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -' && \ 17 sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \ 18 apt-get update && apt-get install -y google-chrome-stable 19 20RUN apt-get install -y vim 21 22RUN mkdir /myapp 23 24WORKDIR /myapp 25 26ADD Gemfile /myapp/Gemfile 27ADD Gemfile.lock /myapp/Gemfile.lock 28 29RUN gem install bundler 30RUN bundle install 31 32ADD . /myapp 33 34RUN mkdir -p tmp/sockets 35RUN mkdir -p tmp/pids
docker-compose.yml↓
version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: password MYSQL_USER: user MYSQL_PASSWORD: 1234 MYSQL_DATABASE: myapp_db volumes: - mysql-data:/var/lib/mysql ports: - "4306:3306" app: build: . command: bundle exec puma -C config/puma.rb volumes: - .:/myapp - public-data:/myapp/public - tmp-data:/myapp/tmp - log-data:/myapp/log web: build: context: containers/nginx volumes: - public-data:/myapp/public - tmp-data:/myapp/tmp ports: - 80:80 volumes: mysql-data: public-data: tmp-data: log-data:
database.yml↓
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: <%= ENV.fetch('MYSQL_USER') { 'root' } %> password: <%= ENV.fetch('MYSQL_PASSWORD') { 'password' } %> host: db development: <<: *default database: myapp_development test: <<: *default database: myapp_test production: <<: *default adapter: mysql2 encoding: utf8mb4 charaset: utf8md4 collation: utf8mb4_general_ci host: <%= Rails.application.credentials.rds[:host] %> database: <%= Rails.application.credentials.rds[:database] %> username: <%= Rails.application.credentials.rds[:username] %> password: <%= Rails.application.credentials.rds[:password] %>
credentials.ymlには、二つ目の記事を参考に、
rds: host: RDSのエンドポイント database: RDSの「データベースの名前」 username: RDSの「マスターユーザの名前」 password: RDSの「マスターパスワード」
を書いて保存し、master.keyのローカルからec2への移行も済ませました。
このkeyが実は移行できていないのが原因ということもありえるでしょうか。
備考
あちこちのサイトのコードやコマンドをコピペしながら進めていたので、一部のファイル間で矛盾が起きていたりするのかもしれません。
圧倒的知識不足で恐縮なのですが、解決方法を教えて頂けますでしょうか。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。