rayplawlor@gmail.com's avatar

Laravel Sail - I have to switch the DB_HOST Value in .env to use seeders. Is there a way around this?

Hi Laracasters! :)

Many thanks for reading my issue.

I installed Laravel using Laravel Sail on Docker for Mac.

When I run my app (at this point just API endpoints) everything works fine.

But when I go to run seeders from the CLI I need to change the "DB_HOST" setting in my env file to:

DB_HOST=127.0.0.1

Then back to:

DB_HOST=mysql

to get the app to work again.

What am I doing wring here?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

This is expected behavior when using Laravel Sail. When running Sail, your application is running inside a Docker container, and the database is running in a separate container. Therefore, you need to use the container name as the DB_HOST value in your .env file.

To run your seeders, you can use the following command:

sail artisan db:seed

This will run the seeders inside the Docker container, and you don't need to change the DB_HOST value in your .env file.

If you want to run the seeders outside of the Docker container, you can use the following command:

php artisan db:seed --database=mysql

This will use the mysql database connection defined in your config/database.php file, which should have the correct DB_HOST value for running outside of the Docker container.

1 like
rayplawlor@gmail.com's avatar

Thank you AI! :D

I have to remember that when using Docker, it's inside the container! :)

Please or to participate in this conversation.