Start/stop Laravel Docker Container Bash Script


The purpose of this bash script is to automataclly start up or shutdown a laravel sail project with docker.

To run this script follow these instructions:

  1. Create a new file called laravel.sh one level outside the project folder
  2. Copy and paste the code below and save the file
  3. Change the permission to allow to execute:
    $ chmod +x laravel.sh
  4. Run the script
    ./laravel.sh
  5. Done

 

laravel.sh


#!/bin/bash

clear

APPNAME_DEFAULT="api"
read -p "Enter The App you want to Start: [$APPNAME_DEFAULT/] " APPNAME
if [[ $APPNAME == "" ]]
then
    APPNAME=$APPNAME_DEFAULT
    set -e
fi

#https://stackoverflow.com/questions/59838/how-can-i-check-if-a-directory-exists-in-a-bash-shell-script
# check if directory exists
if [ ! -d "$APPNAME" ]; then
  echo "FATAL FERROR: $APPNAME DOES NOT EXIST. Try Again."
  set -e
  exit
fi
echo "[OK] App was found: $APPNAME"

STATUS_DEFAULT="up -d"
read -p "App Action: up/down [$STATUS_DEFAULT/] " STATUS
if [[ $STATUS == "" ]]
then
    STATUS=$STATUS_DEFAULT
    set -e
fi


cd $APPNAME
./vendor/bin/sail $STATUS

if [[ $STATUS == STATUS_DEFAULT ]]
then
  # ALWAYS PUT THIS AT THE END OF THE SCRIPT
  echo "Opening browser with localhost"
  xdg-open http://localhost
fi