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

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

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

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

Docker

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

Q&A

解決済

2回答

1295閲覧

DockerでRuby on Railsの環境構築をしようとしているのですが、localhost:3000にアクセスするとMySQLに関するエラーが発生しており困っています

hitoyasablue

総合スコア8

Ruby on Rails 6

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

Docker

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

0グッド

0クリップ

投稿2020/06/26 17:11

編集2020/06/26 17:21

前提・実現したいこと

DockerでRuby on Railsの環境構築をしようとしております。
以下の順に作業を終えた後、/localhost:3000/にアクセスしたところ、下図のようなエラーが表示されました。

  • 作業ディレクトリを作成・移動
  • 作業ディレクトリでDockerFileの作成・編集
  • gemfileを作成・編集
  • 空のgemfile.lockを作成
  • docker-compose.ymlを作成・編集
  • rails newの実行
  • database.ymlを修正
  • Webpackerのインストール
  • Dockerの起動
  • DB作成
  • ブラウザでlocalhost:3000にアクセスする

また、下記のDockerfile, Gemfile, docker-compose.yml, database.ymlは以下の記事を参考に作成しております。

何卒よろしくお願いいたします。

発生している問題・エラーメッセージ

該当のソースコード

Dockerfile

1FROM ruby:2.7.1 2 3# 必要なパッケージのインストール 4RUN apt-get update -qq && \ 5 apt-get install -y build-essential \ 6 libpq-dev \ 7 nodejs 8 9# yarnパッケージ管理ツールをインストール 10RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ 11curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ 12echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ 13apt-get update && apt-get install -y yarn 14 15# Node.jsをインストール 16RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \ 17apt-get install nodejs 18 19# 作業ディレクトリの作成、設定 20RUN mkdir /app_name 21##作業ディレクトリ名をAPP_ROOTに割り当てて、以下$APP_ROOTで参照 22ENV APP_ROOT /app_name 23WORKDIR $APP_ROOT 24 25# ホスト側(ローカル)のGemfileを追加する 26ADD ./Gemfile $APP_ROOT/Gemfile 27ADD ./Gemfile.lock $APP_ROOT/Gemfile.lock 28 29# Gemfileのbundle install 30RUN bundle install 31ADD . $APP_ROOT

Gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.7.1' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 6.0.3', '>= 6.0.3.2' 8# Use mysql as the database for Active Record 9gem 'mysql2', '>= 0.4.4' 10# Use Puma as the app server 11gem 'puma', '~> 4.1' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 4.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Redis adapter to run Action Cable in production 21# gem 'redis', '~> 4.0' 22# Use Active Model has_secure_password 23# gem 'bcrypt', '~> 3.1.7' 24 25# Use Active Storage variant 26# gem 'image_processing', '~> 1.2' 27 28# Reduces boot times through caching; required in config/boot.rb 29gem 'bootsnap', '>= 1.4.2', require: false 30 31group :development, :test do 32 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 33 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 34end 35 36group :development do 37 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 38 gem 'web-console', '>= 3.3.0' 39 gem 'listen', '~> 3.2' 40 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 41 gem 'spring' 42 gem 'spring-watcher-listen', '~> 2.0.0' 43end 44 45group :test do 46 # Adds support for Capybara system testing and selenium driver 47 gem 'capybara', '>= 2.15' 48 gem 'selenium-webdriver' 49 # Easy installation and use of web drivers to run system tests with browsers 50 gem 'webdrivers' 51end 52 53# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 54gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 55

docker-compose.yml(すみません、なぜかコードのファイル名に記号を含めると、タイトル表示がされなくなるため、コードの上にファイル名を表記している次第です。)

docker

1version: '3' 2services: 3 db: 4 image: mysql:5.7 5 environment: 6 MYSQL_ROOT_PASSWORD: 7 MYSQL_DATABASE: root 8 ports: 9 - "3306:3306" 10 11 web: 12 build: . 13 command: rails s -p 3000 -b '0.0.0.0' 14 volumes: 15 - .:/app_name 16 ports: 17 - "3000:3000" 18 links: 19 - db

database.yml

database.yml

1# MySQL. Versions 5.5.8 and up are supported. 2# 3# Install the MySQL driver 4# gem install mysql2 5# 6# Ensure the MySQL gem is defined in your Gemfile 7# gem 'mysql2' 8# 9# And be sure to use new-style password hashing: 10# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html 11# 12default: &default 13 adapter: mysql2 14 encoding: utf8 15 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 16 username: root 17 password: 18 host: db # docker-compose.ymlのservice名 19 20development: 21 <<: *default 22 database: app_name_development 23 24# Warning: The database defined as "test" will be erased and 25# re-generated from your development database when you run "rake". 26# Do not set this db to the same as development or production. 27test: 28 <<: *default 29 database: app_name_test 30 31# As with config/credentials.yml, you never want to store sensitive information, 32# like your database password, in your source code. If your source code is 33# ever seen by anyone, they now have access to your database. 34# 35# Instead, provide the password as a unix environment variable when you boot 36# the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database 37# for a full rundown on how to provide these environment variables in a 38# production deployment. 39# 40# On Heroku and other platform providers, you may have a full connection URL 41# available as an environment variable. For example: 42# 43# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" 44# 45# You can use this database configuration with: 46# 47# production: 48# url: <%= ENV['DATABASE_URL'] %> 49# 50production: 51 <<: *default 52 database: app_name_production 53 username: app_name 54 password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %> 55

補足情報(FW/ツールのバージョンなど)

  • macOS Catalina 10.15.5

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

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

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

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

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

guest

回答2

0

ベストアンサー

手順のどれかが間違っているのでしょう。
まずは、dockerもmysqlも使わず、railsだけを動かすところから
始めてみてください。

投稿2020/06/26 23:51

technocore

総合スコア7225

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

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

hitoyasablue

2020/06/27 00:21

ご返信いただきありがとうございます。 ご助言の通りRailsだけで動かす前に、参考にしたQiitaの記事を作成した方のGithubにアクセスし、該当のコードと自分のコードの差分を確認し、コードを編集し直したところ、エラーは解消されました。MySQLのパスワードを空欄にしていたのが原因でした。 できることを全て行わずして質問に臨んでしまい、申し訳ありません。
guest

0

参考にしたQiitaの記事を作成した方のGithubにアクセスし、該当のコードと自分のコードの差分を確認し、コードを編集し直したところ、エラーは解消されました。MySQLのパスワードを空欄にしていたのが原因でした。

投稿2020/06/27 00:21

hitoyasablue

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問