Creating test database with Laravel sail - laravel

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

Related

Laravel Sail problem "no configuration file provided: not found"

trying to launch a laravel project with sail:
~/Documents/__laravel_projects/testApp$ ./vendor/bin/sail up
I get this error message:
no configuration file provided: not found
versions: Ubuntu 22.04, Laravel Framework 9.20.0, Docker Desktop 4.10.1
Does anyone know how to fix the problem.
Thanks!
[Solved]
problem solved thanks to #signorpiero here: https://laracasts.com/discuss/channels/laravel/sail-problem-no-configuration-file-provided-not-found?page=1&replyId=811828
[Solution]
1/2)
From project path execute the instruction: php artisan sail:install
:~/Documents/__laravel_projects/testApp$ php artisan sail:install
2/2)
:~/Documents/__laravel_projects/testApp$ ./vendor/bin/sail up
[+] Running 12/13
Try to run php artisan sail:install.
Add docker-compose.yml into your root directory, the content will be like below:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.2
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.2/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
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/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
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
sail-redis:
driver: local

How do you allow a Docker myadmin instance AllowUserDropDatabase in phpMyAdmin?

I have a docker instance working with a myadmin to install phpmyadmin so I can develop a Laravel application. It's all working great, but sometimes I need to drop the database through the phpmyadmin and import a new copy from my production server. I'm not able to. I found this php setting: AllowUserDropDatabase, which, when set to true, should allow phpMyAdmin to drop a database. I tried adding this to the myadmin section of my docker-composer.yml in multiple ways:
AllowUserDropDatabase: true
ALLOW_USER_DROP_DATABASE: true
with no luck. I have used UPLOAD_LIMIT: 10000000 to change that settings, so I thought maybe that would work. Can't seem to find much info on the myadmin element of the docker composer.
My docker-composer.yml file:
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/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
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'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
myadmin:
image: 'phpmyadmin:latest'
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
UPLOAD_LIMIT: 100000000
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local

PHPMyAdmin on Docker with Laravel Sail POST Content-Length exceeds the limit

I have a Laravel environment running on Docker via Sail on Mac OS X Big Sur. I added a phpmyadmin to my docker-compose.yml file and it's all working, but when I try to import my data to the database, I get:
Warning: POST Content-Length of 68109047 bytes exceeds the limit of 2097152 bytes in Unknown on line 0
I published the docker config files to my Laravel project and restarted docker. Here's my docker-composer.yml:
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/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
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'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
myadmin:
image: 'phpmyadmin:latest'
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sail-mysql:
driver: local
and in docker/8.1/php.ini
post_max_size = 100M
upload_max_filesize = 100M
variables_order = EGPCS
But still no luck. I also tried running this command and then at the prompt:
sail php -a
phpinfo();
Which revealed this:
Configuration File (php.ini) Path => /etc/php/8.1/cli
Loaded Configuration File => /etc/php/8.1/cli/php.ini
But there is no php folder in my /etc folder. I also noticed this in my docker config at docker/8.1/Dockerfile
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.1/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
But again, there is no /etc/php folder
According to https://hub.docker.com/_/phpmyadmin you need to specify in your docker-compose.yml file the environment variable UPLOAD_LIMIT to your desired limit. Eg.
myadmin:
image: 'phpmyadmin:latest'
ports:
- 8080:80
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
UPLOAD_LIMIT: 100000000
links:
- "mysql:db"
depends_on:
- mysql
networks:
- sail
so as to have an upload limit of 100.000.000 bytes.
This answer is not correct.
UPLOAD_LIMIT: 100000
specifies an limit of 100000 Bytes! So that is 97,66kB.

Docker - Laravel App Not Running and Not Error

The container stands up and doesn't give any error but also doesn't work. When I try another app that has the same sub-core of the app, I see that it works successfully. The problem is in this app, I'm sure now.
I am leaving some necessary files below. I have no idea what the problem is. Thank you if anyone can help.
supervisord.conf file
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[program:php]
command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan octane:start --watch --server=swoole --host=0.0.0.0 --port=8000
user=sail
environment=LARAVEL_SAIL="1"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:laravel-horizon]
command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan horizon
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=sail
numprocs=1
redirect_stderr=true
stopwaitsecs=3600
compose file
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
container_name: Laravel
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-8000}:8000'
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'
container_name: MySql
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- "/var/lib/mysql"
networks:
- sail
phpmyadmin:
image: 'phpmyadmin:latest'
container_name: PhpMyAdmin
ports:
- 8081:80
environment:
PMA_HOST: mysql
MYSQL_ROOT_USER: '${DB_USERNAME}'
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
depends_on:
- mysql
networks:
- sail
redis:
image: 'redis:alpine'
container_name: Redis
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
Docker Desktop Image

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.

Resources