私はrailsにてアプリケーションを作成している初学者です。
ローカル環境でDockerを用いたRailsアプリケーションを作成し、capistranoを用いてEC2へデプロイを行う際にエラーが発生し、解決に至れておりません。
なにかアドバイスを頂けないかと思い、質問をさせていただきました。
ご不明な点、資料が不足している点などありましたらお教えくださいますと幸いです。
お手数おかけしますが、よろしくお願いします。
解決したいこと
capistranoによるデプロイを行う為、下記コマンドを実行後にエラー
※capistrano導入前では手動でのデプロイはできておりました
% docker-compose run web bundle exec cap production deploy Starting fuuun_ramen_db_1 ... done Creating fuuun_ramen_web_run ... done #<Thread:0x0000557489338148@/usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true): Traceback (most recent call last): 12: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute' 11: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:31:in `run' 10: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:31:in `instance_exec' 9: from /usr/local/bundle/gems/capistrano-rbenv-2.2.0/lib/capistrano/tasks/rbenv.rake:10:in `block (3 levels) in <top (required)>' 8: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:61:in `test' 7: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute' 6: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `tap' 5: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute' 4: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/netssh.rb:130:in `execute_command' 3: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/netssh.rb:177:in `with_ssh' 2: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/connection_pool.rb:63:in `with' 1: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/backends/connection_pool.rb:63:in `call' /usr/local/bundle/gems/net-ssh-6.1.0/lib/net/ssh.rb:268:in `start': Authentication failed for user ec2-user@Elastic IP (Net::SSH::AuthenticationFailed) 1: from /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute' /usr/local/bundle/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as ec2-user@Elastic IP: Authentication failed for user ec2-user@Elastic IP (SSHKit::Runner::ExecuteError) (Backtrace restricted to imported tasks) cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@Elastic IP: Authentication failed for user ec2-user@Elastic IP Caused by: Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user@ElasticIP Tasks: TOP => rbenv:validate (See full trace by running task with --trace)
設定ファイル
- Capfile
ruby
1require "capistrano/setup" 2require "capistrano/deploy" 3require 'capistrano/rbenv' 4require 'capistrano/bundler' 5require 'capistrano/rails/assets' 6require 'capistrano/rails/migrations' 7require 'capistrano3/unicorn' 8Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
- config/deploy.rb
ruby
1lock "~> 3.14.1" 2set :application, "fuuun_ramen" 3set :repo_url, "git@github.com:<リポジトリ>.git" 4set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads') 5set :rbenv_type, :user 6set :rbenv_ruby, '2.6.5' 7set :ssh_options, auth_methods: ['publickey'], 8 keys: ['~/.ssh/fuuunramen.pem'] 9set :unicorn_pid, -> { "#{shared_path}/tmp/pids/unicorn.pid" } 10set :unicorn_config_path, -> { "#{current_path}/config/unicorn.rb" } 11set :keep_releases, 5 12after 'deploy:publishing', 'deploy:restart' 13namespace :deploy do 14 task :restart do 15 invoke 'unicorn:restart' 16 end 17end
- config/unicorn.rb
ruby
1app_path = File.expand_path('../../../', __FILE__) 2worker_processes 1 3working_directory "#{app_path}/current" 4pid "#{app_path}/shared/tmp/pids/unicorn.pid" 5listen "#{app_path}/shared/tmp/sockets/unicorn.sock" 6stderr_path "#{app_path}/shared/log/unicorn.stderr.log" 7stdout_path "#{app_path}/shared/log/unicorn.stdout.log" 8timeout 60 9 10preload_app true 11GC.respond_to?(:copy_on_write_friendly=) && GC.copy_on_write_friendly = true 12check_client_connection false 13run_once = true 14before_fork do |server, worker| 15 defined?(ActiveRecord::Base) && 16 ActiveRecord::Base.connection.disconnect! 17 if run_once 18 run_once = false # prevent from firing again 19 end 20 old_pid = "#{server.config[:pid]}.oldbin" 21 if File.exist?(old_pid) && server.pid != old_pid 22 begin 23 sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU 24 Process.kill(sig, File.read(old_pid).to_i) 25 rescue Errno::ENOENT, Errno::ESRCH => e 26 logger.error e 27 end 28 end 29end 30 31after_fork do |_server, _worker| 32 defined?(ActiveRecord::Base) && ActiveRecord::Base.establish_connection 33end
- config/deploy/production.rb
server '46.51.231.102', user: 'ec2-user', roles: %w{app db web} set :ssh_options, { keys: [File.expand_path('~/.ssh/fuuunramen.pem)')] }
試したこと
ローカル環境での実行コマンド「% docker-compose run web bundle exec cap production deploy」が前提として間違っているのかと思い、コンテナに接続して「myapp# bundle exec cap production deploy」と実行も行ってみましたが、エラー内容は同様でございました。
ネットから得た情報で「SSH鍵が消えてしまっている可能性がある為、SSH鍵を登録し直す」とありましたので、下記コマンドにて登録を行っておりますが、エラー解決に至れておりません。
% ssh-add ~/.ssh/fuuunramen.pem Identity added: /Users/kobayashitaku/.ssh/fuuunramen.pem (/Users/kobayashitaku/.ssh/fuuunramen.pem)
% docker-compose run web bundle exec ssh-add ~/.ssh/fuuunramen.pem Starting fuuun_ramen_db_1 ... done Creating fuuun_ramen_web_run ... done Could not open a connection to your authentication agent.
基本的なインフラ関連の知識や理解が不足しており、確認すべき箇所を掴めずにおります。
なにかお気づきの点などあればお教えくださいますと幸いです。
あなたの回答
tips
プレビュー