#!/usr/bin/env bash # Start a local development container from the cac image for a PHP version. # The PHP version is the IMAGE TAG (cac:php83, cac:php85, ...): -a picks the tag. # `cac:latest` is PHP 8.5 today; this script never uses it. name='' http_port='80' https_port='443' root_path="$(pwd)" verbose='false' phpver='' xdebug='false' docroot_subdir='' # Override to run a locally built image, e.g. CAC_IMAGE=cac:mytest ./local-dev.sh -n x -a 85 # (the tag php is NOT appended to an override; it is used as given). image_base="repo.anhonesthost.net/cloud-hosting-platform/cac" while getopts 'n:p:s:r:a:d:xvh' flag; do case "${flag}" in n) name="${OPTARG}" ;; p) http_port="${OPTARG}" ;; s) https_port="${OPTARG}" ;; r) root_path="${OPTARG}" ;; a) phpver="${OPTARG}" ;; d) docroot_subdir="${OPTARG}" ;; x) xdebug='true' ;; v) verbose='true' ;; h) echo "Variables" echo "-n = Name of Container, Required" echo "-p = Non-https Port Override, default 80" echo "-s = Https Port Override, default 443" echo "-r = Root Path for files and database, defaults to current working path" echo "-a = PHP version, one of 74 80 81 82 83 84 85 86 (or 8.3 style), default 83." echo " The version is the image tag (cac:phpNN); cac:latest is PHP 8.5 and is not used." echo "-d = Web root folder inside public_html, e.g. public for Laravel/CodeIgniter (DOCROOT_SUBDIR)" echo "-x = Install and enable Xdebug (XDEBUG=1), client host.docker.internal:9003" echo "-v = Enable Verbose Mode" exit 1 ;; *) exit 1 ;; esac done [ "$verbose" = 'true' ] && set -x #Check if Docker is installed echo "Checking if Docker is Installed..." check_docker=$(command -v docker) if [ -z "$check_docker" ]; then echo "Docker must be installed to run this application" exit 1 fi echo "Docker looks to be installed" if [ -z "$name" ]; then echo "Name not set, please set it with -n" exit 1 fi # 8.3 -> 83; default 83. phpver="${phpver//./}" if [ -z "$phpver" ]; then phpver=83 fi if ! [[ "$phpver" =~ ^(74|80|81|82|83|84|85|86)$ ]]; then echo "Unsupported PHP version '$phpver': use one of 74 80 81 82 83 84 85 86" exit 1 fi if [ -n "${CAC_IMAGE:-}" ]; then image="$CAC_IMAGE" pull='missing' else image="$image_base:php$phpver" pull='always' fi user=$(whoami) uid=$(id -u) if [ "$uid" = 0 ]; then echo "Run this as your normal user, not root (files in ./user are owned by you)." exit 1 fi # The container's own entrypoint creates user/logs and user/public_html as your # uid; do NOT bind-mount anything over /var/log or /etc/httpd/logs (the # entrypoint moves /var/log/httpd itself and refuses to start on a busy mount). mkdir -p "$root_path/user" extra_env=() [ -n "$docroot_subdir" ] && extra_env+=(-e "DOCROOT_SUBDIR=$docroot_subdir") [ "$xdebug" = 'true' ] && extra_env+=(-e XDEBUG=1) echo "Starting $image ..." $check_docker volume create "$name-mysql" if ! $check_docker run --pull="$pull" -d -p "$http_port":80 -p "$https_port":443 \ -e PHPVER="$phpver" -e environment=DEV -e PUBLIC_HTTPS_PORT="$https_port" \ --add-host host.docker.internal:host-gateway \ --mount type=bind,source="$root_path"/user,target=/home/"$user" \ -v "$name-mysql":/var/lib/mysql \ -e uid="$uid" -e user="$user" -e domain="$name-local.dev" \ "${extra_env[@]}" --name "$name" "$image"; then echo "docker run failed (see above)" exit 1 fi echo "Creating management scripts in root directory..." { echo "#!/usr/bin/env bash" echo "docker start $name" } > "$root_path/instance_start" { echo "#!/usr/bin/env bash" echo "docker stop $name" } > "$root_path/instance_stop" { echo "#!/usr/bin/env bash" echo "docker logs -f $name" } > "$root_path/instance_logs" { echo "#!/usr/bin/env bash" echo "docker exec $name cat /home/$user/mysql_creds" } > "$root_path/instance_db_info" chmod +x "$root_path"/instance_* # DEV mode installs MariaDB and memcached (and Xdebug if asked) on first start: # wait for the container to report healthy (or die) instead of a fixed sleep. echo "Waiting for the container to become healthy (first start installs MariaDB and memcached; up to 5 minutes)..." status='' for _ in $(seq 1 150); do state=$($check_docker inspect -f '{{.State.Status}}' "$name" 2>/dev/null) status=$($check_docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{end}}' "$name" 2>/dev/null) if [ "$state" != "running" ]; then echo "Container is $state; last log lines:" $check_docker logs --tail 40 "$name" 2>&1 exit 1 fi # healthy = Apache answers; the credentials file appears once MariaDB is up. if [ "$status" = "healthy" ] && $check_docker exec "$name" test -s "/home/$user/mysql_creds" 2>/dev/null; then break fi sleep 2 done if [ "$status" != "healthy" ]; then echo "Container did not become healthy in time (status: $status); last log lines:" $check_docker logs --tail 40 "$name" 2>&1 exit 1 fi # Read the generated credentials (written by the entrypoint to /home//mysql_creds). creds=$($check_docker exec "$name" cat "/home/$user/mysql_creds") wpdbuser=$(echo "$creds" | grep User | awk -F ": " '{print $2}') wpdbpass=$(echo "$creds" | grep Password | awk -F ": " '{print $2}') wpdb=$(echo "$creds" | grep Database | awk -F ": " '{print $2}') # Install WordPress only into an empty web root (re-running must not clobber your files). webroot="/home/$user/public_html" [ -n "$docroot_subdir" ] && webroot="$webroot/$docroot_subdir" if $check_docker exec "$name" bash -c "[ -z \"\$(ls -A '$webroot')\" ]"; then echo "Installing WordPress..." $check_docker exec -u "$uid" -e HOME="/home/$user" -w "$webroot" "$name" bash -c \ "wp core download && wp config create --dbname='$wpdb' --dbuser='$wpdbuser' --dbpass='$wpdbpass'" else echo "$webroot is not empty: skipping the WordPress download." fi echo "Local Development Instance Created, to stop run ./instance_stop from within the base directory" echo "PHP: $($check_docker exec "$name" php -r 'echo PHP_VERSION;') (image tag php$phpver)" echo "URL: http://localhost:$http_port (HTTPS https://localhost:$https_port, self-signed); site name $name-local.dev" echo "MySQL DB Credentials" echo "$creds" exit 0