Skip to content

Instantly share code, notes, and snippets.

@bvelastegui
Created April 22, 2026 14:37
Show Gist options
  • Select an option

  • Save bvelastegui/6ddc59f504f67676fdb1efa106c1c5e3 to your computer and use it in GitHub Desktop.

Select an option

Save bvelastegui/6ddc59f504f67676fdb1efa106c1c5e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
UNAME_OUT="$(uname -s)"
# Verify operating system is supported...
case "${UNAME_OUT}" in
Linux*) MACHINE=linux ;;
Darwin*) MACHINE=mac ;;
*) MACHINE="UNKNOWN" ;;
esac
if [ "$MACHINE" == "UNKNOWN" ]; then
echo "Unsupported operating system [$(uname -s)]. Only supports macOS, Linux, and Windows (WSL2)." >&2
exit 1
fi
# Determine if stdout is a terminal...
if test -t 1; then
# Determine if colors are supported...
ncolors=$(tput colors)
if test -n "$ncolors" && test "$ncolors" -ge 8; then
BOLD="$(tput bold)"
YELLOW="$(tput setaf 3)"
GREEN="$(tput setaf 2)"
NC="$(tput sgr0)"
fi
fi
# Function that prints the available commands...
function display_help {
echo "Laradock"
echo
echo "${YELLOW}Usage:${NC}" >&2
echo " laradock COMMAND [options] [arguments]"
echo
echo "Unknown commands are passed to the docker-compose binary."
echo
echo "${YELLOW}docker-compose Commands:${NC}"
echo " ${GREEN}laradock up${NC} Start the application"
echo " ${GREEN}laradock up -d${NC} Start the application in the background"
echo " ${GREEN}laradock stop${NC} Stop the application"
echo " ${GREEN}laradock restart${NC} Restart the application"
echo " ${GREEN}laradock ps${NC} Display the status of all containers"
echo
echo "${YELLOW}Artisan Commands:${NC}"
echo " ${GREEN}laradock artisan ...${NC} Run an Artisan command"
echo " ${GREEN}laradock artisan queue:work${NC}"
echo
echo "${YELLOW}PHP Commands:${NC}"
echo " ${GREEN}laradock php ...${NC} Run a snippet of PHP code"
echo " ${GREEN}laradock php -v${NC}"
echo
echo "${YELLOW}Composer Commands:${NC}"
echo " ${GREEN}laradock composer ...${NC} Run a Composer command"
echo " ${GREEN}laradock composer require laravel/sanctum${NC}"
echo
echo "${YELLOW}Node Commands:${NC}"
echo " ${GREEN}laradock node ...${NC} Run a Node command"
echo " ${GREEN}laradock node --version${NC}"
echo
echo "${YELLOW}NPM Commands:${NC}"
echo " ${GREEN}laradock npm ...${NC} Run a npm command"
echo " ${GREEN}laradock npx${NC} Run a npx command"
echo " ${GREEN}laradock npm run prod${NC}"
echo
echo "${YELLOW}Database Commands:${NC}"
echo " ${GREEN}laradock mysql${NC} Start a MySQL CLI session within the 'mysql' container"
echo " ${GREEN}laradock redis${NC} Start a Redis CLI session within the 'redis' container"
echo
echo "${YELLOW}Container CLI:${NC}"
echo " ${GREEN}laradock shell${NC} Start a shell session within the workspace container"
echo " ${GREEN}laradock bash${NC} Alias for 'laradock shell'"
echo " ${GREEN}laradock root-shell${NC} Start a root shell session within the workspace container"
echo " ${GREEN}laradock root-bash${NC} Alias for 'laradock root-shell'"
echo " ${GREEN}laradock tinker${NC} Start a new Laravel Tinker session"
echo
echo "${YELLOW}Binaries:${NC}"
echo " ${GREEN}laradock bin ...${NC} Run Composer binary scripts from the vendor/bin directory"
echo
echo "${YELLOW}Customization:${NC}"
echo " ${GREEN}laradock build --no-cache${NC} Rebuild all of the laradock containers"
exit 1
}
# Proxy the "help" command...
if [ $# -gt 0 ]; then
if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
display_help
fi
else
display_help
fi
# Source the ".env" file so Laravel's environment variables are available...
# shellcheck source=/dev/null
if [ -n "$APP_ENV" ] && [ -f ./.env."$APP_ENV" ]; then
source ./.env."$APP_ENV"
elif [ -f ./.env ]; then
source ./.env
fi
export LARADOCK_FILES="../laradock-*/docker-compose.yml"
export WORKSPACE_SERVICE="workspace"
export WORKING_DIR=${PWD##*/}
function laradock_is_not_running {
echo "${BOLD}Laradock is not running.${NC}" >&2
echo "" >&2
echo "${BOLD}You may laradock using the following commands:${NC} 'laradock up' or 'laradock up -d'" >&2
exit 1
}
# Define Docker Compose command prefix...
if docker compose &>/dev/null; then
DOCKER_COMPOSE=(docker compose)
else
DOCKER_COMPOSE=(docker-compose)
fi
if [ -n "$LARADOCK_FILES" ]; then
for FILE in $LARADOCK_FILES; do
if [ -f "$FILE" ]; then
DOCKER_COMPOSE+=(-f "$FILE")
else
echo "${BOLD}Unable to find Docker Compose file: '${FILE}'${NC}" >&2
exit 1
fi
done
fi
EXEC="yes"
if [ -z "$LARADOCK_SKIP_CHECKS" ]; then
# Ensure that Docker is running...
if ! docker info >/dev/null 2>&1; then
echo "${BOLD}Docker is not running.${NC}" >&2
exit 1
fi
# Determine if laradock is currently up...
if "${DOCKER_COMPOSE[@]}" ps "$WORKSPACE_SERVICE" 2>&1 | grep 'Exit\|exited'; then
echo "${BOLD}Shutting down old laradock processes...${NC}" >&2
"${DOCKER_COMPOSE[@]}" down >/dev/null 2>&1
EXEC="no"
elif [ -z "$("${DOCKER_COMPOSE[@]}" ps -q)" ]; then
EXEC="no"
fi
fi
ARGS=()
# Proxy PHP commands to the "php" binary on the workspace container...
if [ "$1" == "php" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" "php")
else
laradock_is_not_running
fi
# Proxy vendor binary commands on the workspace container...
elif [ "$1" == "bin" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
CMD=$1
shift 1
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" ./vendor/bin/"$CMD")
else
laradock_is_not_running
fi
# Proxy docker-compose commands to the docker-compose binary on the workspace container...
elif [ "$1" == "docker-compose" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" "${DOCKER_COMPOSE[@]}")
else
laradock_is_not_running
fi
# Proxy Composer commands to the "composer" binary on the workspace container...
elif [ "$1" == "composer" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" "composer")
else
laradock_is_not_running
fi
# Proxy Artisan commands to the "artisan" binary on the workspace container...
elif [ "$1" == "artisan" ] || [ "$1" == "art" ] || [ "$1" == "a" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php artisan)
else
laradock_is_not_running
fi
# Proxy the "test" command to the "php artisan test" Artisan command...
elif [ "$1" == "test" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php artisan test)
else
laradock_is_not_running
fi
# Proxy the "phpunit" command to "php vendor/bin/phpunit"...
elif [ "$1" == "phpunit" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php vendor/bin/phpunit)
else
laradock_is_not_running
fi
# Proxy the "pest" command to "php vendor/bin/pest"...
elif [ "$1" == "pest" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php vendor/bin/pest)
else
laradock_is_not_running
fi
# Proxy the "pint" command to "php vendor/bin/pint"...
elif [ "$1" == "pint" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php vendor/bin/pint)
else
laradock_is_not_running
fi
# Proxy the "dusk" command to the "php artisan dusk" Artisan command...
elif [ "$1" == "dusk" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=(-e "APP_URL=http://${WORKSPACE_SERVICE}")
ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
ARGS+=("$WORKSPACE_SERVICE" php artisan dusk)
else
laradock_is_not_running
fi
# Proxy the "dusk:fails" command to the "php artisan dusk:fails" Artisan command...
elif [ "$1" == "dusk:fails" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=(-e "APP_URL=http://${WORKSPACE_SERVICE}")
ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
ARGS+=("$WORKSPACE_SERVICE" php artisan dusk:fails)
else
laradock_is_not_running
fi
# Initiate a Laravel Tinker session within the workspace container...
elif [ "$1" == "tinker" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" php artisan tinker)
else
laradock_is_not_running
fi
# Proxy Node commands to the "node" binary on the workspace container...
elif [ "$1" == "node" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" node)
else
laradock_is_not_running
fi
# Proxy NPM commands to the "npm" binary on the workspace container...
elif [ "$1" == "npm" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" npm)
else
laradock_is_not_running
fi
# Proxy NPX commands to the "npx" binary on the workspace container...
elif [ "$1" == "npx" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" npx)
else
laradock_is_not_running
fi
# Initiate a MySQL CLI terminal session within the "mysql" container...
elif [ "$1" == "mysql" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec)
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=(mysql bash -c)
ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}")
else
laradock_is_not_running
fi
# Initiate a Bash shell within the workspace container...
elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u laradock --workdir "/var/www/$WORKING_DIR")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" bash)
else
laradock_is_not_running
fi
# Initiate a root user Bash shell within the workspace container...
elif [ "$1" == "root-shell" ] || [ "$1" == "root-bash" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec -u root)
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$WORKSPACE_SERVICE" bash)
else
laradock_is_not_running
fi
# Initiate a Redis CLI terminal session within the "redis" container...
elif [ "$1" == "redis" ]; then
shift 1
if [ "$EXEC" == "yes" ]; then
ARGS+=(exec)
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=(redis redis-cli)
else
laradock_is_not_running
fi
fi
# Run Docker Compose with the defined arguments...
"${DOCKER_COMPOSE[@]}" "${ARGS[@]}" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment