Improve local Docker runner: auto-pick free port
Browse files- docs/hf.md +6 -1
- run_docker_local.sh +38 -1
docs/hf.md
CHANGED
|
@@ -32,6 +32,12 @@ Run the Docker image locally before pushing to HF:
|
|
| 32 |
|
| 33 |
Then open `http://localhost:7860` and click **Start Conversation**.
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
## Deploy (push to Space repo)
|
| 36 |
|
| 37 |
The Space is also configured as a git remote locally (`hf`).
|
|
@@ -55,4 +61,3 @@ git push --force hf main
|
|
| 55 |
- Confirm `PORT` is being respected (HF sets it automatically; we bind to `0.0.0.0:$PORT`).
|
| 56 |
- **OpenRouter errors**
|
| 57 |
- Confirm `LLM_API_KEY` secret is set and `LLM_MODEL` is valid on OpenRouter.
|
| 58 |
-
|
|
|
|
| 32 |
|
| 33 |
Then open `http://localhost:7860` and click **Start Conversation**.
|
| 34 |
|
| 35 |
+
If `7860` is already in use locally, run:
|
| 36 |
+
|
| 37 |
+
```bash
|
| 38 |
+
HOST_PORT=7861 ./run_docker_local.sh
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
## Deploy (push to Space repo)
|
| 42 |
|
| 43 |
The Space is also configured as a git remote locally (`hf`).
|
|
|
|
| 61 |
- Confirm `PORT` is being respected (HF sets it automatically; we bind to `0.0.0.0:$PORT`).
|
| 62 |
- **OpenRouter errors**
|
| 63 |
- Confirm `LLM_API_KEY` secret is set and `LLM_MODEL` is valid on OpenRouter.
|
|
|
run_docker_local.sh
CHANGED
|
@@ -10,6 +10,44 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
| 10 |
echo "Building Docker image: ${IMAGE_NAME}"
|
| 11 |
docker build -t "${IMAGE_NAME}" "${ROOT_DIR}"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
ENV_ARGS=()
|
| 14 |
if [[ -f "${ROOT_DIR}/.env" ]]; then
|
| 15 |
ENV_ARGS+=(--env-file "${ROOT_DIR}/.env")
|
|
@@ -21,4 +59,3 @@ exec docker run --rm -it \
|
|
| 21 |
-e "PORT=${CONTAINER_PORT}" \
|
| 22 |
"${ENV_ARGS[@]}" \
|
| 23 |
"${IMAGE_NAME}"
|
| 24 |
-
|
|
|
|
| 10 |
echo "Building Docker image: ${IMAGE_NAME}"
|
| 11 |
docker build -t "${IMAGE_NAME}" "${ROOT_DIR}"
|
| 12 |
|
| 13 |
+
port_in_use() {
|
| 14 |
+
local port="$1"
|
| 15 |
+
if command -v ss >/dev/null 2>&1; then
|
| 16 |
+
ss -ltn "sport = :${port}" 2>/dev/null | rg -q ":${port}\\b" && return 0 || return 1
|
| 17 |
+
fi
|
| 18 |
+
if command -v lsof >/dev/null 2>&1; then
|
| 19 |
+
lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1 && return 0 || return 1
|
| 20 |
+
fi
|
| 21 |
+
return 1
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
select_free_host_port() {
|
| 25 |
+
local start_port="$1"
|
| 26 |
+
local port="${start_port}"
|
| 27 |
+
local attempts=20
|
| 28 |
+
|
| 29 |
+
for _ in $(seq 1 "${attempts}"); do
|
| 30 |
+
if ! port_in_use "${port}"; then
|
| 31 |
+
echo "${port}"
|
| 32 |
+
return 0
|
| 33 |
+
fi
|
| 34 |
+
port=$((port + 1))
|
| 35 |
+
done
|
| 36 |
+
|
| 37 |
+
echo "${start_port}"
|
| 38 |
+
return 0
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
if port_in_use "${HOST_PORT}"; then
|
| 42 |
+
FREE_PORT="$(select_free_host_port "${HOST_PORT}")"
|
| 43 |
+
if [[ "${FREE_PORT}" != "${HOST_PORT}" ]]; then
|
| 44 |
+
echo "⚠️ Port ${HOST_PORT} is in use; using ${FREE_PORT} instead."
|
| 45 |
+
HOST_PORT="${FREE_PORT}"
|
| 46 |
+
else
|
| 47 |
+
echo "⚠️ Port ${HOST_PORT} appears to be in use; you can run with HOST_PORT=7861 ./run_docker_local.sh"
|
| 48 |
+
fi
|
| 49 |
+
fi
|
| 50 |
+
|
| 51 |
ENV_ARGS=()
|
| 52 |
if [[ -f "${ROOT_DIR}/.env" ]]; then
|
| 53 |
ENV_ARGS+=(--env-file "${ROOT_DIR}/.env")
|
|
|
|
| 59 |
-e "PORT=${CONTAINER_PORT}" \
|
| 60 |
"${ENV_ARGS[@]}" \
|
| 61 |
"${IMAGE_NAME}"
|
|
|