@814hiros

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Dockerでrails+MYSQLの環境構築およびdocker-compose run web rake db:createを実行

解決したいこと

Dockerでrails+MYSQLの環境構築。

ruby 2.5.3
rails 5.2.4
MYSQL 5.7
windows home

発生している問題・エラー

ec2-user:~/environment/rails-app (master) $ docker-compose run web rake db:create
Starting rails-app_db_1 ... done
Unknown MySQL server host 'db' (-2)
Couldn't create 'app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-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-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:119:in `create'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
/usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
【database.yml】

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: db

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production
  username: app
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>

Dockerfile↓

FROM ruby:2.5.3
ENV LANG C.UTF-8

RUN apt-get update -qq && apt-get install -y build-essential nodejs

# yarnパッケージ管理ツールをインストール
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
# Node.jsをインストール
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash - && \
    apt-get install nodejs

WORKDIR /tmp
COPY Gemfile Gemfile.lock ./
RUN bundle install

WORKDIR /app
COPY . /app

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

Gemfile↓

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.5'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]


docker-compose.yml↓

version: '3.3'
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbuser
    expose:
      - "5432"
    ports:
      - "5433:5432"
    volumes:
      - ./tmp/db:/var/lib/mysql/data
  web:
    build:
      context: .
      dockerfile: Dockerfile
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    ports:
      - "3000:3000"
    environment:
      MYSQL_HOST: db
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: ''
      MYSQL_PORT: 5432
    volumes:
      - .:/app
      - bundle:/bundle
    depends_on:
      - db
    tty: true
    stdin_open: true

volumes:
  bundle:
    driver: local

entrypoint.sh↓

#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /rails-app/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

自分で試したこと

次の記事を参照にして
https://gist.github.com/riouruma
https://qiita.com/seweb14wm54/items/4306506617ddba199eaa
https://qiita.com/kodai_0122/items/795438d738386c2c1966

Dockerに関するファイルを作成後、
docker-compose run web bundle exec rake db:createで
データベースの作成を実行しましたが、

$ docker-compose run web rake db:create
Starting rails-app_db_1 ... done
Unknown MySQL server host 'db' (-2)
Couldn't create 'app_development' database. Please check your configuration.
rake aborted!
Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-2)

とエラーが返ってきました。
直訳すると
「不明なMySQLサーバーホスト 'db'(-2)
'app_development'データベースを作成できませんでした。構成を確認してください。
レーキが中止されました!
Mysql2 :: Error :: ConnectionError:不明なMySQLサーバーホスト 'db'(-2)」

MYSQサーバーホストが不明なようです。
database.ymlのhostをdbにしております。

他、この記事を参照しましたがdocker-compose.ymlの内容が間違って
エラーが生じてしまったのかと推測しております。

https://qiita.com/isacRU/items/dda2f2d9511c3a61854e

実現したいこと
$ docker-compose run web rake db:createが実行できるようになること。

アドバイス・ご指摘いただけますと幸甚に存じます。
よろしくお願い申し上げます。

0 likes

1Answer

エラーしか見てないですが、dbコンテナ立ち上がってないかも

docker-compose ps

dbがなければ以下コマンドで

docker-compose up -d db
# log
docker-compose logs -ft db
0Like

Comments

  1. @814hiros

    Questioner

    ご教授いただきまことにありがとうございます。
    ご指示通り、dbが存在していなかったため、
    次のコマンドで作成いたしました。

    ```
    docker-compose up -d db

    ```

    ```
    $ docker-compose logs -ft db

    ec2-user:~/environment/myapp (master) $ docker-compose logs -ft db
    Attaching to myapp_db_1
    db_1 | 2021-02-16T06:54:44.258025610Z 2021-02-16 06:54:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T06:54:44.502771720Z 2021-02-16 06:54:44+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T06:54:44.541178088Z 2021-02-16 06:54:44+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T06:54:44.585705346Z 2021-02-16 06:54:44+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T06:54:44.585767056Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T06:54:50.767212762Z 2021-02-16 06:54:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T06:54:50.903650115Z 2021-02-16 06:54:50+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T06:54:50.917185371Z 2021-02-16 06:54:50+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T06:54:51.054683929Z 2021-02-16 06:54:51+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T06:54:51.084738589Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T07:00:19.721529849Z 2021-02-16 07:00:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:00:19.877445922Z 2021-02-16 07:00:19+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T07:00:19.899729250Z 2021-02-16 07:00:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:00:20.054265549Z 2021-02-16 07:00:20+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T07:00:20.054420913Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T07:00:40.303676869Z 2021-02-16 07:00:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:00:40.387227165Z 2021-02-16 07:00:40+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T07:00:40.392269433Z 2021-02-16 07:00:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:00:40.443167991Z 2021-02-16 07:00:40+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T07:00:40.489345933Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T07:01:32.157490656Z 2021-02-16 07:01:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:01:32.341624647Z 2021-02-16 07:01:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T07:01:32.367203070Z 2021-02-16 07:01:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:01:32.519906546Z 2021-02-16 07:01:32+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T07:01:32.521130716Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T07:16:40.928681732Z 2021-02-16 07:16:40+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:16:41.305326106Z 2021-02-16 07:16:41+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T07:16:41.359523725Z 2021-02-16 07:16:41+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:16:41.496267409Z 2021-02-16 07:16:41+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T07:16:41.497033163Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    db_1 | 2021-02-16T07:17:52.827793515Z 2021-02-16 07:17:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:17:52.910073505Z 2021-02-16 07:17:52+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
    db_1 | 2021-02-16T07:17:52.915765177Z 2021-02-16 07:17:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.
    db_1 | 2021-02-16T07:17:53.013953844Z 2021-02-16 07:17:53+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    db_1 | 2021-02-16T07:17:53.014029979Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
    myapp_db_1 exited with code 1

    ```
    ログの内容の最後、


    ```
    db_1 | 2021-02-16T07:17:53.014029979Z You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD

    ```

    database.ymlでは次の通りパスワード設定しております。


    ```
    default: &default
    adapter: mysql2
    encoding: utf8
    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: app_production
    username: app
    password: password

    ```

    なお
    $ docker-compose run web rake db:createと実行したら
    次のエラーが返ってきました。

    ```
    ec2-user:~/environment/myapp (master) $ docker-compose run web rake db:create
    Starting myapp_db_1 ... done
    Unknown MySQL server host 'db' (-2)
    Couldn't create 'app_development' database. Please check your configuration.
    rake aborted!
    Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (-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-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/connection_handling.rb:90:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:119:in `create'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
    /usr/local/bundle/gems/activerecord-5.2.4.5/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>'
    /usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
    Tasks: TOP => db:create
    (See full trace by running task with --trace)

    ```

    エラーの中にしばしば次のコードが見られます。

    /tasks/database_tasks.rb

    こちらは他のアプリケーションが
    存在してしまっているため、データベース作成できないことでしょうか。

    重ねての質問で大変申し訳ありませんが、
    何卒よろしくお願い申し上げます。

Your answer might help someone💌