開発環境において既存のRailsアプリにDockerを導入する方法(Rails、nginx、mysql)
こちらのサイトを参考に、Webサーバーにnginx、アプリケーションサーバーにpumaを使うようなDocker開発環境を構築しました。
これをAWSにデプロイするために、
無料!かつ最短?で Ruby on Rails on Docker on AWS のアプリを公開するぞ。
こちらの記事を参考に、ec2にgit cloneしてdocker-compose buildを済ませた後、タイトルのエラーが発生しました。
ec2
1[sue@ip-10-0-0-239 home-study]$ docker-compose run app rails db:create 2Unknown MySQL server host 'db' (-2) 3Couldn't create 'myapp_development' database. Please check your configuration. 4rails aborted! 5Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2) 6/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' 7/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' 8/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new' 9/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection' 10/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection' 11/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection' 12/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' 13/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection' 14/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout' 15/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection' 16/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection' 17/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection' 18/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/connection_handling.rb:90:in `connection' 19/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection' 20/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create' 21/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:119:in `create' 22/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current' 23/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration' 24/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:313:in `each' 25/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration' 26/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/tasks/database_tasks.rb:138:in `create_current' 27/usr/local/bundle/gems/activerecord-5.2.4.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>' 28/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' 29/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' 30/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/command.rb:48:in `invoke' 31/usr/local/bundle/gems/railties-5.2.4.3/lib/rails/commands.rb:18:in `<top (required)>' 32/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' 33/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' 34/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' 35/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' 36/usr/local/bundle/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' 37/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `block in require' 38/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:257:in `load_dependency' 39/usr/local/bundle/gems/activesupport-5.2.4.3/lib/active_support/dependencies.rb:291:in `require' 40/myapp/bin/rails:9:in `<top (required)>' 41/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load' 42/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call' 43/usr/local/bundle/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call' 44/usr/local/bundle/gems/spring-2.1.0/lib/spring/client.rb:30:in `run' 45/usr/local/bundle/gems/spring-2.1.0/bin/spring:49:in `<top (required)>' 46/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load' 47/usr/local/bundle/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>' 48/myapp/bin/spring:15:in `<top (required)>' 49bin/rails:3:in `load' 50bin/rails:3:in `<main>' 51Tasks: TOP => db:create 52(See full trace by running task with --trace)
色々な記事を参考に進めていたのですが、一部のファイル間で参照先がずれていて矛盾が起きてしまっているのだと思います...
その他気になる点
・gitclone後にmaster.keyを紛失していることがわかり、EDITOR=vim rails credentials:editでcredentials.yml.encとmaster.keyの二つを再生成して再びgitclone→ローカルからec2へmaster.keyを渡すという操作を行っております。それも関係があるかもしれません。
・ec2内では/var/www/rails/ここにgit cloneしています。このディレクトリ順がdocker-compose.ymlとあっていないかもしれません。
・環境変数についてまだよくわかっていないのですが、docker-compose.ymlとdatabase.ymlの間でおかしなことになっているかもしれません。
恐れ入りますが、原因が分かる方教えていただけますでしょうか。
思い当たるファイル↓
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 36
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: - ./tmp/db:/var/lib/mysql/data 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: user password: 1234 host: db development: <<: *default database: myapp_development test: <<: *default database: myapp_test production: <<: *default database: <%= ENV['DB_DATABASE'] %> adapter: mysql2 encoding: utf8mb4 charset: utf8mb4 collation: utf8mb4_general_ci host: <%= ENV['DB_HOST'] %> username: <%= ENV['DB_USERNAME'] %> password: <%= ENV['DB_PASSWORD'] %>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/07/09 10:23
2020/07/09 10:34
2020/07/13 06:39
2020/07/13 07:16