実現したいこと前提
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も確認しましたが、デバッグに役立ちそうなログはないように思えました。
該当のソースコード
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に記載させていただいてます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/10 04:35
2021/05/11 07:22
2021/05/11 07:57 編集
2021/05/11 08:00