ConverTA / run_docker_local.sh
MikelWL's picture
Chore: persist /data in local Docker run
0826229
#!/usr/bin/env bash
set -Eeuo pipefail
IMAGE_NAME="${IMAGE_NAME:-converta:local}"
HOST_PORT="${HOST_PORT:-7860}"
CONTAINER_PORT="${CONTAINER_PORT:-7860}"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "Building Docker image: ${IMAGE_NAME}"
docker build -t "${IMAGE_NAME}" "${ROOT_DIR}"
port_in_use() {
local port="$1"
if command -v ss >/dev/null 2>&1; then
ss -ltn "sport = :${port}" 2>/dev/null | grep -q ":${port}\\b" && return 0 || return 1
fi
if command -v lsof >/dev/null 2>&1; then
lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1 && return 0 || return 1
fi
return 1
}
select_free_host_port() {
local start_port="$1"
local port="${start_port}"
local attempts=20
for _ in $(seq 1 "${attempts}"); do
if ! port_in_use "${port}"; then
echo "${port}"
return 0
fi
port=$((port + 1))
done
echo "${start_port}"
return 0
}
if port_in_use "${HOST_PORT}"; then
FREE_PORT="$(select_free_host_port "${HOST_PORT}")"
if [[ "${FREE_PORT}" != "${HOST_PORT}" ]]; then
echo "⚠️ Port ${HOST_PORT} is in use; using ${FREE_PORT} instead."
HOST_PORT="${FREE_PORT}"
else
echo "⚠️ Port ${HOST_PORT} appears to be in use; you can run with HOST_PORT=7861 ./run_docker_local.sh"
fi
fi
ENV_ARGS=()
if [[ -f "${ROOT_DIR}/.env" ]]; then
ENV_ARGS+=(--env-file "${ROOT_DIR}/.env")
fi
# HF Spaces provide a persistent volume at `/data`. For HF-like local runs, mount
# a host directory into `/data` and default DB_PATH into that mount.
LOCAL_DATA_DIR="${ROOT_DIR}/.localdata"
mkdir -p "${LOCAL_DATA_DIR}"
INTERNAL_DB_PATH="${DB_PATH:-/data/converta/converta.db}"
# The app runs as a single process with the backend mounted at `/api`, so the
# in-container websocket target must point at the same server, not :8000.
INTERNAL_WS_URL="ws://127.0.0.1:${CONTAINER_PORT}/api/ws/conversation"
INTERNAL_API_BASE="http://127.0.0.1:${CONTAINER_PORT}/api"
echo "Running container on http://localhost:${HOST_PORT}"
exec docker run --rm -it \
-p "${HOST_PORT}:${CONTAINER_PORT}" \
-v "${LOCAL_DATA_DIR}:/data" \
-e "PORT=${CONTAINER_PORT}" \
-e "FRONTEND_WEBSOCKET_URL=${INTERNAL_WS_URL}" \
-e "FRONTEND_BACKEND_BASE_URL=${INTERNAL_API_BASE}" \
-e "DB_PATH=${INTERNAL_DB_PATH}" \
"${ENV_ARGS[@]}" \
"${IMAGE_NAME}"