Docker上のLaravelでDBをmigrateしたらエラー(could not connect to server: Connection refused)になった話

# はじめに

`laradock`にて構築した、`laravel`+`postgreSQL`の環境で`migrate`を行ったところ、エラーになりました。

## 1. マイグレーションファイルの作成

```
laradock $ docker-compose exec workspace bash
root@16a77d3a94fe:/var/www# cd quickstart/
root@16a77d3a94fe:/var/www/quickstart# php artisan make:migration create_tasks_table --create=tasks
Created Migration: 2019_04_10_151437_create_tasks_table
```

## 2. migrate

```
root@16a77d3a94fe:/var/www/quickstart# php artisan migrate
```

## 3. エラー

```
In Connection.php line 664:
                                                                                                                         
  SQLSTATE[08006] [7] could not connect to server: Connection refused                                                    
        Is the server running on host "localhost" (127.0.0.1) and accepting                                                   
        TCP/IP connections on port 5432?                                                                                      
  could not connect to server: Cannot assign requested address                                                           
        Is the server running on host "localhost" (::1) and accepting                                                         
        TCP/IP connections on port 5432? (SQL: select * from information_schema.tables where table_schema = public and table  
  _name = migrations)                                                                                                    
                                                                                                                         

In Connector.php line 67:
                                                                        
  SQLSTATE[08006] [7] could not connect to server: Connection refused   
        Is the server running on host "localhost" (127.0.0.1) and accepting  
        TCP/IP connections on port 5432?                                     
  could not connect to server: Cannot assign requested address          
        Is the server running on host "localhost" (::1) and accepting        
        TCP/IP connections on port 5432?                                     
```                                                                        

# 対処法

laravelアプリケーションのディレクトリ配下にある、`.env`ファイルを修正します。

`DB_HOST=localhost`であったところを、`laradock/docker-compose.yml`中でPostgreSQLに指定されたサービス名`postgres`を用いて、`DB_HOST=postgres`に変更します。

```yml:docker-compose.yml
### PostgreSQL ###########################################
    postgres:
      build: ./postgres
      volumes:
        - ${DATA_PATH_HOST}/postgres:/var/lib/postgresql/data
        - ${POSTGRES_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${POSTGRES_PORT}:5432"
      environment:
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        - GITLAB_POSTGRES_INIT=${GITLAB_POSTGRES_INIT}
        - GITLAB_POSTGRES_USER=${GITLAB_POSTGRES_USER}
        - GITLAB_POSTGRES_PASSWORD=${GITLAB_POSTGRES_PASSWORD}
        - GITLAB_POSTGRES_DB=${GITLAB_POSTGRES_DB}
        - JUPYTERHUB_POSTGRES_INIT=${JUPYTERHUB_POSTGRES_INIT}
        - JUPYTERHUB_POSTGRES_USER=${JUPYTERHUB_POSTGRES_USER}
        - JUPYTERHUB_POSTGRES_PASSWORD=${JUPYTERHUB_POSTGRES_PASSWORD}
        - JUPYTERHUB_POSTGRES_DB=${JUPYTERHUB_POSTGRES_DB}
      networks:
        - backend
```

```:.env
#(略)
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=quickstart
DB_USERNAME=default
DB_PASSWORD=secret
#(略)
```

# migrateの再実行結果

無事、migrateされました。

```
root@16a77d3a94fe:/var/www/quickstart# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2019_04_10_151437_create_tasks_table
Migrated:  2019_04_10_151437_create_tasks_table
```

# 参考

- [docker-composeとネットワーク、コンテナのホスト名の割り当てなどを理解する - Qiita](https://qiita.com/roba4coding/items/efd3a38db08eb476d412)

- [Docker環境でLaravelを構築するまで、わずか15分。 - Qiita](https://qiita.com/hosono/items/dd404f3f074722ef989d)