Skip to content

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.

Keep the model a training script saves when it runs in Docker (#12994)

Nilay82bd5f31a1d9
diff --git a/docker/DOCKERHUB.md b/docker/DOCKERHUB.md
index 56ba7ccbd7d..d8a305f063f 100644
--- a/docker/DOCKERHUB.md
+++ b/docker/DOCKERHUB.md
@@ -82,7 +82,7 @@ The login token is printed in `docker logs`. With no command the image runs `pyt
### Scripts
```bash
-docker run --rm --gpus all --ipc=host -v "$PWD":/workspace/host \
+docker run --rm --gpus all --ipc=host -v "$PWD":/workspace/host -w /workspace/host \
unsloth/unsloth:core python /workspace/host/train.py
```
diff --git a/docker/run.sh b/docker/run.sh
index d999fd19f3e..a95c0f28df7 100755
--- a/docker/run.sh
+++ b/docker/run.sh
@@ -383,7 +383,7 @@ if ! [[ "$STOP_BUDGET" =~ ^[0-9]+$ ]]; then
printf "\033[1;31mERROR:\033[0m UNSLOTH_STUDIO_SHUTDOWN_STOP_TIMEOUT_S=%s is not a number of seconds.\n" "$STOP_BUDGET" >&2
exit 1
fi
-# 10# as in studio_launch.sh: a leading zero must not read as octal
+# matching studio_launch.sh, 10# prevents leading zeros from selecting octal
STOP_TIMEOUT=$(( 10#$STOP_BUDGET + 30 ))
declare -a PORT_FLAGS=()
@@ -392,15 +392,136 @@ if [[ -n "${UNSLOTH_PORTS:-}" ]]; then
PORT_FLAGS=(${UNSLOTH_PORTS})
fi
-# CI / piped invocations otherwise hit "the input device is not a TTY"
+# CI and piped invocations otherwise hit "the input device is not a TTY"
TTY_FLAG=()
if [ -t 0 ] && [ -t 1 ]; then
TTY_FLAG=(-it)
fi
-# URL runs use mounted $PWD so unsloth-run saves survive --rm; local paths still use /workspace
+# a mounted script starts in its directory so relative saves survive --rm
WORKDIR_FLAG=()
RUN_USER_ENV=()
+if [[ $# -gt 0 ]]; then
+ case "$1" in
+ /workspace/host | /workspace/host/*)
+ WORKDIR_FLAG=(-w /workspace/host)
+ ;;
+ *)
+ _args=("$@")
+ _runner="${1##*/}"
+ _scan_from=1
+ case "$_runner" in
+ accelerate)
+ _runner=""
+ if [[ "${_args[1]:-}" == launch ]]; then
+ _runner=launcher
+ _scan_from=2
+ fi
+ ;;
+ accelerate-launch | torchrun | deepspeed) _runner=launcher ;;
+ python | python[0-9]* | pypy | pypy[0-9]*)
+ _runner=""
+ _script=""
+ for (( _i=1; _i < ${#_args[@]}; _i++ )); do
+ _arg="${_args[$_i]}"
+ if [[ "$_arg" =~ ^-([bBdEhiIOPqRsSuvVx]*)([cmWX])(.*)$ ]]; then
+ _kind="${BASH_REMATCH[2]}"
+ _value="${BASH_REMATCH[3]}"
+ case "$_kind" in
+ c) break ;;
+ m)
+ if [[ -z "$_value" ]]; then
+ _value="${_args[$((_i + 1))]:-}"
+ _scan_from=$((_i + 2))
+ else
+ _scan_from=$((_i + 1))
+ fi
+ case "$_value" in
+ accelerate.commands.launch | deepspeed.launcher.runner | torch.distributed.launch | torch.distributed.run)
+ _runner=launcher
+ ;;
+ esac
+ break
+ ;;
+ W | X)
+ [[ -z "$_value" ]] && _i=$((_i + 1))
+ continue
+ ;;
+ esac
+ fi
+ case "$_arg" in
+ -) break ;;
+ --check-hash-based-pycs) _i=$((_i + 1)) ;;
+ --)
+ _script="${_args[$((_i + 1))]:-}"
+ break
+ ;;
+ -*) ;;
+ *) _script="${_args[$_i]}"; break ;;
+ esac
+ done
+ case "$_script" in
+ /workspace/host | /workspace/host/*) WORKDIR_FLAG=(-w /workspace/host) ;;
+ esac
+ ;;
+ bash | sh | zsh)
+ _runner=""
+ _script=""
+ for (( _i=1; _i < ${#_args[@]}; _i++ )); do
+ case "${_args[$_i]}" in
+ -c | -s) break ;;
+ -O | +O | -o | +o | --init-file | --rcfile) _i=$((_i + 1)) ;;
+ --)
+ _script="${_args[$((_i + 1))]:-}"
+ break
+ ;;
+ -* | +*) ;;
+ *) _script="${_args[$_i]}"; break ;;
+ esac
+ done
+ case "$_script" in
+ /workspace/host | /workspace/host/*) WORKDIR_FLAG=(-w /workspace/host) ;;
+ esac
+ ;;
+ *) _runner="" ;;
+ esac
+ if [[ "$_runner" == launcher ]]; then
+ _prev=""
+ for (( _i=_scan_from; _i < ${#_args[@]}; _i++ )); do
+ _arg="${_args[$_i]}"
+ _arg_case="$_arg"
+ if [[ "$_arg_case" == --* ]]; then
+ _arg_case="${_arg_case#--}"
+ _arg_case="--${_arg_case//-/_}"
+ fi
+ if [[ "$_prev" == -* ]]; then
+ _prev=""
+ continue
+ fi
+ case "$_arg_case" in
+ -m | -q | --bind_cores_to_rank | --cpu | --debug | --downcast_bf16 | --dynamo_use_dynamic | --dynamo_use_fullgraph | --dynamo_use_regional_compilation | --enable_cpu_affinity | --elastic_training | --force_multi | --fp8_use_autocast_during_eval | --module | --multi_gpu | --no_local_rank | --no_python | --no_ssh | --no_ssh_check | --no_tpu_cluster | --quiet | --run_path | --same_network | --save_pid | --standalone | --tpu | --tpu_cluster | --tpu_use_sudo | --use_cpu | --use_deepspeed | --use_env | --use_fsdp | --use_megatron_lm | --use_mps_device | --use_parallelism_config | --use_tp | --use_xpu | --virtual_local_rank)
+ _prev=""
+ continue
+ ;;
+ -- | -H?* | -e?* | -i?* | -r?* | -t?* | -*=*)
+ _prev=""
+ continue
+ ;;
+ -*)
+ _prev="$_arg"
+ continue
+ ;;
+ /workspace/host | /workspace/host/*)
+ WORKDIR_FLAG=(-w /workspace/host)
+ break
+ ;;
+ *) break ;;
+ esac
+ done
+ fi
+ ;;
+ esac
+fi
if [[ $# -gt 0 && "$1" == "unsloth-run" ]]; then
for _arg in "${@:2}"; do
case "$_arg" in
@@ -413,7 +534,7 @@ if [[ $# -gt 0 && "$1" == "unsloth-run" ]]; then
done
fi
-# no set -x: it leaks HF_TOKEN/WANDB_API_KEY; this array form is nounset-safe on macOS Bash 3.2
+# set -x leaks HF_TOKEN/WANDB_API_KEY; this array form is nounset-safe on macOS Bash 3.2
exec docker run --rm ${TTY_FLAG[@]+"${TTY_FLAG[@]}"} \
${GPU_FLAG[@]+"${GPU_FLAG[@]}"} \
--ipc=host \
diff --git a/tests/python/test_docker_hub_readme.py b/tests/python/test_docker_hub_readme.py
index eadee589eb9..8438eab08fa 100644
--- a/tests/python/test_docker_hub_readme.py
+++ b/tests/python/test_docker_hub_readme.py
@@ -42,8 +42,7 @@ def test_the_hub_readme_describes_the_shipped_images():
"UNSLOTH_STUDIO_PASSWORD",
):
assert needle in text, f"the Hub README no longer mentions {needle!r}"
- # the previous image's conventions, none of which exist in this one
- # Studio writes the generated password to a file and does not print it itself
+ # old-image conventions are invalid; Studio writes generated passwords to a file
for stale in (
"USER_PASSWORD",
"/workspace/work",
@@ -54,11 +53,17 @@ def test_the_hub_readme_describes_the_shipped_images():
assert stale not in text, f"the Hub README still carries {stale!r} from the old image"
+def test_the_hub_readme_runs_a_script_in_the_mounted_dir():
+ text = HUB_README.read_text(encoding = "utf-8")
+ section = text[text.index("### Scripts") :]
+ start = section.index("```")
+ command = section[start : section.index("```", start + 3)]
+ assert "python /workspace/host/train.py" in command
+ assert "-w /workspace/host" in command
+
+
def test_the_hub_readme_explains_the_studio_volume():
- """The volume keeps Studio's data and never pins its code; a volume from an image
- before the code/data split is migrated with its old code kept aside. Both facts,
- the way back to an older image, and what `docker rm` still discards have to be on
- the page, since the quick start above them mounts the volume by default."""
+ """the Studio volume keeps data, not code, with migration, rollback, and `docker rm` limits."""
text = HUB_README.read_text(encoding = "utf-8")
for needle in (
"-v unsloth-studio:/opt/unsloth-studio",
diff --git a/tests/python/test_docker_run_url_keeps_outputs.py b/tests/python/test_docker_run_url_keeps_outputs.py
index 59786ba6bb2..2b575ec02f7 100644
--- a/tests/python/test_docker_run_url_keeps_outputs.py
+++ b/tests/python/test_docker_run_url_keeps_outputs.py
@@ -340,9 +340,153 @@ def test_run_sh_starts_unsloth_run_in_the_mounted_host_dir(tmp_path):
)
@pytest.mark.parametrize(
"command",
- [("jupyter", "lab"), ("unsloth-run", "unsloth-notebooks/nb/Llama.ipynb")],
+ [
+ ("python", "/workspace/host/train.py"),
+ ("/usr/bin/python", "/workspace/host/train.py"),
+ ("python", "-u", "/workspace/host/train.py", "--data", "data/train.jsonl"),
+ ("python", "-uW", "ignore", "/workspace/host/train.py"),
+ ("python", "-uX", "dev", "/workspace/host/train.py"),
+ ("python", "-u", "/workspace/host/train"),
+ ("python", "-u", "/workspace/host/job"),
+ (
+ "python",
+ "-m",
+ "accelerate.commands.launch",
+ "--multi_gpu",
+ "/workspace/host/train.py",
+ ),
+ (
+ "python",
+ "-um",
+ "accelerate.commands.launch",
+ "--multi_gpu",
+ "/workspace/host/train.py",
+ ),
+ (
+ "python",
+ "-umaccelerate.commands.launch",
+ "--multi_gpu",
+ "/workspace/host/train.py",
+ ),
+ (
+ "python",
+ "-m",
+ "torch.distributed.launch",
+ "--nproc_per_node=2",
+ "/workspace/host/train.py",
+ ),
+ (
+ "python",
+ "-m",
+ "torch.distributed.launch",
+ "--use-env",
+ "/workspace/host/train.py",
+ ),
+ ("bash", "/workspace/host/train"),
+ ("bash", "--noprofile", "/workspace/host/train"),
+ ("bash", "+O", "extglob", "/workspace/host/train.sh"),
+ ("/workspace/host/train.sh",),
+ ("accelerate-launch", "/workspace/host/train.py"),
+ ("accelerate", "launch", "--multi_gpu", "/workspace/host/train.py"),
+ ("accelerate", "launch", "--multi-gpu", "/workspace/host/train.py"),
+ ("accelerate", "launch", "--multi_gpu", "/workspace/host/train"),
+ ("accelerate", "launch", "--no_python", "/workspace/host/train"),
+ ("deepspeed", "-H", "/workspace/host/hosts", "/workspace/host/train.py"),
+ (
+ "accelerate",
+ "launch",
+ "--config_file=/workspace/host/config.yaml",
+ "/workspace/host/train.py",
+ ),
+ (
+ "accelerate",
+ "launch",
+ "--config_file",
+ "configs/default.yaml",
+ "/workspace/host/train.py",
+ ),
+ (
+ "accelerate",
+ "launch",
+ "--multi_gpu",
+ "/workspace/host/train.py",
+ "--data",
+ "data/train.jsonl",
+ ),
+ ],
+)
+def test_run_sh_starts_a_host_script_in_the_mounted_host_dir(tmp_path, command):
+ if "/workspace/host/train" in command:
+ (tmp_path / "train").write_text("print('train')\n", encoding = "utf-8")
+ if "/workspace/host/job" in command:
+ (tmp_path / "job").mkdir()
+ (tmp_path / "job" / "__main__.py").write_text("print('job')\n", encoding = "utf-8")
+ argv = _run_sh_argv(tmp_path, *command)
+ image = argv.index("unsloth/unsloth:latest")
+ assert ["-w", "/workspace/host"] in [argv[i : i + 2] for i in range(image)]
+ assert argv[image + 1 :] == list(command)
+
+
+ os.name != "posix" or shutil.which("bash") is None, reason = "POSIX shell required"
+)
+ "command",
+ [
+ ("jupyter", "lab"),
+ ("cp", "/workspace/host/input.txt", "relative-output.txt"),
+ ("deepspeed", "-H", "/workspace/host/hosts", "/workspace/smoke_test.py"),
+ ("python", "-c", "print('ok')", "/workspace/host/input.py"),
+ ("python", "-uc", "print('ok')", "/workspace/host/input.py"),
+ ("python", "-m", "tool", "/workspace/host/input.py"),
+ ("python", "-m", "pytest", "--basetemp", "/workspace/host"),
+ ("python", "train.py", "--config", "/workspace/host/config.json"),
+ (
+ "accelerate",
+ "launch",
+ "--no_python",
+ "./train",
+ "--data",
+ "/workspace/host/data.json",
+ ),
+ (
+ "torchrun",
+ "--standalone",
+ "./train",
+ "--data",
+ "/workspace/host/data.json",
+ ),
+ ("accelerate", "launch", "-m", "package.train", "--data", "/workspace/host/data.json"),
+ ("accelerate", "launch", "train", "--data", "/workspace/host/data.json"),
+ ("accelerate", "config", "update", "--config_file", "/workspace/host/config.json"),
+ ("bash", "-c", "printf ok", "/workspace/host/input.sh"),
+ ("unsloth-run", "unsloth-notebooks/nb/Llama.ipynb"),
+ ("unsloth-run", "unsloth-notebooks/nb/Llama.ipynb", "--out", "/workspace/host/Llama.ipynb"),
+ ("python", "/workspace/smoke_test.py"),
+ ("python", "/workspace/smoke_test.py", "--out", "/workspace/host/result"),
+ (
+ "jupyter",
+ "nbconvert",
+ "--execute",
+ "unsloth-notebooks/nb/Llama.ipynb",
+ "--output-dir",
+ "/workspace/host",
+ ),
+ (
+ "jupyter",
+ "nbconvert",
+ "--output-dir",
+ "/workspace/host",
+ "--execute",
+ "unsloth-notebooks/nb/Llama.ipynb",
+ ),
+ ],
)
def test_run_sh_leaves_other_commands_in_the_image_workdir(tmp_path, command):
+ if "/workspace/host/config.json" in command:
+ (tmp_path / "config.json").write_text("{}\n", encoding = "utf-8")
+ if "/workspace/host/data.json" in command:
+ (tmp_path / "data.json").write_text("{}\n", encoding = "utf-8")
argv = _run_sh_argv(tmp_path, *command)
flags = argv[: argv.index("unsloth/unsloth:latest")]
assert "-w" not in flags