Files
caiwuzongzhang/start-docker.sh
2026-08-07 09:43:34 +08:00

49 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env sh
set -eu
APP_PORT="${APP_PORT:-${1:-4173}}"
export APP_PORT
case "$APP_PORT" in
''|*[!0-9]*)
echo "APP_PORT must be an integer between 1 and 65535." >&2
exit 1
;;
esac
if [ "$APP_PORT" -lt 1 ] || [ "$APP_PORT" -gt 65535 ]; then
echo "APP_PORT must be an integer between 1 and 65535." >&2
exit 1
fi
docker compose version >/dev/null
docker info >/dev/null
docker compose up --build --detach
container_id="$(docker compose ps --quiet app)"
if [ -z "$container_id" ]; then
echo "The application container was not created." >&2
exit 1
fi
attempt=0
while [ "$attempt" -lt 60 ]; do
state="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")"
case "$state" in
healthy)
echo "Application is ready: http://localhost:${APP_PORT}"
exit 0
;;
unhealthy|exited|dead)
docker compose logs --no-color --tail 100 app
echo "The application container entered state: $state" >&2
exit 1
;;
esac
attempt=$((attempt + 1))
sleep 1
done
docker compose logs --no-color --tail 100 app
echo "Timed out waiting for the application health check." >&2
exit 1