

### 前提・実現したいこと

RailsアプリにDockerを導入しようとしています。  
エラーが起き、自力で解決できなかったので質問させていただきます。

現在ローカル側でdockerを起動しても以下のエラーが発生してしまいます。

[code]

    We're sorry, but something went wrong.
    If you are the application owner check the logs for more information.
    
[/code]

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

ターミナルでdockerを起動させ `$ docker-compose exec web bundle exec rake db:migrate`
のコマンドを叩いたところ以下のようなエラーが起きました。

[code]

    rake aborted!
    ActiveRecord::ConnectionNotEstablished: Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:45:in `rescue in new_client'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:39:in `new_client'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `mysql2_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:327:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:283:in `connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/tasks/database_tasks.rb:237:in `migrate'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:92:in `block (3 levels) in <main>'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `each'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `block (2 levels) in <main>'
    /usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
    /usr/local/bin/bundle:23:in `load'
    /usr/local/bin/bundle:23:in `<main>'
    
    Caused by:
    Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)
    /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.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:40:in `new'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:40:in `new_client'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:23:in `mysql2_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `public_send'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:926:in `checkout_new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:905:in `try_to_checkout_new_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:866:in `acquire_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:588:in `checkout'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:428:in `connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1128:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:327:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/connection_handling.rb:283:in `connection'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/tasks/database_tasks.rb:237:in `migrate'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:92:in `block (3 levels) in <main>'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `each'
    /usr/local/bundle/gems/activerecord-6.1.4/lib/active_record/railties/databases.rake:90:in `block (2 levels) in <main>'
    /usr/local/bundle/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
    /usr/local/bin/bundle:23:in `load'
    /usr/local/bin/bundle:23:in `<main>'
    Tasks: TOP => db:migrate
    (See full trace by running task with --trace)
    
[/code]

### 試したこと

  * [mysqlのsocketエラーでrailsアプリが起動できない](https://qiita.com/fujitora/items/d341c52706d1954cae28) などを参考にしながら、  
MySQLが起動できていることを確認。

  * databe.ymlに以下を追記するも変化なし。
[code]     socket: /run/mysqld/mysqld.sock

    
[/code]

必要な情報等あればお答えします。  
2日程度悩んでおります。お願い致します。(m1 macです。)

* * *

### 該当のソースコード

▼Dockerfile

[code]

    FROM ruby:2.7
    
    ENV RAILS_ENV=production
    
    RUN wget --quiet -O - /tmp/pubkey.gpg 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
    
    COPY start.sh /start.sh
    RUN chmod 744 /start.sh
    CMD ["sh", "/start.sh"]
    
[/code]

▼docker-compose.yml

[code]

    version: '3'
    services:
        db:
            image: mysql:8.0
            platform: linux/x86_64
            command: --default-authentication-plugin=mysql_native_password
            volumes:
                - ./src/db/mysql_data:/var/lib/mysql
            environment:
                MYSQL_ROOT_PASSWORD: password
        web:
            build: .
            command: bundle exec rails s -p 3000 -b '0.0.0.0'
            volumes: 
                - ./src:/app
            ports:
                - "3000:3000"
            depends_on:
                - db
    
[/code]

▼database.yml

[code]

    default: &default
      adapter: mysql2
      encoding: utf8mb4
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      username: root
      password: password
      host: db
    
    development:
      <<: *default
      database: app_development
    
    test:
      <<: *default
      database: app_test
    
    production:
      <<: *default
      database: <%= ENV['APP_DATABASE'] %>
      username: <%= ENV['APP_DATABASE_USERNAME'] %>
      password: <%= ENV['APP_DATABASE_PASSWORD'] %>
      host: <%= ENV['APP_DATABASE_HOST'] %>
    
[/code]


CC BY-SA 4.0
2021-08-19T15:41:59.567
80924
2021-08-20T11:46:45.590
2021-08-20T06:09:01.483
3060
36066
question
ruby-on-rails mysql docker docker-compose
docker-compose exec web bundle exec rake db:migrateをするとCan't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2) となってしまう


docker コンテナの web から db は仮想的に別のサーバーに見えてるはずなので

socket: /run/mysqld/mysqld.sock

はかいちゃだめです

web から MySQL ホスト名は db という名前でみえるはずなので  
ENV['APP_DATABASE'] に db という文字列が入ってるか確認してください

はいってなければ

[code]

    <%= ENV['APP_DATABASE'] | 'db' %>
    
[/code]

とかかくか docker-compose に

[code]

    web:
      environment:
        APP_DATABASE: db
    
[/code]

とか起動ユーザの bash_profile なり .env なりで環境変数セットする必要があります


CC BY-SA 4.0
2021-08-20T04:07:24.710
80931
2021-08-20T04:22:05.100
2021-08-20T04:22:05.100
80924
answer


回答いただきありがとうございました。  
`environment`キーの`RAILS_ENV`という環境変数に`development`を入れることで解決しました。

▼docker-compose

[code]

    version: '3'
    services:
        db:
            image: mysql:8.0
            platform: linux/x86_64
            command: --default-authentication-plugin=mysql_native_password
            volumes:
                - ./src/db/mysql_data:/var/lib/mysql
            environment:
                MYSQL_ROOT_PASSWORD: password
        web:
            build: .
            command: bundle exec rails s -p 3000 -b '0.0.0.0'
            volumes: 
                - ./src:/app
            ports:
                - "3000:3000"
            environment:
                RAILS_ENV: development #追加
            depends_on:
                - db
    
[/code]

助かりました。ありがとうございました。


CC BY-SA 4.0
2021-08-20T11:46:45.590
80935
2021-08-20T11:46:45.590
36066
80924
answer