Well, I have a container with PostgreSQL that I connect to from Laravel showing records on the screen. The Laravel container access the PostgreSQL data ok. This is the result:
object(Illuminate\Support\Collection)#463 (2) {
["items":protected]=>
array(2) {
[0]=>
object(stdClass)#466 (3) {
["pru_id"]=>
int(1)
["pru_name"]=>
string(5) "george"
["pru_years"]=>
int(45)
}
[1]=>
object(stdClass)#467 (3) {
["pru_id"]=>
int(2)
["pru_name"]=>
string(5) "paul"
["pru_years"]=>
int(30)
}
}
}
So far very good!
But when I try to run migrations, with php artisan migrate, I get:
SQLSTATE[08006] [7] could not translate host name "pgcontainer" to address: Name or service not known
Checking the connection, the db_host: (pgcontainer)
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'pgcontainer'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'mydatabase'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypass'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
I tried to modify the host, because I understand what it does not recognize, changing pgcontainer for 127.0.0.1, but when doing it two interesting things happen:
- ON THE SCREEN: It no longer shows the records on the screen, because the Laravel container no longer accesses the PostgreSQL:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432
- SSH - PHP ARTISAN MIGRATE: Now Laravel can see the PostgreSQL container but it cannot authenticate (the password is correct since before it shows records on the screen):
SQLSTATE[08006] [7] FATAL: password authentication failed for user "mydatabase"
Any idea?
If I understand correctly, you have one container with Postgres, and a different container with Laravel. The connection from the Laravel -> Postgres containers works fine.
The error you are seeing suggests you are running
php artisan migrateon your host, not in a container. But your host does not know whatpgcontainermeans - Docker resolves containers by name, but your host can't.You should be running
php artisan <anything>from inside the container where PHP/Laravel is, for eg:This way
artisanruns inside the container, and host name resolution works just the same as it does for Laravel. Do not change your.envor config,artisanis part of Laravel and needs the same config as the Laravel application itself.