Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucafregoso/3c11ceadc03f4940793edf298419ed0e to your computer and use it in GitHub Desktop.
Save lucafregoso/3c11ceadc03f4940793edf298419ed0e to your computer and use it in GitHub Desktop.
Setup Laravel Sail from source control

Laravel Sail Setup

This is a simple script along with some useful information to help when setting up a Laravel Sail project that was cloned from source control. I made this because I could not find anywhere that explained how to do this and kept running into problems. The main issue was a problem with database authentication. It turns out that when the Docker volume is created for the database for the first time the password that is in the .env file is used, which I usually created after starting Sail for the first time. Another issue was not having access to the Sail binary to start the containers. This is solved by the script by installing the composer dependencies with a disposable container. If you follow the steps below in order you should not run into any issues. Feel free to leave a comment on this Gist if it could be improved.

How to use

  1. Run the script from the root directory of the Laravel project
curl -s "https://gist.githubusercontent.com/LaurenceRawlings/3b4f801cafb2e683f45a3b573dad868d/raw/15c952586e75d514b909aec6a6e47088563c6612/sail-setup" | bash
  • This script can be found at the bottom of the page
  1. Configure a bash alias
alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
  1. Add a .env file See below for default Sail values
cp .env.example .env
  1. Start Sail
sail up -d
  1. Generate app key
sail artisan key:generate
sail atrisan optimize
  1. Migrate database
sail artisan migrate

Fix database authentication issue

Illuminate\Database\QueryException

SQLSTATE[08006] [7] FATAL:  password authentication failed for user "sail" (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

This happens when Sail was started for the first time and the env variables for the database were not set correctly Make sure to update env variables before starting Sail to avoid database authentication issues.

If you get this issue when migrating your database try the following:

  • Stop Sail
sail down
  • Remove existing database Docker volume
docker volume ls
docker volume rm example-app_sailmysql
  • Make sure your databse env variables are all set correctly (see below)
    • Sail uses the database credentials in your env file when creating the Docker volume for the frist time
  • Restart Sail
sail up -d

Optional extras:

sail artisan storage:link
sail npm install

Laravel Sail default env variables

Replace the relevant env variable for the tools you are using in your stack

Any

DB_USERNAME=sail
DB_PASSWORD=password

MySQL

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306

PostgreSQL

DB_CONNECTION=pgsql
DB_PORT=5432
DB_HOST=pgsql

MariaDB

DB_HOST=mariadb

Memcached

MEMCACHED_HOST=memcached

Redis

REDIS_HOST=redis

Meilisearch

SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://meilisearch:7700
#!/bin/bash
# Usage:
# curl -s "https://gist.githubusercontent.com/LaurenceRawlings/3b4f801cafb2e683f45a3b573dad868d/raw/15c952586e75d514b909aec6a6e47088563c6612/sail-setup" | bash
# Ensure that Docker is running
docker info > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Docker is not running."
exit 1
fi
echo "Installing composer dependencies..."
# Install composer dependanices
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment