Skip to content

documentdb-local restart loop on second boot: sample-data seed re-runs and fails with DuplicateKey (regression of #479) #612

Closed
richardsimmonds/documentdb
#1

Description

@xgerman

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:

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:

  1. Detect that the sample collections (sampledb.users / products / orders / analytics) already exist and skip the load, or
  2. Make the seed scripts idempotent (e.g. insertMany(..., { ordered: false }) and tolerate DuplicateKey (E11000), or upsert by _id), or
  3. 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

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

  1. added
    documentdb-local
    on May 18, 2026
  2. documentdb-triage-tool commented on Jun 2, 2026

    @documentdb-triage-tool

    👋 @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.

  3. documentdb-triage-tool commented on Jun 16, 2026

    @documentdb-triage-tool

    👋 @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.

  4. guanzhousongmicrosoft commented on Jun 17, 2026

    @guanzhousongmicrosoft
    Member

    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:

    • Idempotent seeds. The bundled sample scripts now insert each document only if its _id isn't already present, so re-running against a populated volume is a no-op and a partial first run self-heals.
    • First-boot sentinel (your option 3). Sample and custom initialization are guarded by one-shot markers under <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.
    • Hardened custom init. User-provided init scripts record an attempt marker immediately before the first script runs, so a non-idempotent custom script that fails partway is not retried on restart; if the marker can't be written, init aborts before touching data so a retry stays safe.

    I'm keeping this open until the change lands on main here and a documentdb-local image 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.

  5. documentdb-triage-tool commented on Jul 1, 2026

    @documentdb-triage-tool

    👋 @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.

  6. documentdb-triage-tool commented on Jul 16, 2026

    @documentdb-triage-tool

    👋 @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.

  7. added this to the 0.115-0 milestone on Jul 24, 2026
  8. guanzhousongmicrosoft commented on Jul 25, 2026

    @guanzhousongmicrosoft
    Member

    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):

    1. First boot with --init-data true and a persistent volume → seeds sampledb (5 users).
    2. docker restart → container stays running (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.
    3. Data intact after restart: sampledb.users.countDocuments() = 5, no duplicate-key errors.

    The one-shot init markers also cover custom /init_doc_db.d scripts (attempt + success markers, so a failed non-idempotent seed is not blindly re-run), and test_emulator_entrypoint.py pins 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

documentdb-localmerged

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions