Skip to content

Docker Studio: keep Studio's data on a volume without pinning its code - #10829

Merged
danielhanchen merged 19 commits into
unslothai:mainfrom
oobabooga:docker-studio-data-volume
Sep 14, 2026
Merged

danielhanchen merged 19 commits into
unslothai:mainfrom
oobabooga:docker-studio-data-volume

Conversation

@oobabooga

@oobabooga oobabooga commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Today a Studio container gives you a choice between losing your data and never getting a new Studio:

  • No volume (what the Docker Hub quick start shows): docker rm plus docker run again, the normal way to move to a new image, gives you a new generated password, no chats, and no trained or exported models. The old password gets a 401.
  • With a volume (the only advice in the repo, a comment in unsloth_studio_update.sh): the data survives, but the volume also holds that image's code, so every later image mounts it and keeps running the old Studio, silently.

This separates the two, so a volume keeps the data and the image always brings the code.

Why it happens

/opt/unsloth-studio is one root for everything: the venv, source tree, Node, whisper.cpp and the transformers tiers sit next to auth/, studio.db, outputs/, exports/, runs/ and rag/. A volume mounted there necessarily captures both.

Measured on the published images: starting from a volume created by nightly-2026.09.09 and then running latest, studio/backend/core/inference/inference.py stayed at the old build's md5 (ee15933b) although the image ships 5ff793a5. A third shape of the same problem: an empty bind mount on that path hides the venv and Studio never starts (FATAL).

The change

The image keeps Studio's code in /opt/unsloth-studio-app and leaves /opt/unsloth-studio for Studio's data, with one symlink per code entry so Studio still sees a single directory:

/opt/unsloth-studio/unsloth_studio -> /opt/unsloth-studio-app/unsloth_studio
/opt/unsloth-studio/src            -> /opt/unsloth-studio-app/src
... node, share, bin, cache, whisper.cpp, llama.cpp, .venv_t5_*, the install locks
  • UNSLOTH_STUDIO_HOME is unchanged, so studio_root() still resolves to /opt/unsloth-studio and the absolute paths baked into the venv at build time keep resolving through the links.
  • The move happens inside the same RUN that installs Studio; a later layer would store the 10+ GB install twice.
  • docker/studio_home.sh creates, repairs and prunes the links. The entrypoint runs it at every start, before it touches the Studio venv. A volume from an earlier image holds that image's code as real directories, and those get replaced with links to this image's copy, which is what unfreezes the upgrade. Everything Studio wrote at runtime is left alone. The one thing it removes is a link into the app dir whose entry this image dropped, e.g. a transformers tier, which would otherwise dangle.
  • The Docker Hub page and the README now mount it: -v unsloth-studio:/opt/unsloth-studio, with a row saying what is in it.

Before and after

A is the published unsloth/unsloth:latest. B is this branch built with the same pinned refs; for the upgrade row the "newer image" is this branch built from current main, so the code identity visibly changes.

Check A (published) B (this PR)
No volume, docker rm + run again password 401, trained output gone unchanged: password 401, output gone
Volume, docker rm + run again password 200, output kept password 200, output kept
Volume size after that run 10.0 GB 856 KB
Upgrade from a nightly-2026.09.09 volume code stays ee15933b; the image ships 5ff793a5 code becomes the image's 0ad553ae
Same upgrade: password, trained output, health kept, 200 kept, 200
Empty bind mount on /opt/unsloth-studio Studio FATAL, health 000 Studio RUNNING, health 200

The image is the same size (27.070 vs 27.072 GB), and the upgrade names each directory it replaces in the logs.

On B, with the volume mounted: the Playwright pass over Studio and JupyterLab is 9/9 (sign in, pick gemma-3-270m-it-GGUF, two chat turns, notebook cell reporting the GPU), the Studio Train page finishes a 10-step Qwen3-0.6B run (3/3), and after docker rm plus a fresh container on the same volume the trained adapter is still in outputs/ and the password still works.

Verification

  • tests/python/test_docker_studio_home_volume.py, 10 cases: an empty home, a volume holding an earlier image's code, runtime data left untouched, links already in place, a stale link retargeted, a link to a dropped entry pruned while the user's own links stay, a base image with no app dir, the move happening in the install layer, the entrypoint hook running before the venv is used, and the build context allowlist.
  • Every tests/python/test_docker_*.py: 495 passed, 5 skipped, 1 failed. The failure, test_docker_nb_refresh_state_durability.py::test_an_abandoned_restore_puts_the_tree_back, fails identically on main.
  • The new script is chmod'd with the other /usr/local/bin scripts rather than in a RUN of its own, which is what test_docker_studio_password_banner.py::test_the_image_wires_the_scripts_in reads.
  • The build check at the end of the install RUN asserts the venv still imports studio through $UNSLOTH_STUDIO_HOME/src.

Not in this PR

Nothing copies data out of an unmounted container: a user who never mounted a volume still has to docker cp before recreating. A user who already has a volume keeps their data, and the first start on the new image replaces the code in it.

Caches under the Studio home (cache/) count as code here, so they live in the image and are rebuilt rather than carried in the volume.

What Studio writes into a linked directory at runtime, such as a cloudflared binary in bin/ or a transformers tier the image does not ship, still lands in the container layer and is lost on docker rm. Both are re-fetched on demand.

unsloth-studio-update --ref on main replaces $UNSLOTH_STUDIO_HOME/src with a real tree, which this relinking undoes at the next start: the image wins on code, as it does for everything else here. That would cost an in-container --ref update its survival across docker restart, so the updater is changed in the same set to write through the link into the image's copy, where the update survives a restart exactly as it does today. Merged in either order the result is the same; merged alone, this PR makes --ref image-only until that one lands.

The non-root question is untouched: the Studio image still runs its services as root.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed /opt/unsloth-studio in docker/Dockerfile.studio is still one root for the venv and for auth, studio.db and outputs, so a volume there does pin the code. One thing before I review: with this alone, an in-container unsloth-studio-update --ref gets relinked away at the next start, so could you confirm how that lands if the updater change is not merged first?

…them

The linker moves whatever is in the way of a code entry (an earlier image's real
directory, a user's file or link under the same name) to
.unsloth-studio-legacy/<name> on the volume, a rename on the same filesystem, and
prints how to restore or free it. unsloth-studio-home --restore puts the entries
back so the volume can go back to an image from before the code/data split.
UNSLOTH_STUDIO_KEEP_LEGACY=0 deletes instead.

The two roots are compared as the kernel sees them and the same or nested roots
are refused before anything moves. The move completes before the link is made, so
a failure between the two leaves the kept copy in place and names it. The
entrypoint now exits when the linker fails in the Studio image instead of
starting Studio against a half-linked home.
… and fix the symlinked-sidecar paths

UV_CACHE_DIR now points into UNSLOTH_STUDIO_APP so the install layer's 9 GB uv cache
moves with the venv that hardlinks into it, and cache/ stays a real directory in the
Studio home: download-resume manifests, llama slots and dataset caches survive docker rm
and a volume from an earlier image keeps its cache/ instead of losing it to the linker.
The build asserts the cache did not land in the home.

_ensure_venv_dir repairs the directory a sidecar link points at (rmtree refused the
link and ignore_errors hid it, so damaged files survived the wipe). Home inference from
sys.prefix declines the app dir so a process without UNSLOTH_STUDIO_HOME cannot write
studio.db into the container layer. The updater's failure text and its test say what is
true now: docker rm + docker run restores the image's venv.
…a split

run.sh now mounts a named volume (UNSLOTH_STUDIO_VOLUME, default unsloth-studio,
empty disables) at /opt/unsloth-studio on every path, so the helper keeps Studio's
accounts, chats and outputs the way the quick start in DOCKERHUB.md does.

DOCKERHUB.md, README.md and the Dockerfile.studio header now say the same thing:
the volume holds data only, each image links its own code in at start, a volume
from an older image is migrated with the old code kept under .unsloth-studio-legacy/
(UNSLOTH_STUDIO_KEEP_LEGACY=0 deletes it), how to go back to an older image, what
docker rm still discards, and that the home needs a named volume rather than a bind
mount of a Windows or macOS host directory.

Tests: run.sh default, opt-out, AMD and CPU paths, a path with a space; Hub README
needles for the volume section.
@danielhanchen

Copy link
Copy Markdown
Member

Verified this on a B200 host against the published unsloth/unsloth:latest (pre-split layout) and images built from this branch, then pushed five commits on top of the branch. Summary of what was found and what changed.

Before the PR: /opt/unsloth-studio held both the install (venv, src, node, bin, share, cache, whisper.cpp, the .venv_t5_* tiers) and the data (auth/, studio.db, outputs/, exports/, runs/, rag/). Without a volume, docker rm lost every account and trained model. With a volume, Docker copied the first image's real directories in and every later image ran that code (confirmed: main.py md5 stayed at the old image's value across an image change).

With the PR as submitted: fresh volume, docker rm, nocopy, empty bind mount, CPU start all worked, volume is 798 kB. Three problems reproduced on real volumes:

  1. A volume from an older image had every colliding entry rm -rf'd, including cache/ (Studio's download resume manifests, llama slots, dataset caches) and bin/, and the same happened to any user content that shared a name with a code entry (bin/mytool, a file called share).
  2. No way back: the migrated volume under the previous image failed with /opt/unsloth-studio/bin/unsloth: No such file or directory, supervisord FATAL.
  3. set -e between rm -rf and ln -s left an entry missing, and the entrypoint only printed a WARN and started Studio against a half-linked home. UNSLOTH_STUDIO_HOME=/opt/unsloth-studio-app (or the reverse) deleted the whole install with exit 0.

Commits pushed

  • 51dd0f2d7 studio_home.sh: entries in the way are moved (a same-filesystem rename) to .unsloth-studio-legacy/<name> on the volume instead of deleted, UNSLOTH_STUDIO_KEEP_LEGACY=0 deletes, the two roots are canonicalised and refused when equal or nested, ln -sT runs only after the move completed so a failure names the intact copy, and unsloth-studio-home --restore moves everything back for a rollback. The entrypoint now exits 1 when the linker fails in the Studio image. 23 tests.
  • f305324c8 Dockerfile.studio: UV_CACHE_DIR=/opt/unsloth-studio-app/uv-cache is set before the install RUN (install.sh, setup.sh and _setup_cache_env only default it when unset, and the build asserts the cache landed there), and cache/ is excluded from the move so it stays runtime data in the home. unsloth_studio_update.sh failure text and its test say the truth under this layout. _ensure_venv_dir repairs a symlinked sidecar through realpath (rmtree on a symlink was a silent no-op), and _infer_studio_home_from_venv refuses to adopt the app dir as the home.
  • 014a7a16a run.sh mounts unsloth-studio:/opt/unsloth-studio on every path (UNSLOTH_STUDIO_VOLUME, empty disables). README, DOCKERHUB and the Dockerfile header agree on what the volume keeps, how migration and rollback work, what docker rm still discards, and that a bind mount of a Windows or macOS host directory is not the supported path because the home needs symlinks.
  • 59d96c986 the documented alpine rollback one-liner also moves symlinked legacy entries (the llama.cpp link dangles inside alpine).
  • The merge with main resolves the Dockerfile.studio ENV conflict by keeping UNSLOTH_STUDIO_APP alongside main's UNSLOTH_IMAGE_ALLOW_CPU=1.

Verification on the pushed tree (image built from this branch on the published :core, Studio at main):

Case Result
Fresh volume, docker rm, run again account, marker kept, 798 kB, code identity is the new image
Volume created by published :latest, then this image 9 entries kept aside (4.7 GB), account, marker, cache/USER_CACHE_FILE and bin/USER_TOOL all survive, new code active
--restore, then the published :latest on that volume boots, old code, marker and user file intact; forward again to the new image works
Documented alpine one-liner instead of --restore same outcome
User bin/, file share, symlink src in the home all kept aside, outputs/ untouched
UNSLOTH_STUDIO_HOME set to the app dir refused, exit 1, install intact
Read-only home, filesystem that cannot create symlinks exit 1 before Studio starts, on a legacy layout the one moved entry is named intact and --restore puts it back
CPU start, UNSLOTH_SKIP_GPU_CHECK=1 linker runs, Studio up
GPU: TRL patch probe and an SFT run in the Studio venv UnslothSFTTrainer, trains
docker diff after a hub download nothing written under the app dir; manifests land in the home's cache/
Image size and layers unchanged (one 15.3 GB install layer)
Linker simulation harness (fresh, legacy, ln/mv/mkdir failing at every position, read-only, case collisions, unicode, 200 seeded random interleavings) 232/232 on the new script; the original script failed 178 of them, all by deleting something

tests/python/test_docker_*.py: 533 passed, 2 skipped.

Two things worth knowing that are not changed here: unsloth-studio-update --ref <git sha> installs a wheel without the built frontend and Studio then fails to start with Unsloth frontend build not found; that reproduces on main's image too, so it is independent of this PR. And Studio's in-app whisper.cpp updater treats a symlinked install as a local link and skips, which is the same gating llama.cpp already had in the image.

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-14T04:25:49.034130Z 5fa1da9 Manual request
🔒 Security Review ✅ Completed 2026-09-14T02:37:29.342674Z bb67d48 Manual request

Security findings

Advisory findings (1)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

…has no kept-aside copy, so an older image can run it
…Hub page says how to roll back a post-split volume
chatgpt-codex-connector[bot]

This comment was marked as resolved.

…of writing through it

mkdir -p, rm -rf and mv all follow a symlink, so anything but a real directory at
$UNSLOTH_STUDIO_HOME/.unsloth-studio-legacy pointed the migration at whatever the link
resolved to. A link to the app dir made a clean image's first start clear and overwrite
this image's own code. Both the link run and --restore now stop with the path named.
…ot mistaken for code

The post-split branch removed the link and copied over it, so a copy that died half way
left a partial real directory where the code goes. A rerun iterates symlinks only, so it
skipped that entry and reported success over an incomplete tree. The copy now lands in a
sibling temp dir and is renamed into place once it is whole; on failure the temp dir goes
and the link stays, so the rerun works.
…a user checkout

The updater treats a linked llama.cpp / whisper.cpp directory as a checkout it must never
write through, so no update is offered or applied. whisper.cpp is discovered through the
Studio home and has no path variable to shortcut that, so once the image links its code
in from $UNSLOTH_STUDIO_APP the whisper update went unsupported in the published image.
A link whose target resolves inside that tree is the image's own install, so it no longer
counts as external; with the variable unset, which is every install outside the image,
nothing changes, and a --with-llama-cpp-dir checkout never resolves inside it.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c0b2855b7d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +283 to +284
root = os.path.realpath(app)
return os.path.commonpath([root, os.path.realpath(path)]) == root

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Recognize the image-owned llama.cpp link

In the Studio image, the home’s llama.cpp link points to $UNSLOTH_STUDIO_APP/llama.cpp, but that app entry is itself the symlink to /opt/unsloth/llama.cpp created at docker/Dockerfile.studio:110. Fully resolving path therefore lands outside the app root, so active_install_is_local_link() still classifies the bundled llama.cpp as user-managed and the in-app updater refuses to offer or apply updates. Fresh evidence beyond the earlier fix is this two-hop link retained by the Dockerfile; the regression test only uses a real directory under the app tree and does not exercise it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two-hop link is real but the conclusion is not: in the image, discovery for llama.cpp never walks through the home link. The base image sets UNSLOTH_LLAMA_CPP_PATH=/opt/unsloth/llama.cpp, which _find_llama_server_binary honours before the managed home dir, so the active binary is /opt/unsloth/llama.cpp/llama-server and no parent up to the component dir is a symlink. Measured in the built image from the Studio venv: _active_install_is_local_link returns False and get_update_status reports supported True with b10830 installed against b10909-mix latest. whisper.cpp has no such variable, which is why the earlier fix was needed there, and its tree under the app dir is a real directory, not a second link.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 738d7aacd5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread docker/studio_home.sh
Comment on lines +154 to +160
for entry in "$APP"/*; do
name="${entry##*/}"
[ "$name" = "$LEGACY_NAME" ] && continue
# unsloth-studio-update's staging and previous-tree directories, left in the app dir
# by a killed update: scratch, not code; never linked into the home
case "$name" in .src-update.*|.src-prev.*) continue;; esac
target="$HOME_DIR/$name"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Relocate runtime-created sidecars out of the volume

When a user has provisioned the optional transformers “latest” tier (or the lazy llm-compressor tier), it exists only as a real $UNSLOTH_STUDIO_HOME/.venv_t5_latest directory because the image build does not create a corresponding app entry. This loop migrates only names already present in $APP, so that executable code remains on the mounted volume; transformers_version.py detects its pin on the next image and automatically routes models through it. Consequently an image upgrade or rollback can keep running packages from the previous container rather than the versions shipped by the selected image, undermining the code/data split and reproducibility. Known runtime-created code directories need to be moved or otherwise reset even when the new app tree lacks them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not taking this. A tier provisioned at runtime is something the user installed into their home, which is what the volume is for, and it is not carried blindly: _venv_dir_is_valid_and_undamaged checks the sidecar against the pins the running image expects at every start and _ensure_venv_dir rebuilds it when they do not match, so a new image with different pins does not run the old packages. Before this PR every one of these directories lived on the volume too, so there is no regression, and a list of runtime-created code names to reset on each start would go stale the first time Studio adds one.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 14, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 563cf09fb0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 5fa1da93a9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 6d7fd47 into unslothai:main Sep 14, 2026
27 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants