Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Docker: let UNSLOTH_STUDIO_PORT change Studio's port inside the container (#11045)
Nilay90cab2b52c78
diff --git a/docker/DOCKERHUB.md b/docker/DOCKERHUB.mdindex 0b9f81391d2..cdd71fda7b7 100644--- a/docker/DOCKERHUB.md+++ b/docker/DOCKERHUB.md@@ -118,7 +118,7 @@ Turing has no bfloat16; Unsloth falls back to float16 there. These images are CU| Port | Service | Image ||---|---|---|-| 8000 | Unsloth Studio | `latest` |+| 8000 | Unsloth Studio (`UNSLOTH_STUDIO_PORT`) | `latest` || 8888 | JupyterLab | both || 22 | SSH, key only, off unless `SSH_KEY` or `PUBLIC_KEY` is set | `latest` |@@ -128,7 +128,8 @@ Turing has no bfloat16; Unsloth falls back to float16 there. These images are CU|---|---|| `UNSLOTH_STUDIO_PASSWORD` | Initial Studio password for user `unsloth`; ignored once one is stored. Unset: generated, printed in the logs, and Studio stops after an hour unless changed (`UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT=0` disables). || `JUPYTER_PASSWORD` | JupyterLab password, read by the `latest` launcher. Unset: generated and printed in the logs. On `core` you start JupyterLab yourself. |-| `JUPYTER_PORT` | JupyterLab's port inside the container. Default `8888`. `8000` is refused: that is Studio's. |+| `JUPYTER_PORT` | JupyterLab's port inside the container. Default `8888`. Studio's port (`UNSLOTH_STUDIO_PORT`) is refused. |+| `UNSLOTH_STUDIO_PORT` | Studio's port inside the container, for `--network host` when 8000 is taken. Default `8000`. || `SSH_KEY` or `PUBLIC_KEY` | OpenSSH public key for root login; enables sshd on port 22. Password login is never enabled. || `UNSLOTH_ALLOW_CPU=1` | Allow starting without a GPU (`latest` already does). Dropped when a GPU is visible, where it would turn off Unsloth's training patches. || `UNSLOTH_STUDIO_SECURE=1` | Serve Studio over a Cloudflare HTTPS link only, bound to loopback inside the container so no raw port is published. Fails closed if the tunnel does not come up. |diff --git a/docker/Dockerfile.studio b/docker/Dockerfile.studioindex aeb2b2bb935..fef3e7d6ca7 100644--- a/docker/Dockerfile.studio+++ b/docker/Dockerfile.studio@@ -66,6 +66,7 @@ ENV UNSLOTH_STUDIO_HOME=/opt/unsloth-studio \UV_CACHE_DIR=/opt/unsloth-studio-app/uv-cache \UNSLOTH_IMAGE_ALLOW_CPU=1 \JUPYTER_PORT=8888 \+ UNSLOTH_STUDIO_PORT=8000 \UNSLOTH_ENABLE_SSHD=false \DEBIAN_FRONTEND=noninteractivediff --git a/docker/run.sh b/docker/run.shindex b7833b1b66d..5df9e12b7ad 100755--- a/docker/run.sh+++ b/docker/run.sh@@ -308,6 +308,7 @@ declare -a ENV_FORWARD=(-e HF_HUB_ENABLE_HF_TRANSFER=1)# read by studio_launch.sh; without these it uses a random password and no sshd[[ -n "${JUPYTER_PASSWORD:-}" ]] && ENV_FORWARD+=(-e JUPYTER_PASSWORD)[[ -n "${UNSLOTH_STUDIO_PASSWORD:-}" ]] && ENV_FORWARD+=(-e UNSLOTH_STUDIO_PASSWORD)+[[ -n "${UNSLOTH_STUDIO_PORT:-}" ]] && ENV_FORWARD+=(-e UNSLOTH_STUDIO_PORT)[[ -n "${UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT:-}" ]] && ENV_FORWARD+=(-e UNSLOTH_STUDIO_BOOTSTRAP_TIMEOUT)[[ -n "${PUBLIC_KEY:-}" ]] && ENV_FORWARD+=(-e PUBLIC_KEY)[[ -n "${SSH_KEY:-}" ]] && ENV_FORWARD+=(-e SSH_KEY)diff --git a/docker/studio_launch.sh b/docker/studio_launch.shindex a75506341b9..36edb97150f 100644--- a/docker/studio_launch.sh+++ b/docker/studio_launch.sh@@ -2,7 +2,7 @@# Default CMD of the full Unsloth image (Dockerfile.studio).## Bootstraps the three services managed by supervisord:-# studio port 8000 user unsloth; password from UNSLOTH_STUDIO_PASSWORD, or+# studio port UNSLOTH_STUDIO_PORT (8000) user unsloth; password from UNSLOTH_STUDIO_PASSWORD, or# the generated one printed in `docker logs` (studio-password)# jupyter port 8888 password from JUPYTER_PASSWORD, or a random one# printed in `docker logs` when unset@@ -21,13 +21,24 @@set -euo pipefailexport JUPYTER_PORT="${JUPYTER_PORT:-8888}"-# Studio is fixed at 8000 (studio_run.sh); JupyterLab starts first and wins the bind, so-# Studio silently falls back to an unpublished 8001. Compared as traitlets does, via int():-# whitespace, leading zeros, a leading + and underscores ("08000", " 8000", "8_000") all bind 8000.+export UNSLOTH_STUDIO_PORT="${UNSLOTH_STUDIO_PORT:-8000}"+# at most five significant digits, or bash arithmetic wraps 18446744073709551617 to 1+if ! [[ "$UNSLOTH_STUDIO_PORT" =~ ^0*[0-9]{1,5}$ ]] || (( 10#$UNSLOTH_STUDIO_PORT < 1 || 10#$UNSLOTH_STUDIO_PORT > 65535 )); then+ printf "\033[1;31mERROR:\033[0m UNSLOTH_STUDIO_PORT=%s is not a port number (1-65535).\n" "$UNSLOTH_STUDIO_PORT" >&2+ exit 1+fi+UNSLOTH_STUDIO_PORT=$(( 10#$UNSLOTH_STUDIO_PORT ))+if [[ -n "${SSH_KEY:-${PUBLIC_KEY:-}}" ]] && (( UNSLOTH_STUDIO_PORT == 22 )); then+ printf "\033[1;31mERROR:\033[0m UNSLOTH_STUDIO_PORT=22 is sshd's port inside the container when SSH_KEY or PUBLIC_KEY is set.\n" >&2+ exit 1+fi+# JupyterLab starts first and wins the bind, so Studio silently falls back to an unpublished+# port. Compared as traitlets does, via int(): whitespace, leading zeros, a leading + and+# underscores ("08000", " 8000", "8_000") all bind 8000.jupyter_port_digits="${JUPYTER_PORT//[[:space:]_]/}"jupyter_port_digits="${jupyter_port_digits#+}"-if [[ "$jupyter_port_digits" =~ ^[0-9]+$ ]] && (( 10#$jupyter_port_digits == 8000 )); then- printf "\033[1;31mERROR:\033[0m JUPYTER_PORT=8000 is Unsloth Studio's port inside the container.\n" >&2+if [[ "$jupyter_port_digits" =~ ^[0-9]+$ ]] && (( 10#$jupyter_port_digits == UNSLOTH_STUDIO_PORT )); then+ printf "\033[1;31mERROR:\033[0m JUPYTER_PORT=%s is Unsloth Studio's port inside the container (UNSLOTH_STUDIO_PORT).\n" "$UNSLOTH_STUDIO_PORT" >&2printf " Leave JupyterLab on 8888 and map the host side instead: -p 9000:8888\n" >&2exit 1fi@@ -149,7 +160,7 @@ elsefiunset UNSLOTH_STUDIO_PASSWORDexport UNSLOTH_STUDIO_PASSWORD_STATE # read by unsloth-studio-password-echo "Unsloth Studio -> http://localhost:8000 (${STUDIO_NOTE})"+echo "Unsloth Studio -> http://localhost:${UNSLOTH_STUDIO_PORT} (${STUDIO_NOTE})"echo "JupyterLab -> http://localhost:${JUPYTER_PORT} (${JUPYTER_NOTE})"if [[ "${UNSLOTH_JUPYTER_CLOUDFLARE}" == "1" ]]; thenecho "JupyterLab tunnel-> enabled; public trycloudflare URL appears below once it is up"diff --git a/docker/studio_password.sh b/docker/studio_password.shindex 967511dc560..59d09c75a2d 100755--- a/docker/studio_password.sh+++ b/docker/studio_password.sh@@ -81,7 +81,7 @@ echo "Unsloth Studio login -> ${STUDIO_LINE}"studio_ok=""; jupyter_ok=""deadline=$(( $(date +%s) + READY_WAIT ))while :; do- if [[ -z "$studio_ok" ]] && curl -sf -o /dev/null --max-time 3 http://127.0.0.1:8000/api/health; then+ if [[ -z "$studio_ok" ]] && curl -sf -o /dev/null --max-time 3 "http://127.0.0.1:${UNSLOTH_STUDIO_PORT:-8000}/api/health"; thenstudio_ok=1fiif [[ -z "$jupyter_ok" ]] && curl -sf -o /dev/null --max-time 3 "http://127.0.0.1:${JUPYTER_PORT}/login"; then@@ -113,7 +113,7 @@ firule="$(printf '=%.0s' $(seq 1 72))"printf '%s\n' "${_g}${rule}${_r}"printf ' %s\n' "${_g}${title}${_r}"-printf ' Studio %s %s\n' "${_b}http://localhost:8000${_r}" "${_y}${studio_text}${_r}"+printf ' Studio %s %s\n' "${_b}http://localhost:${UNSLOTH_STUDIO_PORT:-8000}${_r}" "${_y}${studio_text}${_r}"printf ' JupyterLab %s %s\n' "${_b}http://localhost:${JUPYTER_PORT}${_r}" "${_y}${jupyter_text}${_r}"printf ' %s\n' "Ports are the container's: use the host side of your -p flags, or an SSH tunnel to a remote host."printf '%s\n' "${_g}${rule}${_r}"diff --git a/docker/studio_run.sh b/docker/studio_run.shindex 7744b475162..b0cc343e99c 100644--- a/docker/studio_run.sh+++ b/docker/studio_run.sh@@ -85,13 +85,13 @@ fi# Mirrors UNSLOTH_JUPYTER_CLOUDFLARE, which JupyterLab has had all along. The two# are mutually exclusive in the CLI, so refuse the pair here rather than let# supervisord restart Studio forever on an argument error.-STUDIO_ARGS=(-H 0.0.0.0 -p 8000)+STUDIO_ARGS=(-H 0.0.0.0 -p "${UNSLOTH_STUDIO_PORT:-8000}")if [[ "${UNSLOTH_STUDIO_SECURE:-0}" == "1" && "${UNSLOTH_STUDIO_CLOUDFLARE:-0}" == "1" ]]; thenecho "ERROR: set UNSLOTH_STUDIO_SECURE=1 or UNSLOTH_STUDIO_CLOUDFLARE=1, not both:" >&2echo " --secure serves only the tunnel, --cloudflare serves the tunnel and the local port." >&2exit 2elif [[ "${UNSLOTH_STUDIO_SECURE:-0}" == "1" ]]; then- STUDIO_ARGS=(--secure -p 8000)+ STUDIO_ARGS=(--secure -p "${UNSLOTH_STUDIO_PORT:-8000}")elif [[ "${UNSLOTH_STUDIO_CLOUDFLARE:-0}" == "1" ]]; thenSTUDIO_ARGS+=(--cloudflare)fidiff --git a/docker/supervisord.conf b/docker/supervisord.confindex 7d01f1e407f..f530239fd69 100644--- a/docker/supervisord.conf+++ b/docker/supervisord.conf@@ -1,5 +1,5 @@# Service manager for the full Unsloth image (Dockerfile.studio):-# studio Unsloth Studio web UI port 8000+# studio Unsloth Studio web UI port $UNSLOTH_STUDIO_PORT (default 8000)# jupyter JupyterLab for the notebooks port $JUPYTER_PORT (default 8888)# sshd key-only SSH for cloud hosts port 22#diff --git a/docker/unsloth_studio_update.sh b/docker/unsloth_studio_update.shindex 538f3eb2d61..547294f212c 100755--- a/docker/unsloth_studio_update.sh+++ b/docker/unsloth_studio_update.sh@@ -557,17 +557,18 @@ if [ "$RESTART" = "1" ]; thenlog "health check skipped (UNSLOTH_STUDIO_UPDATE_HEALTH_WAIT=0); the update is committed"else_deadline=$(( $(date +%s) + _wait ))+ _port="${UNSLOTH_STUDIO_PORT:-8000}"_up=0while [ "$(date +%s)" -lt "$_deadline" ]; do# --noproxy: a container-wide HTTP_PROXY must not answer for the loopback- curl -sf -o /dev/null --max-time 3 --noproxy '*' http://127.0.0.1:8000/api/health && { _up=1; break; }+ curl -sf -o /dev/null --max-time 3 --noproxy '*' "http://127.0.0.1:${_port}/api/health" && { _up=1; break; }sleep 2doneif [ "$_up" = "1" ]; thencommit_update- log "Studio is answering on port 8000"+ log "Studio is answering on port ${_port}"else- back_out "Studio did not answer on port 8000 within ${_wait}s (see docker logs)"+ back_out "Studio did not answer on port ${_port} within ${_wait}s (see docker logs)"fifielsediff --git a/tests/python/test_docker_studio_launch_port_guard.py b/tests/python/test_docker_studio_launch_port_guard.pyindex e3afb154c24..a3bebc93efb 100644--- a/tests/python/test_docker_studio_launch_port_guard.py+++ b/tests/python/test_docker_studio_launch_port_guard.py@@ -3,7 +3,7 @@"""JupyterLab must not be started on Studio's port.-Studio's port is fixed at 8000 (`studio_run.sh`), so with `JUPYTER_PORT=8000`+Studio's port is 8000 unless `UNSLOTH_STUDIO_PORT` says otherwise, so with `JUPYTER_PORT=8000`JupyterLab wins the bind and Studio falls back to an unpublished 8001. Both reportRUNNING and the summary still points at 8000, where Jupyter answers 404."""@@ -76,7 +76,7 @@ def test_the_check_only_exit_comes_after_the_guard():"""The guard is the point; check-only must not skip it, and nothing before thecheck-only exit may touch the host."""body = LAUNCH.read_text(encoding = "utf-8")- guard = body.index("jupyter_port_digits == 8000")+ guard = body.index("jupyter_port_digits == UNSLOTH_STUDIO_PORT")check = body.index("UNSLOTH_STUDIO_LAUNCH_CHECK_ONLY:-")assert guard < checkassert "> /etc/profile.d/unsloth_env.sh" not in body[:check]diff --git a/tests/python/test_docker_studio_port.py b/tests/python/test_docker_studio_port.pynew file mode 100644index 00000000000..574f5b91453--- /dev/null+++ b/tests/python/test_docker_studio_port.py@@ -0,0 +1,153 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-Present the Unsloth team. See /studio/LICENSE.AGPL-3.0++"""UNSLOTH_STUDIO_PORT moves Studio off 8000 inside the container.++Every place that named the port (the CLI launch, the JupyterLab port guard, the ready+summary and its health probe, the in-place updater's health wait) reads the variable,+and the image sets the default so a bare docker run keeps 8000.+"""++from __future__ import annotations++import os+import shutil+import subprocess+from pathlib import Path++import pytest++REPO_ROOT = Path(__file__).resolve().parents[2]+DOCKER = REPO_ROOT / "docker"+LAUNCH = DOCKER / "studio_launch.sh"+RUN = DOCKER / "studio_run.sh"+PASSWORD = DOCKER / "studio_password.sh"+UPDATE = DOCKER / "unsloth_studio_update.sh"++pytestmark = pytest.mark.skipif(shutil.which("bash") is None, reason = "needs bash")+++def _stub(bin_dir: Path, name: str, body: str) -> None:+ bin_dir.mkdir(parents = True, exist_ok = True)+ path = bin_dir / name+ path.write_text("#!/usr/bin/env bash\n" + body, encoding = "utf-8")+ path.chmod(0o755)+++def _clean_env(bin_dir: Path) -> dict:+ e = {k: v for k, v in os.environ.items() if not k.startswith("UNSLOTH_STUDIO")}+ e["PATH"] = f"{bin_dir}{os.pathsep}" + e["PATH"]+ return e+++def _launch(**env: str) -> subprocess.CompletedProcess:+ e = dict(_clean_env(Path("/nonexistent")), UNSLOTH_STUDIO_LAUNCH_CHECK_ONLY = "1", **env)+ return subprocess.run(["bash", str(LAUNCH)], capture_output = True, text = True, env = e, timeout = 120)+++def test_the_default_stays_8000():+ res = _launch(JUPYTER_PORT = "8000")+ assert res.returncode != 0+ assert "JUPYTER_PORT=8000" in res.stderr, res.stderr+ assert _launch(JUPYTER_PORT = "8888").returncode == 0+++def test_the_jupyter_guard_follows_the_studio_port():+ res = _launch(UNSLOTH_STUDIO_PORT = "9000", JUPYTER_PORT = "9000")+ assert res.returncode != 0+ assert "JUPYTER_PORT=9000" in res.stderr, res.stderr+ assert "-p 9000:8888" in res.stderr+ assert _launch(UNSLOTH_STUDIO_PORT = "9000", JUPYTER_PORT = "8000").returncode == 0++[email protected]("spelling", ["09000", " 9000", "9_000", "+9000"])+def test_jupyter_spellings_of_the_studio_port_are_still_caught(spelling: str):+ res = _launch(UNSLOTH_STUDIO_PORT = "9000", JUPYTER_PORT = spelling)+ assert res.returncode != 0, spelling+ assert "JUPYTER_PORT=9000" in res.stderr, res.stderr+++def test_leading_zeros_on_the_studio_port_normalize():+ res = _launch(UNSLOTH_STUDIO_PORT = "09000", JUPYTER_PORT = "9000")+ assert res.returncode != 0+ assert "JUPYTER_PORT=9000" in res.stderr, res.stderr+++ "value", ["abc", "0", "65536", "8000.0", "-1", "80 00", "18446744073709551617"]+)+def test_a_studio_port_that_is_not_a_port_is_refused(value: str):+ res = _launch(UNSLOTH_STUDIO_PORT = value, JUPYTER_PORT = "8888")+ assert res.returncode != 0, value+ assert f"UNSLOTH_STUDIO_PORT={value}" in res.stderr, res.stderr+++def test_many_leading_zeros_still_normalize():+ res = _launch(UNSLOTH_STUDIO_PORT = "000000000000000000009000", JUPYTER_PORT = "9000")+ assert "JUPYTER_PORT=9000" in res.stderr, res.stderr++[email protected]("key_var", ["SSH_KEY", "PUBLIC_KEY"])+def test_studio_on_sshd_port_is_refused_when_ssh_is_enabled(key_var: str):+ res = _launch(+ UNSLOTH_STUDIO_PORT = "22", JUPYTER_PORT = "8888", **{key_var: "ssh-ed25519 AAAA test"}+ )+ assert res.returncode != 0+ assert "UNSLOTH_STUDIO_PORT=22 is sshd's port" in res.stderr, res.stderr+ assert _launch(UNSLOTH_STUDIO_PORT = "22", JUPYTER_PORT = "8888").returncode == 0+++def test_studio_run_hands_the_port_to_the_cli(tmp_path: Path):+ _stub(tmp_path / "bin", "unsloth", 'printf "%s\\n" "$*"\n')+ e = _clean_env(tmp_path / "stub-bin")+ e["UNSLOTH_STUDIO_HOME"] = str(tmp_path)+ e["UNSLOTH_STUDIO_INITIAL_PASSWORD_FILE"] = str(tmp_path / "initial")+ res = subprocess.run(["bash", str(RUN)], capture_output = True, text = True, env = e, timeout = 60)+ assert res.stdout.strip() == "studio -H 0.0.0.0 -p 8000", res.stdout + res.stderr+ e["UNSLOTH_STUDIO_PORT"] = "9000"+ res = subprocess.run(["bash", str(RUN)], capture_output = True, text = True, env = e, timeout = 60)+ assert res.stdout.strip() == "studio -H 0.0.0.0 -p 9000", res.stdout + res.stderr+++def test_the_ready_summary_and_its_health_probe_use_the_port(tmp_path: Path):+ bin_dir = tmp_path / "stub-bin"+ log = tmp_path / "curl.log"+ _stub(bin_dir, "curl", f'echo "$*" >> "{log}"\nexit 0\n')+ _stub(bin_dir, "unsloth-studio-run", "exit 0\n")+ e = _clean_env(bin_dir)+ e.update(+ UNSLOTH_STUDIO_HOME = str(tmp_path),+ UNSLOTH_STUDIO_PASSWORD_STATE = "stored",+ UNSLOTH_STUDIO_PASSWORD_WAIT = "3",+ UNSLOTH_STUDIO_READY_WAIT = "2",+ UNSLOTH_STUDIO_PORT = "9000",+ JUPYTER_PORT = "8888",+ NO_COLOR = "1",+ )+ res = subprocess.run(["bash", str(PASSWORD)], capture_output = True, text = True, env = e, timeout = 60)+ assert res.returncode == 0, res.stderr+ assert "Studio http://localhost:9000" in res.stdout, res.stdout+ assert "http://127.0.0.1:9000/api/health" in log.read_text(encoding = "utf-8")+++def test_the_updater_waits_on_the_port():+ body = UPDATE.read_text(encoding = "utf-8")+ assert '_port="${UNSLOTH_STUDIO_PORT:-8000}"' in body+ assert '"http://127.0.0.1:${_port}/api/health"' in body+ assert "answering on port ${_port}" in body+++def test_the_image_default_and_the_host_wrapper_carry_the_variable():+ assert "UNSLOTH_STUDIO_PORT=8000" in (DOCKER / "Dockerfile.studio").read_text(encoding = "utf-8")+ assert "ENV_FORWARD+=(-e UNSLOTH_STUDIO_PORT)" in (DOCKER / "run.sh").read_text(+ encoding = "utf-8"+ )+ assert "`UNSLOTH_STUDIO_PORT`" in (DOCKER / "DOCKERHUB.md").read_text(encoding = "utf-8")++[email protected]("script", [LAUNCH, RUN, PASSWORD, UPDATE])+def test_no_script_names_the_port_literally_any_more(script: Path):+ lines = script.read_text(encoding = "utf-8").splitlines()+ body = "\n".join(l for l in lines if not l.lstrip().startswith("#"))+ for literal in ("127.0.0.1:8000", "localhost:8000", "-p 8000", "port 8000"):+ assert literal not in body, f"{script.name} still hardcodes {literal}"