前提・実現したいこと
現在DockerDesktopを用いrailsの環境構築を行っています。
railsアプリケーションを作成した後に"docker-compose run web rails db:create"を実行したところ、DBサーバーからアクセスを拒否されてしまいます。
発生している問題・エラーメッセージ
docker-compose run web rails db:createを実行するとwebサーバの接続が拒否されてしまう。
- ターミナル画面
Access denied for user 'app'@'172.19.0.3' (using password: NO) Couldn't create 'app_production' database. Please check your configuration. rails aborted! ActiveRecord::ConnectionNotEstablished: Access denied for user 'app'@'172.19.0.3' (using password: NO) Caused by: Mysql2::Error::ConnectionError: Access denied for user 'app'@'172.19.0.3' (using password: NO) Tasks: TOP => db:create (See full trace by running task with --trace)
- docker-compose logs dbを実行した結果
db_1 | 2021-09-06T13:49:19.141064Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 db_1 | 2021-09-06T13:49:19.141124Z 0 [Note] IPv6 is available. db_1 | 2021-09-06T13:49:19.141134Z 0 [Note] - '::' resolves to '::'; db_1 | 2021-09-06T13:49:19.141162Z 0 [Note] Server socket created on IP: '::'. db_1 | 2021-09-06T13:49:19.143849Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. db_1 | 2021-09-06T13:49:19.148721Z 0 [Note] Event Scheduler: Loaded 0 events db_1 | 2021-09-06T13:49:19.149551Z 0 [Note] mysqld: ready for connections. db_1 | Version: '5.7.35' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) db_1 | 2021-09-06T13:49:26.463107Z 2 [Note] Access denied for user 'app'@'172.19.0.3' (using password: NO) db_1 | 2021-09-06T14:04:31.237450Z 3 [Note] Access denied for user 'app'@'172.19.0.3' (using password: NO)
該当のソースコード
- Dockerfile
FROM ruby:2.7 ENV RAILS_ENV=production RUN 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 -qq \ && apt-get install -y nodejs yarn WORKDIR /app COPY ./src /app RUN bundle config --local set path 'vendor/bundle' \ && bundle install
- docker-compose.yml
version: '3' services: # db: # image: mysql:8.0 # command: --default-authentication-plugin=mysql_native_password # volumes: # - ./src/db/mysql_data:/var/lib/mysql # ports: # - "3306:3306" # environment: # MYSQL_ROOT_PASSWORD: password db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: password ports: - "3306:3306" web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - ./src:/app ports: - "3000:3000" depends_on: - db
- database.yml
FROM ruby:2.7 ENV RAILS_ENV=production RUN 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 -qq \ && apt-get install -y nodejs yarn WORKDIR /app COPY ./src /app RUN bundle config --local set path 'vendor/bundle' \ && bundle install
- docker-compose.yml
# MySQL. Versions 5.5.8 and up are supported. # # Install the MySQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: password host: db development: <<: *default database: app_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: app_test # As with config/credentials.yml, you never want to store sensitive information, # like your database password, in your source code. If your source code is # ever seen by anyone, they now have access to your database. # # Instead, provide the password or a full connection URL as an environment # variable when you boot the app. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # If the connection URL is provided in the special DATABASE_URL environment # variable, Rails will automatically merge its configuration values on top of # the values provided in this file. Alternatively, you can specify a connection # URL environment variable explicitly: # # production: # url: <%= ENV['MY_APP_DATABASE_URL'] %> # # Read https://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full overview on how database connection configuration can be specified. # production: <<: *default database: app_production username: app password: <%= ENV['APP_DATABASE_PASSWORD'] %>
試したこと
- 初めはwindowsとdockerのファイル共有関連でDB側にエラーがでていたため、volumeを削除し再実行しました。(docker-compose.ymlの上部コメントアウトの箇所です。)
- docker-compose.yml及びdatabese.ymlのpasswordの設定とdepend_on,hostの設定が間違っていないか確認しました。
- コンテナやイメージをpruneコマンドなどを用い作成しなおしました。
エラーメッセージの末尾に(using password: NO)とあるので、パスワードが必要なのにその入力ができていないのか?と予想しております。
補足情報(FW/ツールのバージョンなど)
- 実行環境
Windows10 Pro (21H1)
ubuntu 20.04LTS
DockerDesktop v1.21.4
- こちらの内容に沿って学習しておりました。
https://www.youtube.com/watch?v=ltDdZAJli8c&t=0s
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/06 23:44