Repository navigation
Description
Describe the bug
documentdb-local:latest enters a permanent restart loop on the second container boot whenever a persistent data volume is used. The entrypoint unconditionally re-runs the bundled sample-data load (/home/documentdb/gateway/sample-data/01-users.js …) on every start, which insertManys rows whose _ids already exist from the first boot. The bulk write fails with a duplicate-key error, init_documentdb_data.sh returns non-zero, and emulator_entrypoint.sh does exit 1 (line ~469), so any orchestrator with a restart policy loops forever.
This is a regression (or an incomplete close) of:
- DocumentDB-local Container restartfials due to duplicate initialziation of demo data #479 — closed 2026-04-15 without the fix landing
- Docker Container Fails to Restart Due to Duplicate Key Violation #399 — closed as duplicate of DocumentDB-local Container restartfials due to duplicate initialziation of demo data #479
- fix: Skip sample data init on restart if data already exists (#479) #498 — the actual fix PR, still open and unmerged as of today
Even with --skip-init-data available as a workaround, the default behavior should not crash. Re-running an idempotent seed against pre-seeded data is a normal and expected operation for any container with a persistent volume.
Reproduction Steps
# compose.yml
services:
documentdb:
image: ghcr.io/documentdb/documentdb/documentdb-local:latest
command: ["--username", "localadmin", "--password", "Admin100"]
ports: ["10260:10260"]
volumes:
- documentdb-data:/data
restart: unless-stopped
volumes:
documentdb-data:docker compose up -d # first boot: succeeds, seeds sampledb
docker compose restart documentdb # second boot: restart loop
docker compose logs documentdb | tail
Actual output (truncated)
Initializing users collection...
MongoBulkWriteError: Duplicate key violation on the requested collection: Index '_id_'
Error: Failed to execute: 01-users.js
This indicates invalid JavaScript syntax or operation error.
Error: Failed to process custom initialization data
Error: Sample data initialization failed
…then exit 1 from emulator_entrypoint.sh:469, container restarts, repeat indefinitely.
Expected behavior
The entrypoint should either:
- Detect that the sample collections (
sampledb.users/products/orders/analytics) already exist and skip the load, or - Make the seed scripts idempotent (e.g.
insertMany(..., { ordered: false })and tolerateDuplicateKey(E11000), or upsert by_id), or - Gate the entire sample-data block on a first-boot sentinel file inside
/data(the volume).
Whichever approach, a successful no-op on subsequent boots is the right default. Crashing the container is never correct here.
Workaround
Pass --skip-init-data (or SKIP_INIT_DATA=true) once the first-boot seed has completed. This is what we ended up doing in our compose stack, but it's a footgun: users only learn the flag exists after their stack is already broken.
Suggested fix
Merge or supersede #498. The simplest robust change is option (3): touch a marker file like /data/.documentdb-sample-data-loaded after a successful seed and short-circuit the block when the marker exists. That survives docker restart, docker compose down && up, host reboots, and volume backups.
Environment
- Image:
ghcr.io/documentdb/documentdb/documentdb-local:latest(pulled 2026-05-18) - Host: macOS (Darwin), Docker Desktop, arm64
- Compose: v2
- Repro stack: https://github.com/xgerman/documentdb-agentic-memory (
compose.full.yml)
Additional context
We hit this in a real downstream project (the documentdb-agentic-memory MCP stack). After confirming the regression locally we worked around it by adding --skip-init-data to our compose command:, but that only helps consumers who happen to read the entrypoint source. Defaults matter.
Activity
documentdb-triage-tool commentedon Jun 2, 2026
👋 @guanzhousongmicrosoft — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
documentdb-triage-tool commentedon Jun 16, 2026
👋 @guanzhousongmicrosoft — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
guanzhousongmicrosoft commentedon Jun 17, 2026
Thanks for the detailed report and clean repro — you're right that crashing on a re-seed against a persistent volume is never the correct default.
A fix is merged in our internal mirror and will reach this repo on the next OSS sync. It's defense-in-depth, so either layer alone breaks the loop:
_idisn't already present, so re-running against a populated volume is a no-op and a partial first run self-heals.<data-volume>/.documentdb-local, so seeding runs only on a fresh data directory — matching common database container images. To re-seed, start with a fresh volume.I'm keeping this open until the change lands on
mainhere and adocumentdb-localimage containing it is published. I'll follow up with the fixed image tag so you can verify — if you still hit the loop on that tag, please reopen.Thanks again for the thorough write-up and the upstream links.
Merged PR 2146214: Fix documentdb-local restart loop from duplicate s…
documentdb-triage-tool commentedon Jul 1, 2026
👋 @guanzhousongmicrosoft — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
documentdb-triage-tool commentedon Jul 16, 2026
👋 @guanzhousongmicrosoft — this issue has been assigned for 14 days with no activity. Is it still in progress? If you've made progress, please leave a quick update. Otherwise it'll be flagged for re-assignment at the next triage meeting.
guanzhousongmicrosoft commentedon Jul 25, 2026
Verified fixed on current
main(commit 14aab35, "Fix documentdb-local restart loop from duplicate sample-data init (#612)").Reproduction from the issue re-run against an image built from main (PG 17, standard packaging flow):
--init-data trueand a persistent volume → seedssampledb(5 users).docker restart→ container staysrunning(RestartCount=0, no crash loop), logs show:Sample data already initialized (found /data/.documentdb-local/sample_data_initialized); skipping. To re-run, start with a fresh data volume.sampledb.users.countDocuments()= 5, no duplicate-key errors.The one-shot init markers also cover custom
/init_doc_db.dscripts (attempt + success markers, so a failed non-idempotent seed is not blindly re-run), andtest_emulator_entrypoint.pypins the behavior (test_sample_data_is_seeded_once_and_skipped_on_restart,test_custom_data_is_one_shot_on_restart).Note: the currently published
documentdb-local:latest(built 2026-07-17) predates the fix and still loops — this closes when users pull an image built from current main; until the next image publish the workaround remains--skip-init-data/ a fresh volume.Closing as fixed on main and covered by regression tests.