質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Capistrano

Rubyで書かれたサーバオーケストレーションで、複数のサーバでスクリプトを実行する際に用いられます。主な使用用途はWebアプリケーションのデプロイメントです。 アプリケーションのバージョンアップ自動化、およびデータベースの変更などもできます。

Q&A

解決済

1回答

1309閲覧

Exception while executing as ec2-user@18.179.97.156: Authentication failed for user ec2-user@18.179.

divclass123

総合スコア35

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Capistrano

Rubyで書かれたサーバオーケストレーションで、複数のサーバでスクリプトを実行する際に用いられます。主な使用用途はWebアプリケーションのデプロイメントです。 アプリケーションのバージョンアップ自動化、およびデータベースの変更などもできます。

0グッド

0クリップ

投稿2021/05/09 04:21

編集2021/05/09 04:43

実現したいこと前提

docker-compose exec web bundle exec cap production deploy

実行時のエラーを解決したい。

現在自分は、Railsアプリをdocker開発環境上で開発をしていて、自動デプロイができない状態です。

エラー内容

#<Thread:0x0000557f0c263870@/usr/local/bundle/gems/sshkit-1.21.2/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.2/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute' 11: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:31:in `run' 10: from /usr/local/bundle/gems/sshkit-1.21.2/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.2/lib/sshkit/backends/abstract.rb:61:in `test' 7: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `create_command_and_execute' 6: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `tap' 5: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/abstract.rb:148:in `block in create_command_and_execute' 4: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:130:in `execute_command' 3: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/netssh.rb:177:in `with_ssh' 2: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/backends/connection_pool.rb:63:in `with' 1: from /usr/local/bundle/gems/sshkit-1.21.2/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@18.179.97.156 (Net::SSH::AuthenticationFailed) 1: from /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute' /usr/local/bundle/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as ec2-user@18.179.97.156: Authentication failed for user ec2-user@18.179.97.156 (SSHKit::Runner::ExecuteError) (Backtrace restricted to imported tasks) cap aborted! SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@18.179.97.156: Authentication failed for user ec2-user@18.179.97.156 Caused by: Net::SSH::AuthenticationFailed: Authentication failed for user ec2-user@18.179.97.156 Tasks: TOP => rbenv:validate (See full trace by running task with --trace)

dockerのwebコンテナ(railsとか)にsshキーがないことが問題で、
ローカルディレクトリのsshキーをwebコンテナのどこかに配置すればいい気がします。

Dockerfile,docker-compose.ymlのどちらにマウントするコードを書くのか。
webコンテナのどこにマウントすればいいのかわかっていません。

docker-compose.ymlに

volumes: - ~/.ssh/coffee-passport-ssh-key.pem:どこにマウントしたらいいかわからん

とこんな感じで書けばいいのでしょうか?
でもヴォリュームマウントしていいのかもわかりません。

 あと、18.179.97.156にアクセスすると
something went wrongになるので、もしかしたらそれも関係あるかもしれません。
production.logも確認しましたが、デバッグに役立ちそうなログはないように思えました。

該当のソースコード

自分のgithub

docker-compose.yml

version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_HOST: db ports: - '3306:3306' volumes: - ./tmp/db:/var/lib/mysql web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/coffee_passport ports: - "3000:3000" depends_on: - db environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} RAILS_MASTER_KEY: ${RAILS_MASTER_KEY} SENDGRID_API_KEY: ${SENDGRID_API_KEY} ADMIN_USER_PASSWORD: ${ADMIN_USER_PASSWORD} AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID} AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_ACCESS_KEY} GOOGLE_USER_NAME: ${GOOGLE_USER_NAME} GOOGLE_PASSWORD: ${GOOGLE_PASSWORD} SENDGRID_USER_NAME: ${SENDGRID_USER_NAME} SENDGRID_PASSWORD: ${SENDGRID_PASSWORD} PAYJP_SECRET_KEY: ${PAYJP_SECRET_KEY} PAYJP_PUBLIC_KEY: ${PAYJP_PUBLIC_KEY} MYSQL_HOST: db # selenium_chrome を使うために以下の行を追加 SELENIUM_DRIVER_URL: http://selenium_chrome:4444/wd/hub" selenium_chrome: image: selenium/standalone-chrome-debug logging: driver: none

Dockerfile

FROM ruby:2.6.5 ## nodejsとyarnはwebpackをインストールする際に必要 # yarnパッケージ管理ツールをインストール RUN curl http://deb.debian.org/debian/dists/buster/main/binary-amd64/by-hash/SHA256/935deda18d5bdc25fb1813d0ec99b6e0e32a084b203e518af0cf7dc79ee8ebda | head RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ apt-get update && apt-get install -y yarn # chromeの追加 RUN apt-get update && apt-get install -y unzip && \ CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \ wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/ && \ unzip ~/chromedriver_linux64.zip -d ~/ && \ rm ~/chromedriver_linux64.zip && \ chown root:root ~/chromedriver && \ chmod 755 ~/chromedriver && \ mv ~/chromedriver /usr/bin/chromedriver && \ sh -c 'wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -' && \ sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' && \ apt-get update && apt-get install -y google-chrome-stable RUN /bin/sh -c /bin/sh -c bundle update --bundler RUN gem install bundler:2.1.4 RUN mkdir /coffee_passport WORKDIR /coffee_passport COPY Gemfile /coffee_passport/Gemfile COPY Gemfile.lock /coffee_passport/Gemfile.lock RUN bundle update rails RUN bundle update RUN bundle update mimemagic RUN bundle update capybara selenium-webdriver #RUN bundle update nokogiri marcel mimemagic RUN bundle install COPY . /coffee_passport RUN yarn install --check-files RUN bundle exec rails webpacker:compile # 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 # Start the main process. CMD ["rails", "server", "-b", "0.0.0.0"]

production.rb

server '18.179.97.156', user: 'ec2-user', roles: %w{app db web} # server-based syntax # ====================== # Defines a single server with a list of roles and multiple properties. # You can define all roles on a single server, or split them: # server "example.com", user: "deploy", roles: %w{app db web}, my_property: :my_value # server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value # server "db.example.com", user: "deploy", roles: %w{db} # role-based syntax # ================== # Defines a role with one or multiple servers. The primary server in each # group is considered to be the first unless any hosts have the primary # property set. Specify the username and a domain or IP for the server. # Don't use `:all`, it's a meta role. # role :app, %w{deploy@example.com}, my_property: :my_value # role :web, %w{user1@primary.com user2@additional.com}, other_property: :other_value # role :db, %w{deploy@example.com} # Configuration # ============= # You can set any configuration variable like in config/deploy.rb # These variables are then only loaded and set in this stage. # For available Capistrano configuration variables see the documentation page. # http://capistranorb.com/documentation/getting-started/configuration/ # Feel free to add new variables to customise your setup. # Custom SSH Options # ================== # You may pass any option but keep in mind that net/ssh understands a # limited set of options, consult the Net::SSH documentation. # http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start # # Global options # -------------- # set :ssh_options, { # keys: %w(/home/user_name/.ssh/id_rsa), # forward_agent: false, # auth_methods: %w(password) # } # # The server-based syntax can be used to override options: # ------------------------------------ server '18.179.97.156', user: "ec2-user", roles: %w{web app}, ssh_options: { user: "ec2-user", # overrides user setting above keys: %w(~/.ssh/coffee-passport-ssh-key.pem), forward_agent: true, # auth_methods: %w(publickey password) # password: "please use keys" }

試したこと

私の行った手順は以下です。
config/deploy/production.rbの記述を変更

server '18.179.97.156', user: "ec2-user", roles: %w{web app}, ssh_options: { user: "ec2-user", # overrides user setting above keys: %w(~/.ssh/coffee-passport-ssh-key.pem), forward_agent: true, # auth_methods: %w(publickey password) # password: "please use keys" }

このようなことを記述しましたが、上記エラーと変化なしでした。

(2) chmod 600 ~/.ssh/coffee-passport-ssh-key.pem
を実行。
秘密鍵の権限が過剰とかググったらでたので、上記のコマンドを実行しましたが、結果は上記のエラーと変わらずでした。

環境

Mac OSで、 バージョンはgithubのreadMeに記載させていただいてます。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

capistranoを実行しているのはwebのコンテナだと思いますが、そのwebのコンテナ上で秘密鍵を参照できてないからじゃないですかね。
webコンテナのどこかにボリュームマウントして、そのボリュームマウントしたところに秘密鍵を置いて、配置した先のパスを参照するようにcapistranoの設定ファイルを編集すれば良さそうです。
もしデプロイ先の環境でもコンテナイメージを使って動かすのであれば、間違ってもコンテナ内に鍵をコピーしてはいけません。

ただ、個人的にはわざわざcapistranoをコンテナ内で実行せずに、ローカルで普通にcapistranoを実行すればいいのでは?とも思いますね…。

投稿2021/05/09 07:57

yu_1985

総合スコア7427

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

divclass123

2021/05/10 04:35

おっしゃる通りで、そもそも実行するコマンドが docker-compose exec web bundle exec cap production deploy ではなく、bundle exec cap production deploy でした。 そこから色々試行錯誤して、新たな質問を投稿したのですが、 そちらにも御回答いただきありがとうございます。
divclass123

2021/05/11 07:22

不勉強で申し訳ないのですが、 https://qiita.com/shun0211/items/6d4dab0698bcc0178e46 この記事にある通り、 ``` docker container cp ~/.ssh/<秘密鍵名> <コンテナ名>:/root/.ssh ``` こんな感じのコマンドを間違っても打っては行けないということでしょうか? また、それは何故でしょうか? ご教授いただければ幸いです。
yu_1985

2021/05/11 07:57 編集

> もしデプロイ先の環境でもコンテナイメージを使って動かすのであれば、 と、書きました。 そのコンテナイメージをローカルでしか使わないのならさほど問題はありません。 が、秘密鍵をあちこちコピーするよりは既に持っているものを参照したほうがどちらかと言えばよいとは思います。
yu_1985

2021/05/11 08:00

なぜダメと言ったかというと、秘密鍵が漏れたら他の何かで制限していない限りその秘密鍵を使ってSSHログインできるサーバに入られてしまうからですね。 なのでイメージにコピーしてしまった場合、もしそのイメージを他のところで使おうとすると他の人にイメージを通じて鍵が渡ってしまいます。 前述の通り、そのイメージを確実にローカルでしか使わないのであれば問題はありませんが…。 秘密鍵は漏れたら致命的なので運用には十分注意する必要があります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問