According to docs:
Also, when the MySQL container is starting, it will ensure a database exists whose name matches the value of your DB_DATABASE environment variable.
My .env
file looks like this.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
Yet when I try to run the migrations via sail artisan migrate
, I get back this error:
SQLSTATE[HY000] [1049] Unknown database 'test' (SQL: select * from information_schema.tables where table_schema = test and table_name = migrations and table_type = 'BASE TABLE')
What I've tried:
sail build --no-cache
(to try and rebuild everything altogether)sail shell
, I went into MySQL and showed all databases. I could see the default laravel database there.How do you tell sail to create the correct DB_DATABASE
?
My docker-compose.yml
:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
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:
- 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
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- 1025:1025
- 8025:8025
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
When using Laravel Sail, your application is executing within a Docker container and is isolated from your local computer. However, Sail provides a convenient way to run various commands against your application such as arbitrary PHP commands, Artisan commands, Composer commands, and Node / NPM commands.
Once you feel comfortable extending Laravel Sail, you already have the knowledge required to build your own environment. Think about it: the docker-compose.yml file is not specific to Laravel Sail, that's just how Docker Compose works. The same goes for Dockerfiles – they are standard Docker stuff.
The --devcontainer option will instruct the sail:install command to publish a default .devcontainer/devcontainer.json file to the root of your application: By default, Sail commands are invoked using the vendor/bin/sail script that is included with all new Laravel applications:
Sail follows a long list of attempts at a Laravel development environment like Homestead, Valet, Laragon, Takeout, Laradock, and Vessel. The entire package is made up of two files: a docker-compose.yml file that holds your application’s Docker containers, and a Sail script that provides you with a CLI for interacting with these containers.
If you have added the sail command to the PATH environment variable...
If you execute the sail command in the project directory where the .env file exists, the database named DB_DATABASE is created when the MySQL container is executed.
https://github.com/laravel/sail/blob/1.x/bin/sail#L66
# Source the ".env" file so Laravel's environment variables are available...
if [ -f ./.env ]; then
source ./.env
fi
I tried with the following example project.
Your First Laravel Project - https://laravel.com/docs/8.x/installation#getting-started-on-windows
curl -s https://laravel.build/example-app | bash
cd example-app
./vendor/bin/sail up
Configuring A Bash Alias - https://laravel.com/docs/8.x/sail#configuring-a-bash-alias
vi ~/.bashrc
alias sail='bash vendor/bin/sail'
I ran it in daemon mode and checked the docker process.
sail up -d
sail ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------
example-app_laravel.test_1 start-container Up 0.0.0.0:80->80/tcp, 8000/tcp
example-app_mailhog_1 MailHog Up 0.0.0.0:1025->1025/tcp, 0.0.0.0:8025->8025/tcp
example-app_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
example-app_redis_1 docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
My .env
DB_DATABASE=example_app
List of MySQL databases.
sail mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| example_app |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
DB migration was also successful.
sail artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (166.17ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (99.25ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (128.87ms)
Laravel Sail - https://laravel.com/docs/8.x/sail
laravel new test
cd test
composer require laravel/sail --dev
php artisan sail:install
sail up -d
The database name follows the project name.
grep DB_DATABASE .env
DB_DATABASE=test
DB migration was also successful.
sail artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (162.35ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (96.68ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (89.60ms)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With