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

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

新規登録して質問してみよう
ただいま回答率
85.48%
docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

Docker

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

Q&A

解決済

2回答

1832閲覧

docker-compose run web rails db:create

divclass123

総合スコア35

docker-compose

docker-composeとは、複数のコンテナで構成されるサービスを提供する手順を自動的し管理を簡単にするツール。composeファイルを使用しコマンド1回で設定した全サービスを作成・起動することが可能です。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

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

Docker

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

0グッド

0クリップ

投稿2021/03/08 22:19

編集2021/03/11 06:19

前提・実現したいこと

docker-compose run web rails db:create

を実行したら

Creating coffee_passport_web_run ... done Access denied for user 'root'@'172.23.0.4' (using password: NO) Couldn't create 'coffee_passport_development' database. Please check your configuration.

と、エラーが出ました

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

エラーメッセージ

該当のソースコード

version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: 生のパスワード ports: - '3306:3306' command: --default-authentication-plugin=mysql_native_password 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
default: &default adapter: mysql2 encoding: utf8 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: <%= ENV['MYSQL_PASSWORD'] %> socket: /tmp/mysql.sock host: db development: <<: *default database: coffee_passport_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: coffee_passport_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 as a unix environment variable when you boot # the app. Read https://guides.rubyonrails.org/configuring.html#configuring-a-database # for a full rundown on how to provide these environment variables in a # production deployment. # # On Heroku and other platform providers, you may have a full connection URL # available as an environment variable. For example: # # DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" # # You can use this database configuration with: # # production: # url: <%= ENV['DATABASE_URL'] %> # production: <<: *default database: coffee_passport_production username: root password: <%= ENV['MYSQL_PASSWORD'] %> socket: /var/lib/mysql/mysql.sock

試したこと

MYSQL_ROOT_PASSWORD: <%= ENV['DATABASE_PASSWORD'] %>

を上記の通りに追記したり、
username: root
password: <%= ENV['DATABASE_PASSWORD'] %>
をdatabase.ymlに追加したりしました。

DARABASE_PASSWORDをdbコンテナの環境変数にも設定しようと

docker-compose run web -e DATABASE_PASSWORD=hogehoge

を実行したら

/usr/bin/entrypoint.sh: line 8: exec: -e: invalid option
exec: usage: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]

とエラーが出ました。

db:
image: mysql:5.7
に変更したら

Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)

とエラーが出ました。

docker-compose.ymlの

MYSQL_ROOT_PASSWORD: 生のパスワード

を打ち込んで

docker-compose run web rails db:create

を実行しても

Access denied for user 'root'@'172.26.0.4' (using password: NO)

とエラーが出ました

docker-compose down
rm -rf tmp/db
docker-compose up -d --build

と打ち込んでも

エラー内容が

soichirohara@SoichironoMBP coffee_passport % docker-compose run web rails db:create Creating coffee_passport_web_run ... done Access denied for user 'root'@'172.21.0.4' (using password: NO) Couldn't create 'coffee_passport_development' database. Please check your configuration. rails aborted! Mysql2::Error::ConnectionError: Access denied for user 'root'@'172.21.0.4' (using password: NO) /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect' /usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:887:in `new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:910:in `try_to_checkout_new_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:871:in `acquire_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:593:in `checkout' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:437:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1119:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:221:in `retrieve_connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/connection_handling.rb:189:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:126:in `create' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `each' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/tasks/database_tasks.rb:184:in `create_current' /usr/local/bundle/gems/activerecord-6.0.3.4/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <main>' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands/rake/rake_command.rb:20:in `perform' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/command.rb:48:in `invoke' /usr/local/bundle/gems/railties-6.0.3.4/lib/rails/commands.rb:18:in `<main>' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /usr/local/bundle/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency' /usr/local/bundle/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require' /coffee_passport/bin/rails:9:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call' /usr/local/bundle/gems/spring-2.1.1/lib/spring/client.rb:30:in `run' /usr/local/bundle/gems/spring-2.1.1/bin/spring:49:in `<top (required)>' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load' /usr/local/bundle/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>' /coffee_passport/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:create (See full trace by running task with --trace)

と変わりませんでした。

もちろんパスワードはあってるはずです。
mysql -u root -pで打ったときにそのパスワードを打ったらちゃんとログイン出来なたので、

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

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

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

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

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

guest

回答2

0

まず、Docker ComposeのYAMLはRubyの記法は使えません。したがって、Docker ComposeのYAML上で"MYSQL_ROOT_PASSWORD: <%= ENV['DATABASE_PASSWORD'] %>"とセットすることはできません。

docker-compose run web -e DATABASE_PASSWORD=hogehoge
とありますが、database.ymlにはrootユーザーをしていますので、DATABASE_PASSWORDではなくMYSQL_ROOT_PASSWORDが使われるはずです。このへんはMySQLの公式Dockerイメージのドキュメントを読み込むと書いてあると思います。

投稿2021/03/09 12:48

inductor

総合スコア428

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

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

0

自己解決

docker-compose down
docker rm -rf tmp/db

database.ymlには生のパスワードを設定

docker-compose.yml

version: '3' services: db: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: 生のパスワード 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
docker-compose up -d --build

mysqlを5.7にして、環境変数から読み込まず生のパスワードにしたら上手くいった。
生のパスワードじゃまずいので、どうやって環境変数から読み込ませるかが、課題。

投稿2021/03/11 13:44

divclass123

総合スコア35

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問