local-dev.sh: start a working container end to end

- mkdir of user/logs/{apache,system} was quoted (literal {apache,system} dir) and is no longer needed: the entrypoint creates logs itself
- drop the /var/log and /etc/httpd/logs bind mounts, which collided with the entrypoint (mv of /var/log/httpd: Device or resource busy, then 'unsafe entry' refusal to start)
- read credentials from /home/<user>/mysql_creds (where the entrypoint writes them), not /var/lib/mysql/creds
- -a N (and the default 83) now selects the image tag cac:phpNN instead of always pulling latest (latest is PHP 8.5); validated; CAC_IMAGE overrides for a locally built image
- wait for the container to be healthy (or fail with its logs) instead of sleep 160; WordPress download runs as the user and only into an empty web root
- instance_logs/instance_db_info use docker logs / the right creds path
- new flags -d (DOCROOT_SUBDIR) and -x (XDEBUG) and PUBLIC_HTTPS_PORT are passed through; they take effect with the image changes in the following commits

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-23 06:03:23 -08:00
1 parent d4ec8b526c
commit ce9fd87183
4 files changed
+323 -38

No files matched your search

+121 -27
View File
@@ -1,33 +1,49 @@
#!/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<NN> 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:vh' flag; do
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 App Version, Default to 8.3"
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 [ $? != 0 ]; then
if [ -z "$check_docker" ]; then
echo "Docker must be installed to run this application"
exit 1
fi
@@ -37,37 +53,115 @@ if [ -z "$name" ]; then
exit 1
fi
# 8.3 -> 83; default 83.
phpver="${phpver//./}"
if [ -z "$phpver" ]; then
phpver=83;
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
echo "Building Docker Image..."
user=$(whoami)
uid=$(id -u)
if [ ! -d "$root_path/user" ]; then
mkdir -p "$root_path/user";
mkdir -p "$root_path/user/logs/{apache,system}";
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"
$check_docker run --pull=always -d -p "$http_port":80 -p "$https_port":443 -e PHPVER=$phpver -e environment=DEV --mount type=bind,source="$root_path"/user,target=/home/"$user" --mount type=bind,source="$(pwd)"/user/logs/apache,target=/etc/httpd/logs --mount type=bind,source="$(pwd)"/user/logs/system,target=/var/log -v"$name-mysql":/var/lib/mysql -e uid="$uid" -e user="$user" -e domain="$name-local.dev" --name "$name" repo.anhonesthost.net/cloud-hosting-platform/cac:latest
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" > "$root_path/instance_start"
echo "docker start $name" >> "$root_path/instance_start"
echo "#!/usr/bin/env bash" > "$root_path/instance_stop"
echo "docker stop $name" >> "$root_path/instance_stop"
echo "#!/usr/bin/env bash" > "$root_path/instance_logs"
echo "docker exec $name bash -c 'tail -f /etc/httpd/logs/*'" >> "$root_path/instance_logs"
echo "#!/usr/bin/env bash" > "$root_path/instance_db_info"
echo "docker exec $name cat /var/lib/mysql/creds" >> "$root_path/instance_db_info"
chmod +x $root_path/instance_*
echo "Waiting 160 seconds for setup to finish"
sleep 160;
echo "Installing WordPress..."
wpdbuser=$(docker exec $name cat /var/lib/mysql/creds |grep User| awk -F ": " {'print $2'})
wpdbpass=$(docker exec $name cat /var/lib/mysql/creds |grep Password| awk -F ": " {'print $2'})
wpdb=$(docker exec $name cat /var/lib/mysql/creds |grep Database| awk -F ": " {'print $2'})
docker exec $name bash -c "cd /home/$(whoami)/public_html; wp core download; wp config create --dbname=$wpdb --dbuser=$wpdbuser --dbpass=$wpdbpass ; chown -R $(whoami):$(whoami) /home/$(whoami)/public_html;"
{
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/<user>/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"
docker exec $name cat /var/lib/mysql/creds
exit 0
echo "$creds"
exit 0