Laravel docker either artisan or model display connection refused - laravel

The application is hosted in a docker container locally on my machine as I am working on the app. The php artisan migrate command is working fine. The problem occurs when I try to load the content onto a web page using any of the Models.
Explanation below:
The Env content:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
The migration command works.
php artisan migrate
But, when I try to display the data on a page:
public function index(){
return App\Models\User::all();
}
Laravel says:
SQLSTATE[HY000] [2002] Connection refused
My docker-compose.yml content for mysql:
services:
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
When I change DB_HOST to the name of the container;
DB_HOST=mysql
Laravel starts to display the data, but php artisan migrate does not work.
How it tries to connect to it:
PDO::__construct("mysql:host=mysql;port=3306;dbname=my_db", "root", "", [])
Then fails:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: No such host is known. (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
I need to understand why it is doing so, and how I can resolve this problem.
I would also like to know if this will have the same problem when I deploy the application on production.
Thank you.

Related

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.

Creating test database with Laravel sail

I'm trying to create a test database for my using Laravel and Sail. Following this post, I updated my docker-compose file with:
mysql:
image: 'mysql:8.0'
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:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
mysql_test:
image: "mysql:8.0"
environment:
MYSQL_ROOT_PASSWORD: "${TEST_DB_PASSWORD}"
MYSQL_DATABASE: "${TEST_DB_DATABASE}"
MYSQL_USER: "${TEST_DB_USERNAME}"
MYSQL_PASSWORD: "${TEST_DB_PASSWORD}"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
networks:
- sail
In my .env file I have:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=real_database
DB_USERNAME=root
DB_PASSWORD=root
TEST_DB_CONNECTION=mysql
TEST_DB_HOST=mysql_test
TEST_DB_PORT=3306
TEST_DB_DATABASE=test_database
TEST_DB_USERNAME=root
TEST_DB_PASSWORD=root
However, when I run sail up, I get:
mysql_test_1 | 2021-03-13 17:18:10+00:00 [Note] [Entrypoint]: Creating database test_database
mysql_test_1 | 2021-03-13 17:18:10+00:00 [Note] [Entrypoint]: Creating user root
mysql_test_1 | ERROR 1396 (HY000) at line 1: Operation CREATE USER failed for 'root'#'%'
Any thoughts on why the build is failing?
Your usernames can not be root, there is already an root user in MySql, so when you insert your user it already exists.
DB_USERNAME=youruser
DB_PASSWORD=password
TEST_DB_USERNAME=testuser
TEST_DB_PASSWORD=testpassword

Laravel Sail SQL Connection timed out

I just started using Laravel Sail, but Laravel can't connect to the MySQL server. Even Artisan in the container gives Connection timed out. I can connect from MySQL Workbench on my host machine.
My .env file has been updated for use with sail:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Laravel sail does not come with phpMyAdmin to visualize your database. so, you need to go with one of these:
add phpMyAdmin to your docker-compose.yml file manually.
services:
...
phpmyadmin:
image: phpmyadmin
restart: always
container_name: phpmyadmin
depends_on:
- mysql
ports:
- 8080:80
environment:
PMA_HOST: mysql
MYSQL_USERNAME: "${DB_USERNAME}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
...
volumes:
...
phpmyadmin:
driver: local
restart your project again.
sail up -d
sail now will download phpMyAdmin image to your docker container where you
will able to view database on port:8080 [http://localhost:8080/].
use a graphical database management application like TablePlus as mentioned in sail documentation.
I recently ran across this issue too, when upgrading an old Laravel project from using homestead to sail. As per the docs you need to set DB_HOST=mysql in your .env file so that your application container can communicate with the MySQL container.
You must write in "DB_HOST" are domain or ip
DB_HOST=1.2.3.4
or
DB_HOST=example.com

Laravel Unable to connect to database using Docker Compose

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.

Resources