Laravel Sail work migrate or contact with database - laravel

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.

Related

How to solve [Undefined index: name] in Laravel-docker

I am trying to execute Laravel with docker on Ubuntu 20.04
At first, I installed docker and created docker-compose.yml so on.
version: "3.7"
services:
app:
build:
args:
user: sammy
uid: 1000
context: ./
dockerfile: Dockerfile
image: travellist
container_name: travellist-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- travellist
db:
image: mysql:5.7
container_name: travellist-db
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
SERVICE_TAGS: dev
SERVICE_NAME: mysql
volumes:
- ./docker-compose/mysql:/docker-entrypoint-initdb.d
networks:
- travellist
nginx:
image: nginx:alpine
container_name: travellist-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d/
networks:
- travellist
networks:
travellist:
driver: bridge
docker-compose exec app composer install
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
In PackageManifest.php line 122:
Undefined index: name
Script #php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1
when I entered this command, I have got some error "undefined index: name"
If you can solve this error, please let me know.
Thanks.

How To Fix SQLSTATE[HY000] [2002] Connection refused Laravel Docker

When I try to run the command php artisan migrate, an error occurs:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = 'BASE TABLE')
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql mysqli
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8088:80"
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0
container_name: db
ports:
- "3307:3306"
expose:
- "3306"
volumes:
- ./cache/mysql:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: root
MYSQL_PASSWORD: root
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
networks:
- laravel
composer:
image: composer:latest
container_name: composer
depends_on:
- nginx
- php
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
.env:
DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root
I try too:
DB_HOST=127.0.0.1
DB_HOST=localhost
thank you very good
In docker-compose.yml
services:
## -----------------------------------------------
## MySql database
## -----------------------------------------------
mysql:
image: mysql:8.0
restart: always
volumes:
- db_mysql:/var/lib/mysql
- ./mysql:/docker-entrypoint-initdb.d
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_ROOT_PASSWORD: root
networks:
- app-network
deploy:
mode: global
ports:
- "3306:3306"
php:
..
..
environment:
DB_HOST: mysql
DB_NAME: db_name
DB_USERNAME: root
DB_PASSWORD: root
networks:
- laravel
// map volume
volumes:
db_mysql:
In MySQL Dockerfile
FROM mysql:8.0
please chnage the ports in docker-compose.yml File , it works for me
ports:
- 8889:3306
----My-> docker-compose.yml File ---
version: '3.8'
services:
Web Server Service
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
- app
- db
#Database Service
db:
image : mysql
container_name : mysql
restart: unless-stopped
tty: true
ports:
- 8889:3306
volumes:
# - ./mysql/data:/var/lib/mysql
environment:
# MYSQL_DATABASE: laravel_blogs
## MYSQL_ROOT_PASSWORD: password
# MYSQL_ROOT_HOST: localhost
# SERVICE_NAME: mysql
# SERVICE_TAGS: dev
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_USER=${DB_USERNAME}
- MYSQL_PASSWORD=${DB_PASSWORD}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
volumes:
- ~/mysql:/var/lib/mysql
app:
container_name: app
build:
context: ./php
dockerfile: Dockerfile
volumes:
- ./src:/var/www
ports:
- "9000:9000"
working_dir: /var/www
phpMyAdmin:
# phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
- "3400:80"
depends_on:
- db
links:
- db
#pgadmin:
#image: dpage/pgadmin4
#container_name: pgAdmin
#ports:
# - "5050:80"
#depends_on:
# - db
#environment:
# PGADMIN_DEFAULT_EMAIL: hanieasemi76#gmail.com
PGADMIN_DEFAULT_PASSWORD: password

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

Add new service using laravel sail

I have a laravel 8 project created with composer. Then I added laravel sail by composer require laravel/sail and then executed command php artisan sail:install.
After sail up command, only mysql container was created and everything works well. But now I want to add redis to my docker container. How can I do this? I have no knowledge with Docker but laravel sail has made it very easy to use Docker. I want to add redis with the help of laravel sail. Is there any way to do that?
you can edit docker-compose.yml
In the services
laravel.test:
...
depends_on:
- mysql
- redis
...
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
then finally, in volumes:
volumes:
sailmysql:
driver: local
sailredis:
driver: local
You may also can start a fresh new project with
curl -s "https://laravel.build/example-app?with=mysql,redis" | bash
then refer the generated docker-compose.yml
after you edit, save it then run ./vendor/bin/sail build then only saild up -d
You can just issue the command php artisan sail:install again
Now select the services you need and separate them with ,
So for example 0, 3, 7
This will update your docker-compose.yml file and add the lines for MySQL, Redis, Mailhog. It might overwrite some of your old settings though!
Once you start sail again it will pull the services and you're good to go.
I know it is not the best solution but I think it's so easy.
Update your docker-compose.yml file using following lines:
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
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", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local

Connection refused when running docker-compose exec app php artisan migrate

I'm getting PDOException::("SQLSTATE[HY000] [2002] Connection refused") when running docker-compose exec app php artisan migrate on the terminal
here's my db config on .env file
DB_CONNECTION=mysql
DB_HOST=172.20.0.4
DB_PORT=3306
DB_DATABASE=queue_db
DB_USERNAME=root
DB_PASSWORD=root
I also inspect the container ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' worker-db
Output: 172.20.0.4
here's my docker-compose.yml
version: '3'
services:
#PHP Service
app:
build:
context: .
dockerfile: ./docker/Dockerfile
image: digitalocean.com/php
container_name: worker-app
restart: unless-stopped
tty: true
environment:
SERVICE_NAME: app
SERVICE_TAGS: dev
working_dir: /var/www
volumes:
- ./:/var/www
- ./docker/config/php/local.ini:/usr/local/etc/php/conf.d/local.ini
networks:
- app-network
#Nginx Service
webserver:
image: nginx:alpine
container_name: worker-webserver
restart: unless-stopped
tty: true
ports:
- "8090:80"
- "443:443"
volumes:
- ./:/var/www
- ./docker/config/nginx/conf.d/:/etc/nginx/conf.d/
networks:
- app-network
#MySQL Service
db:
image: mysql
container_name: worker-db
# restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: queue_db
MYSQL_ROOT_PASSWORD: root
SERVICE_TAGS: dev
SERVICE_NAME: mysql
networks:
- app-network
#Docker Networks
networks:
app-network:
driver: bridge
#Volumes
volumes:
dbdata:
driver: local
There's a weird error on the stack trace where ip is different i'm not sure why it's getting a different ip, but in my .env file i'm using the container ip which is "172.20.0.4"
Exception trace:
1 PDOException::("SQLSTATE[HY000] [2002] Connection refused")
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=172.20.0.3;port=3306;dbname=queue_db", "root", "root", [])
/var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Thanks in advance.
ok, after i tried to run php artisan config:clear it's working now.

Resources