Skip to content

Non-root container with a missing WorkingDir fails to start (os error 2); breaks DDEV #370

Closed

Description

@haraldpdl

Kiri version and system

0.2.1 on macOS 27.2.1 (26B131), Apple silicon. Docker CLI 29.8.2 (API 1.47).

What you did

Minimal reproduction, no DDEV needed, plain alpine:

# fails:
docker --context kiri run --rm --user 1000:1000 -w /does/not/exist alpine pwd

# both of these succeed, isolating the trigger to (non-root + missing workdir):
docker --context kiri run --rm                  -w /does/not/exist alpine pwd   # root + missing dir: OK
docker --context kiri run --rm --user 1000:1000 -w /tmp            alpine pwd   # non-root + existing dir: OK

It also surfaces through DDEV (ddev start), where the ddev-webserver container runs as a non-root user with a WorkingDir that is not present in the image: the db container comes up healthy, the web container exits 127.

What happened

kiri: cannot start container: working directory: No such file or directory (os error 2)

The container exits 127 immediately. Via DDEV the same error appears as service web has exited; state=exited after Failed to chown Mutagen volume.

What you expected instead

Docker creates a missing WorkingDir (like mkdir -p) and starts the container. The same commands on OrbStack and Docker Desktop succeed. On OrbStack the auto-created directory is root:root, mode 0755, and the non-root process enters it fine (chdir needs only +x on the path).

Anything else

Root cause looks like a privilege-ordering issue in crates/kiri-init/src/linux/container.rs, finish_child() (at commit 7846fe0):

1343  setgroups / setgid / setuid        # drop root -> container user
1346  chdir(p.cwd)                        # fails: dir missing
1347  // Docker creates the whole working directory path, like mkdir -p.
1348  fs::create_dir_all(p.cwd)           # fails: now unprivileged, cannot mkdir under /
1349  chdir(p.cwd)                        # still fails
1350  fail("working directory", ...)      # exit 127

The create_dir_all recovery runs after the setuid, so for a non-root container it has no permission to create the directory. The intent in the comment is right; the placement is after the privilege drop.

Suggested fix: create the directory while still root (before the setgroups/setgid/setuid block), then keep a single chdir after the drop. Matching Docker, leave it root:root 0755 (no chown):

// while still root, before setgroups/setgid/setuid:
// SAFETY: Docker creates a missing WorkingDir (mkdir -p), left root-owned 0755,
// so a non-root container can still chdir into it.
unsafe {
    let _ = fs::create_dir_all(
        std::ffi::OsStr::from_encoded_bytes_unchecked(p.cwd.as_bytes())
    );
}

// ... existing setgroups / setgid / setuid ...

if libc::chdir(p.cwd.as_ptr()) != 0 {
    fail("working directory", io::Error::last_os_error());
}

I have not built/tested this patch locally (guest kiri-init cross-build), so treat it as a suggestion; the reproduction above is on a real machine.

Activity

  1. libracoder commented on Nov 16, 2026

    @libracoder
    Owner

    Thanks for tracking down the cause. It was exactly the ordering you described. Fixed in #400: Kiri now creates a missing working directory as root, mode 0755, before it switches to the container's user, as Docker does. docker run --rm -u 1000:1000 -w /does/not/exist alpine pwd now prints the directory, and DDEV's web container should start. It's set for 0.2.2. Checked on macOS, Linux and WSL 2.

  2. libracoder commented on Nov 16, 2026

    @libracoder
    Owner

    Update: the fix is now in #403, which replaces #400. It also covers --read-only containers: docker run --read-only -u 1000:1000 -w /does/not/exist alpine pwd starts too.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions