Reputation: 14771
I am developing a Laravel application. I am using Docker Compose to install my environment. I am using Postgres for database.
This is my docker-compose.yml:
version: '3'
services:
apache:
container_name: easy_eat_apache
image: webdevops/apache:ubuntu-16.04
environment:
WEB_DOCUMENT_ROOT: /var/www/public
WEB_ALIAS_DOMAIN: easy-eat.localhost
WEB_PHP_SOCKET: php-fpm:9000
volumes: # Only shared dirs to apache (to be served)
- ./public:/var/www/public:cached
- ./storage:/var/www/storage:cached
networks:
- easy-eat-network
ports:
- "80:80"
- "443:443"
php-fpm:
container_name: easy_eat_php
image: jguyomard/laravel-php:7.2
volumes:
- ./:/var/www/
- ./ci:/var/www/ci:cached
- ./vendor:/var/www/vendor:delegated
- ./storage:/var/www/storage:delegated
- ./node_modules:/var/www/node_modules:cached
- ~/.ssh:/root/.ssh:cached
- ~/.composer/cache:/root/.composer/cache:delegated
networks:
- easy-eat-network
db:
image: postgres
restart: always
ports:
- 5432
environment:
POSTGRES_PASSWORD: secret
volumes:
- easy-eat-data:/var/lib/postgresql
networks:
easy-eat-network:
driver: "bridge"
volumes:
easy-eat-data:
driver: "local"
In the env file of Laravel, I put the database credentials like this
DB_CONNECTION=pgsql
DB_HOST=db
DB_PORT=5432
DB_DATABASE=postgres
DB_USERNAME=postgres
DB_PASSWORD=secret
Then I run migration command to see if my application is able to connect to the postgres. I got this error.
Illuminate\Database\QueryException : SQLSTATE[08006] [7] timeout expired (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[08006] [7] timeout expired")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("pgsql:host=db;dbname=postgres;port=5432;sslmode=prefer", "postgres", "secret", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.
How can I fix it?
Upvotes: 0
Views: 1810
Reputation: 158706
Your php-fpm
container is attached to the easy-eat-network
network, but your db
container isn't, so it will get attached to the default network Docker Compose creates instead. That will cause problems resolving and routing between the containers.
Probably the easiest solution here is just to rely on the default network Docker Compose will create for you. Delete all of the networks:
blocks, both at the top level and individual services.
If you really want to explicitly declare the network yourself, then you need to add the networks: [easy-eat-network]
stanza to the db:
service block.
Upvotes: 2