Laravel Unable to connect to database using Docker Compose - laravel

I am developing a Laravel application trying to Docker as the development environment. I am using Docker Compose to orchestrate the docker. I am now having trouble in connecting to database. Here is what I have done so far.
I have created a docker-compose.yml in the project root folder.
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:
container_name: easy_eat_db
image: mariadb:10.2
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: easy_eat
MYSQL_USER: easy_eat
MYSQL_PASSWORD: secret
volumes:
- db_data:/var/lib/mysql
networks:
- easy-eat-network
ports:
- "33060:3306"
networks:
easy-eat-network:
driver: "bridge"
volumes:
db_data:
driver: "local"
Then I run the following command to spin up the environment.
docker-compose up --build -d
I modified the database credentials in the env file like this.
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=easy_eat
DB_USERNAME=easy_eat
DB_PASSWORD=secret
To ensure that my laravel application can connect to the database, I run the migration command as follow.
docker-compose exec php-fpm php artisan migrate:fresh --seed
I got the following error.
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'easy_eat'#'172.25.0.4' (using password: YES) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
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[HY000] [1045] Access denied for user 'easy_eat'#'172.25.0.4' (using password: YES)")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=db;port=3306;dbname=easy_eat", "easy_eat", "secret", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
What is wrong with setting and how can I fix it?

I found the issue. There is already a project using Docker previously. In the docker-compose file, db container is using the db_data volume. So it is causing conflict with the existing project. So, I had to changed my configuration to as follow.

Related

Trouble with connecting PostgreSQL in docker-compose

I am trying to create a Laravel project on Docker with a PostgreSQL database locally. The structure of my project is described below:
nginx
conf.d
default.conf
php
Dockerfile
src
Laravel Project
docker-compose-yml
I can run the project successfully on the 8080 port, but I'm having trouble connecting to PostgreSQL.
Here is my docker-compose.yml:
version: '3.8'
networks:
laravel:
services:
nginx:
image: nginx:alpine
command: nginx -g "daemon off;"
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d
depends_on:
- php
networks:
- laravel
php:
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
db:
container_name: postgres
image: postgres
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=root
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_DB=expense
volumes:
- ./postgres:/var/lib/postgresql/data"
ports:
- "5446:5432"
restart: always
networks:
- laravel
In case you want to know, here is the Dockerfile for PHP:
FROM php:8.0.3-fpm
RUN docker-php-ext-install pdo
And also I want to mention the .env that is used by Laravel project:
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=postgres
with this configuration first I've run the docker-compose build and after running the docker-compose up I'm getting the below result for Postgres:
And when I want to reach 127.0.0.1:5432 there's nothing to show me. How can I solve this problem?
In your service db your POSTGRES_DB=expense set the default database name to expense (doc)
So your .env should use DB_DATABASE=expense and not postgres
And a last thing I am not sure about (because I can't find the documentation about it) is your DB_HOST: I don't know if you should use the container_name -> postgres or the service_name -> db, I think it's the service_name so db in your case

Laravel in Docker giving 'php_network_getaddresses: getaddrinfo failed: Name does not resolve'

I am having an issue with my Laravel in Docker.
Currently when I run php artisan migrate inside my container I get the error
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not
resolve (SQL: select * from information_schema.tables where table_schema =
app_database and table_name = migrations and table_type = 'BASE TABLE')
However, I am able to connect to the mysql using Sequel Pro and I am able to see the database created app_database
My docker_compose.yml is as below:
version: '3'
services:
nginx:
build:
context: .
dockerfile: Dockerfile_nginx
container_name: nginx_webserver
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- database
networks:
- laravel
database:
image: mysql:8.0
container_name: docker_database
environment:
- "MYSQL_DATABASE=app_database"
- "MYSQL_USER=app_db_user"
- "MYSQL_PASSWORD=app_db_password"
- "MYSQL_ROOT_PASSWORD=password"
volumes:
- ./mysql/db_data:/var/lib/mysql
ports:
- "3306:3306"
php:
build:
context: .
dockerfile: Dockerfile_php
container_name: my_app
volumes:
- ./src:/var/www
- ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
ports:
- "9000:9000"
networks:
- laravel
depends_on:
- database
networks:
laravel:
driver: bridge
The .env of my laravel app is
DB_CONNECTION=mysql
DB_HOST=docker_database
DB_PORT=3306
DB_DATABASE=app_database
DB_USERNAME=app_db_user
DB_PASSWORD=app_db_password
Can anybody share some insight?
I have tried everything online.
It looks like you need to add network to database container
networks:
- laravel
I got a similar error. In my case the mistake was using docker run which spun up a new container which was in it's own isolated network instead of using docker compose run which will run the command on one of the containers spun up after docker compose up which will have access to the other containers in the network

Laravel Sail work migrate or contact with database

I run larvae 8 sail he work fine
my file content is docker-compose.yml
# For more information: https://laravel.com/docs/sail
version: '3'
services:
rami.dev:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mariadb
mariadb:
image: 'mariadb:10'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmariadb:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" , "-h", "localhost"]
retries: 3
timeout: 5s
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
PMA_ARBITRARY: 1
PMA_HOST: 'mariadb'
PMA_USER: '${DB_USERNAME}'
PMA_PASSWORD: '${DB_PASSWORD}'
networks:
- sail
selenium:
image: 'selenium/standalone-chrome'
volumes:
- '/dev/shm:/dev/shm'
networks:
- sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- '${FORWARD_MAILHOG_PORT:-1025}:1025'
- '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmariadb:
driver: local
if I put
.env DB_HOST=mariadb
I put mariadb because i used mariadb not mysql
put I cant run php artisan migrate if .env DB_HOST=mariadb must change to DB_HOST=127.0.0.1
and get this error with DB_HOST=mariadb
php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = ramiyusu_live and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕ }
+36 vendor frames
37 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
if I change to DB_HOST=127.0.0.1 migrate work and site stop
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `id` = 1 limit 1)
http://localhost/
i can run both if return DB_HOST my site work okay and to run migrate use sail shell then php artisan migrate
Instead of calling php artisan migrate from your local machine, call it from the container. As per documentation you can use sail artisan migrate.

Unable to use Postgres with Docker Compose and Laravel

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?
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.

Unable to connect to pgsql in Laravel in Docker

I am developing a Web application using Laravel. I am an experienced Laravel developer. But, now I am trying to use Docker as my development environment. But I am so new to Docker. Now I am trying to connect to the Postgres database. I added the Postgres docker image in the docker-composer.yml as well. But when I run migration, I am getting error and it is not connecting to the database.
This is my docker-compose.xml file.
version: '2'
services:
web:
build:
context: ./
dockerfile: docker/web.docker
volumes:
- ./:/var/www
ports:
- "80:80"
- "443:443"
- "9000:9000"
links:
- app
app:
build:
context: ./
dockerfile: docker/app.docker
volumes:
- ./:/var/www
links:
- mysql
- redis
- beanstalk
- cache
environment:
- "DB_PORT=3306"
- "DB_HOST=mysql"
- "REDIS_PORT=6379"
- "REDIS_HOST=redis"
mysql:
image: mysql:5.7.18
environment:
- "MYSQL_ROOT_PASSWORD=secret"
- "MYSQL_DATABASE=docker"
ports:
- "3306:3306"
pgsql:
image: postgres:10.1
restart: always
environment:
- POSTGRES_DB=docker
- POSTGRES_USER=root
- POSTGRES_PASSWORD=secret
ports:
- 5431:5431
volumes:
- ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/
redis:
image: redis:3.0
ports:
- "6379:6379"
beanstalk:
image: schickling/beanstalkd
ports:
- "11300:11300"
cache:
image: memcached:alpine
ports:
- "11211:11211"
I know I added the Mysql image as well. When I connect to the Mysql image, it was working. When I connect to the Postgres, I am getting error.
This is my database settings in env file for connecting to the Postgres.
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5431
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=secret
When I run migration in the terminal like this
docker-compose exec app php artisan migrate --seed
I got this error.
In Connection.php line 647:
could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
In PDOConnection.php line 50:
could not find driver
In PDOConnection.php line 46:
could not find driver
What is wrong with my installation?
You didn't add your docker/app.docker details, but if this is PHP running, you need to make sure both php7-pgsql and php7-pdo are installed on this container. driver is usuallu JUST THAT phpX-mysql or phpX-pgsql. X is the optional version number, depending on which repo you're using for php. i.e with default ubuntu repo php-pgsql will be just fine, but with alpine images you have to use php7-pgsql.
with sury repo, you'll have to use specific version i.e php7.2-pgsql.

Resources