LoginSignup
6
8

More than 5 years have passed since last update.

DockerCompose上のRailsでMySQLの起動を待ってdb:migrateする

Last updated at Posted at 2017-04-28

問題

DockerComposeでRails+MySQLを立ち上げてて、Railsコンテナの起動時にdb:migrateする時にCan't connect MySQLのエラーが出る

解決方法

MySQLのコンテナは起動してもMySQLの起動に時間がかかっていることが原因だったもよう。

ということでMySQLの起動を待ってmigrate, 起動するように変更すればOK。

docker-compose.yml
  version: '2'
  services:
    db:
      restart: always
      image: mysql
      ports:
        - "3306:3306"

    web:
      build: .
-     command: bundle exec rails s -p 3000 -b 0.0.0.0
+     command: [bin/entry]
      volumes:
        - .:/app
      ports:
        - "3000:3000"
      links:
        - db
bin/entry
#!/bin/bash
set -e

until mysqladmin ping -h db -P 3306 --silent; do
  echo "waiting for mysql..."
  sleep 1s
done
echo "success to connect mysql"
bundle exec rake db:migrate
bundle exec rake db:seed
bundle exec rails s -p 3000 -b 0.0.0.0

exec "$@"

これでOK

これだけのことに半日ほどハマってしまった('A`)

6
8
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
6
8