sjwmdev/laravel-sail-mysql

By sjwmdev

Updated over 1 year ago

Laravel Sail with Docker and MySQL Server support

Image
Databases & Storage
Languages & Frameworks
Developer Tools

28

Setting up Laravel with Docker and Sail on Windows 11

Prerequisites

Installing WSL

Follow these 6 steps to install WSL:

Step 1 - Enable WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Step 3 - Check requirements for running WSL 2

(Ensure you meet the version and build number criteria) Update to the latest Windows version in the Settings menu if needed.

Step 3 - Enable Virtual Machine feature
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Step 4 - Download the Linux kernel update package

(Choose the appropriate package based on your system type) Install the package and proceed to the next step.

wsl.exe --installorwsl.exe --update
Step 5 - Set WSL 2 as the default version
wsl --set-default-version 2
Step 6 - Install Linux Distribution

Visit the Microsoft Store and select your preferred Linux distribution (e.g., Ubuntu). Launch the newly installed distribution, and create a user account and password.

Install Windows Terminal (Optional)

Install Windows Terminal for enhanced command line functionality.

Installing Docker Desktop

Download and run Docker Desktop Installer.

Follow the installation prompts, ensuring to select the appropriate backend option. Start Docker Desktop after successful installation.

Install Laravel Project

Create a new Laravel project
curl -s https://laravel.build/example-app | bash
Navigate to the project directory and start Laravel Sail
cd example-app
./vendor/bin/sail up

Optionally, create a shell alias for Sail commands.

Add to your shell configuration file (e.g., ~/.zshrc or ~/.bashrc)

alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail'

Restart your shell.

Starting & Stopping Sail

Start Laravel Sail
sail up
Start in detached mode
sail up -d

Access the project in your browser at http://localhost

Stop all containers
sail stop

Connecting to Laravel Sail with MySQL Service

To enable graphical interaction with the database, you'll need to add the phpmyadmin service and configure the DB_HOST, DB_USERNAME, and DB_PASSWORD in your .env file.

After updating the docker-compose.yml file, access the phpMyAdmin interface using either of these URLs in your web browser: 127.0.0.1:8081 or localhost:8081. You'll be prompted to enter server details, username, and password.

The login credentials depend on what's specified in your project's .env file. On the server, use "mysql" as the username. With this setup, you're ready to kickstart your development with Laravel and PhpMyAdmin.

Important Notes

  • To change the database service, edit the docker-compose.yml file.
  • Install PhpMyAdmin for MySQL or PgAdmin 4 for pgsql for graphical database interaction.

Troubleshooting Notes

If you encounter a RuntimeException like:

In order to use the Auth::routes() method, please install the laravel/ui package.

To resolve this issue, you'll need to install the laravel/ui package. This package provides a simple way to scaffold authentication views and routes.

You can install it using Composer with the following command:

composer require laravel/ui

If you receive a message like:

Do not run Composer as root/super user! See https://getcomposer.org/root for details

Instead, it's best to run Composer as a regular user. If you need to install global packages, you can use the --global flag, but still without sudo.

Here's the proper way to install the laravel/ui package without using sudo:

composer require laravel/ui --dev

The --dev flag is optional and is used if you only need this package for development purposes. If you need it in production, you can omit this flag.

After installing the package, you can generate the necessary views and routes for authentication using Artisan:

php artisan ui bootstrap --auth

Replace Bootstrap with the front-end framework you're using (e.g., vue, react, etc.) if it's different.

This will generate the authentication views in your resources/views/auth directory, along with the necessary routes.

Remember to clear your config cache if you've previously cached it:

php artisan config:clear

After doing this, try running your Laravel application again.

If you're using Docker and Laravel Sail, you might need to rebuild your Docker containers to make sure the changes take effect:

sail downsail up -d

Make sure to backup any important files or configurations before making these changes.

Happy coding! 😊

Docker Pull Command

docker pull sjwmdev/laravel-sail-mysql