10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docker-composeコマンド実行時の不可解なエラー

Last updated at Posted at 2019-09-25

前提

docker for mac(またはwindows)がインストールされ、公式に沿ってDockerfile等が作られていること。
さらに公式手順の「docker-compose up」まで実行されている状態であること。
docker-compose.ymlファイルだけ以下のようにアレンジしました。

docker-compose.yml
version: '3'
services:
  db: 
    image: mysql
    command: --default-authentication-plugin=caching_sha2_password
    # mysqlデータのマウントポイントを設定
    volumes:
      - ./tmp/db:/var/lib/mysql/data
    environment:
          MYSQL_ROOT_PASSWORD: hao123
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - 3000:3000
    depends_on:
      - db
    tty: true
    working_dir: "/app"
    # database.ymlで使用する環境変数
    environment:
      DB_USER: root
      DB_PASSWORD: hao123 
      DB_HOST: db

発生した謎のエラー

以下のコマンドを実行後、エラーが出てしまう。
ネットで調べても解決策が見つからなかったので、メモを残しておきます。
ちなみdocker-compose.ymlでもdbと指定してるし、database.ymlとの整合性が取れていることも確認済み。
「Unknown MySQL server host 'db'」だと?!
君は何を言ってるんだい?^ ^

USERnoMBP:app user$ docker-compose run web rake db:create
Starting 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.2/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:1014:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/mysql_database_tasks.rb:6:in `connection'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/mysql_database_tasks.rb:14:in `create'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:119:in `create'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:139:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:316:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:313:in `each'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:313:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:138:in `create_current'
/usr/local/bundle/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:29:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

原因

docker-compose等のファイルを再確認しましたが、オカシイ部分が見つからなかったです。
念の為docker-compose.ymlのvolumesのマウント元(/var/lib/mysql/data)のディレクトリがあるか調べてたら、dataというファイルまたはディレクトリが存在しないことがわかった。

    volumes:
      - ./tmp/db:/var/lib/mysql/data → ここの部分

よって、マウント元のディレクトリが存在しない場合は上記のエラーが起きるということがわかりました。
おそらく今回の場合、マウントが失敗してdbホストが作られなかったと考えられる。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?