DEV Community

Cover image for Why Docker Silently Turns Your Missing Config File Into an Empty Directory
hoagminh
hoagminh

Posted on Edited on

Why Docker Silently Turns Your Missing Config File Into an Empty Directory

Almost everyone who runs Docker Compose has been burned by this exact sequence at least once:

services:
  clickhouse:
    image: clickhouse/clickhouse-server:24
    volumes:
      - ./clickhouse-init.sh:/docker-entrypoint-initdb.d/init.sh
Enter fullscreen mode Exit fullscreen mode

You forget to fetch or copy clickhouse-init.sh onto the VPS before running docker compose up -d.

Docker does not return a "file not found" error. Instead, it silently creates an empty directory on the host named clickhouse-init.sh and bind-mounts that directory into the container. The container immediately crash-loops with a cryptic error:

/docker-entrypoint-initdb.d/init.sh: Is a directory
Enter fullscreen mode Exit fullscreen mode

Even worse, once Docker creates that empty directory on disk, your next deploy trying to write the real clickhouse-init.sh file to that path fails too—until you explicitly remove the empty directory first.

We hit this exact failure on an upstream Appwrite 2.4.0 update (./clickhouse-init.sh was bind-mounted in upstream Compose, but missed by the template's file fetch list). Fixing it cleanly in Nitrobase turned into a small, surprisingly subtle bind-mount validation pipeline.

Nitrobase has only recently launched and is still in Beta. Its bind-source checker is not a full container runtime security sandbox; its goal is narrower: prevent Docker from silently turning missing config files into empty directories, block symlink path escapes inside app volumes, and self-heal stale empty directories without ever risking real data.

1. Classify every bind mount inside the app folder

Not every unwritten path in a volumes: list is a bug. A Compose file mixes config files (./nginx.conf, ./clickhouse-init.sh) with runtime data folders (./pb_data, ./volumes/db/data) that are supposed to start empty.

Nitrobase resolves every bind source inside the app directory (templates/bind-sources.ts) and classifies it into four buckets:

Bind-source bucket How Nitrobase identifies it Behavior before & after compose up
Rendered files (required) Files written directly by the deploy (files) Hard check: must exist as a regular file ([ -f ]) before and after up (exits 97: BIND_FILE_MISSING_EXIT)
Declared runtime data Template data_binds and [backup] paths / reset Left alone as app-managed directories (created with mkdir -p if missing)
Missing upstream files Unwritten bind sources that exist as files in the upstream template repo Refused at render time (TEMPLATE_BIND_SOURCE_MISSING) before touching SSH
Other file-named paths (named) Unwritten, undeclared paths whose name looks like a file (looksLikeFile) Soft check: never mkdir'd; prints a warning before up, then fails after up if Docker turned it into an empty directory

Notice why the upstream file check (assertBindSourcesFetched) only triggers when needsUpstreamListing returns true: if every unwritten bind source is an extensionless folder like volumes/db/data, Nitrobase skips the extra upstream listing call completely. The moment one looks like a file, it verifies whether upstream actually ships a file at that path so a broken template manifest is rejected at render time instead of crashing on the user's VPS.

2. Why .db and .sqlite are deliberately not treated as config files

Guessing "is this path supposed to be a file?" by filename extension is easy to get wrong if you over-match (config/bind-files.ts):

  • Included (BIND_FILE_EXTENSIONS & BIND_FILE_NAMES): Config formats (conf, cfg, cnf, ini, toml, yml, yaml, json, xml, properties, env, exs, lua, tpl), startup scripts (sh, bash, js, mjs, cjs, ts, py, rb, php, sql), TLS/keys (pem, crt, cert, key, p12, pfx), static docs (html, txt), and extensionless standard files (Caddyfile, Dockerfile, htpasswd, .htpasswd).
  • Deliberately excluded: Runtime database and state extensions (db, sqlite, sqlite3, rdb, aof, log, lock, pid, sock, bin, dat).

Why exclude .db and .sqlite? Because applications frequently create those files themselves at runtime—and several Docker images actually expect a folder with a dotted name like ./data.db/. Excluding runtime state extensions prevents false warnings and broken deploys while still catching every missing config or init script.

3. Two-pass symlink checks and rmdir-only self-healing

Validating bind mounts on the VPS introduces two extra edge cases (apps/commands.ts):

1. emptyDirsAtFilesScript (before writing rendered files)
        └─ If target file path is a real, non-symlink empty directory:
           rmdir -- "$p"   (NEVER rm -rf! Non-empty dirs stay intact and fail safely)
        v
2. Write rendered files to app folder
        v
3. bindSourcesCheckScript (before compose up)
        ├─ realpath -m == expected path? (if not -> exit 98: symlink escape!)
        ├─ required file -> must pass [ -f ] (else exit 97)
        ├─ named file    -> never mkdir; warn if missing or empty dir
        ├─ data dir      -> mkdir -p if missing
        └─ realpath -e == expected path? (re-verify no symlink created/followed)
        v
4. docker compose up -d
        v
5. bindSourcesCheckScript (after compose up, after: true)
        └─ Any `named` file that is now an empty dir (Docker created it!) -> exit 97
Enter fullscreen mode Exit fullscreen mode

Look closely at two defensive details in that shell pipeline:

  • Defending against container-planted symlinks (BIND_ESCAPE_EXIT = 98): Because a running container can write inside a bind-mounted directory, a compromised container could replace a path component with a symlink pointing outside the app folder (such as /etc or another app's .env). Checking realpath -m before touching the path and realpath -e after guarantees Nitrobase never follows a symlink outside appRoot.
  • Self-healing with rmdir, never rm -rf (emptyDirsAtFilesScript): If an earlier broken deploy let Docker create an empty directory where clickhouse-init.sh belongs, Nitrobase removes it automatically before uploading the real file—using rmdir only after confirming [ -d ] && [ ! -L ] && realpath -m == path. If anything put real files inside that directory, rmdir refuses to delete it and the upload fails loudly instead of wiping data.

What bind-source validation still cannot catch

Checking that a bind-mounted config file exists as a regular file confirms Docker will mount a file rather than an empty directory. It cannot validate the syntax inside nginx.conf or clickhouse-config.xml, nor can it inspect bind sources that resolve dynamically from shell variables ($VAR) before Compose expands them (though resolvedBindSources reads the expanded paths from docker compose config --format json before up).

My checklist for bind-mounted files in Docker Compose

  • Never run mkdir -p blindly on every bind mount path—you will turn missing config files into directories yourself
  • Verify rendered config/script bind sources exist as regular files ([ -f ]) before docker compose up
  • Exclude runtime state extensions (.db, .sqlite, .sock, .pid, .log) from "must be a file" heuristics
  • Check realpath before and after creating/validating bind paths so container-writable volumes cannot plant symlinks outside the app directory
  • Clean up stale empty directories from previous failed deploys with rmdir only—never rm -rf
  • Re-check file-named bind sources after docker compose up to catch any empty directory Docker silently created

Docker's auto-directory behavior is convenient for data volumes and silently destructive for config files. A few lines of classification and realpath + rmdir checks turn a cryptic Is a directory crash loop into a clear error before the container ever boots.

Nitrobase source code: https://github.com/AydenGroup/nitrobase

Top comments (12)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the exact failure that makes Docker's auto-directory behavior so nasty — not the crash loop itself, but the second failure it plants: the empty directory it leaves behind blocks the next deploy from writing the real clickhouse-init.sh, so one missed file becomes two failed deploys. The detail I would steal is the rmdir-only self-healing — most people reach for rm -rf there, and the [ -d ] && [ ! -L ] && realpath -m == path guard is the difference between cleaning up and wiping data someone put in that directory. One gap I kept looking for: the [ -f ] check catches a missing file, but what about a file that exists and is empty — a fetch that wrote 0 bytes? The mount succeeds, the container still crash-loops, and the error is a different cryptic one. Do you treat a 0-byte rendered file as missing too, or is that a separate check? And for the soft-check bucket, what decides looksLikeFile for an extensionless path that is not in your standard names list — say ./data/seed — does it warn, or stay silent?

Collapse
 
hoagminh profile image
hoagminh •

Thanks for the detailed feedback! We reproduced both cases on Linux with Docker 29, and you're right on both counts.

The 0-byte file was a real gap. Our hard check only used [ -f ], so an empty file passed validation and failed later inside the container. In 0.1.0, we verify the byte count on disk against the rendered content before renaming the temp file into place, so a short write can never replace the existing file. We also added [ -s ] to the pre-up check for rendered files that are expected to have content. Intentionally empty upstream files are still allowed.

The extensionless path was another gap. ./data/seed stayed silent because we only consulted the upstream listing when a bind source looked like a file. That meant a missing upstream file could be mistaken for a data directory.

We've changed that too: for templates with an upstream source, every bind source that the deploy doesn't write is now checked against the upstream listing. If a file exists upstream but wasn't fetched, rendering is refused before deployment. The lookup uses cached tree listings, not file contents.

Both fixes are in 0.1.0. Really appreciate you pointing these out!

Collapse
 
userxnom profile image
Abdullah Naseer •

Thanks for reproducing both and for the detail — the byte-count check against the rendered content is the sharper fix, since it catches a short write too, not just a 0-byte file. And checking every unwritten bind source against the upstream listing, instead of only the ones that look like files, closes the ./data/seed gap cleanly. One question on the intentionally empty upstream files you still allow: how does Nitrobase know one is intentional — is it the upstream listing itself, or an allowlist in the template?

Thread Thread
 
hoagminh profile image
hoagminh •

Neither: there's no allowlist and no separate flag. The decision comes from the rendered content itself, and for upstream files that content is pinned by Git.

When the deploy renders a file that's also a bind source, it records whether the rendered content is empty. A non-empty one gets the [ -s ] check on the server, and an empty one only gets [ -f ]. So "intentionally empty" means "the content we rendered and wrote was empty".

What makes that trustworthy for upstream files is how they're fetched. Each one is downloaded at a pinned commit and checked against the size and blob SHA in the Git tree listing before it's used. A file that's 0 bytes upstream has the well-known empty-blob SHA (e69de29…) and size 0 in the listing. A non-empty upstream file can't come back empty by accident, because a truncated or empty download fails the SHA check and the render stops. So an empty rendered file means the repository really has an empty file at that path at that commit. The write's byte-count check then makes sure what lands on the server matches.

For files the template renders itself, the empty value comes straight from the render. A template that renders a file empty, for example because a variable is empty, gets an empty file on purpose, and it's treated the same way.

The one gap this leaves: if an upstream project ships an empty placeholder that its own image actually needs to be non-empty, we'd mount it as-is. That's the upstream's contract, though, and the container would fail the same way under plain docker compose up

Thread Thread
 
userxnom profile image
Abdullah Naseer •

That is a really clean answer — no allowlist, no flag, just the rendered content plus a Git-pinned SHA check doing the distinguishing. The empty-blob SHA detail is the part I like most: a 0-byte upstream file is provably empty, so a truncated download cannot sneak through as "intentionally empty." And being honest about the remaining gap is good too — the upstream placeholder its own image actually needs non-empty is the upstream's contract at that point, not the check's fault. Have you ever actually hit that gap in practice, or has it stayed theoretical so far?

Collapse
 
smtahosin profile image
S M Tahosin •

The inode side of single file bind mounts is the cousin of this bug that got me. Many editors and tools like sed -i don't edit the file in place, they write a new file and rename it over the old one, so the path gets a new inode while the container keeps the old mounted one. You end up staring at a config on the host that the container will never see until it restarts. Mounting the parent directory instead of the single file avoids that, and it pairs well with your pre-up checks. Do you treat directory mounts as the default for config now, or only for files that change during deploys?

Collapse
 
hoagminh profile image
hoagminh •

You were absolutely right about the inode issue — we were doing exactly that. Rendered files were written to a temp file and renamed over the target, which gives them a new inode. And docker compose up -d doesn't recreate containers just because a bind-mounted file's content changed.

We've addressed this in 0.1.0:

  • Unchanged files are no longer rewritten, so their inode and mtime stay intact. We still correct their mode, including 0600 for secrets and the executable bit, without touching the mtime.
  • After up, we check whether a service's running container started before the mtime of any single file it bind-mounts. If it did, we recreate that service with --no-deps --force-recreate and restart its dependents. The comparison uses nanosecond precision where supported, and it also catches retries after a failed deploy.
  • Directory binds are left alone, since the application can see the updated file immediately and may reload it itself. We've also updated the template docs to recommend directory mounts for configs that support hot-reloading, as you suggested.

Thanks for catching this. It exposed a real deployment issue that the usual file-existence checks wouldn't have caught!

Collapse
 
smtahosin profile image
S M Tahosin •

That mtime check is a neat way to close the loop. Comparing container start against the file's mtime catches the failed-deploy retry case too, which a simple "did we write it this run" flag would miss. Preserving mtime while still fixing the mode is the right call as well, since chmod only touches ctime. Most tools get one of those two wrong. Shipping the fix in 0.1.0 already is fast work, appreciate you taking the report seriously.

Thread Thread
 
hoagminh profile image
hoagminh •

Thanks for taking the time to dig into the implementation and point out these edge cases. Feedback like this genuinely helps us make Nitrobase more reliable.

Thread Thread
 
smtahosin profile image
S M Tahosin •

Glad it was useful. That empty-directory trap has bitten me before, so it was an easy one to spot. Good luck with Nitrobase.

Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

The same thing happens without Compose. docker run -v ./file:/path creates a root-owned directory when the source is missing, while --mount type=bind,source=./file,target=/path refuses to start with "bind source path does not exist". In Compose the long syntax gives the same behaviour if you set bind: {create_host_path: false}, so the failure shows up at up time instead of as a crash loop inside the container. For files that must exist I would use that, plus a plain [ -f ] check in the deploy script before up. The check also covers the stale empty directory left by an earlier failed run, and rmdir only removes a directory that is empty, so it cannot take real data with it.

Collapse
 
hoagminh profile image
hoagminh •

Thanks for pointing out the distinction between -v and --mount, and the create_host_path: false option in Compose. That's a useful way to catch missing bind sources before they turn into container failures.

We've now added bind.create_host_path: false to rendered files that must exist. That means a missing file fails at docker compose up with "bind source path does not exist", rather than silently becoming a directory and causing a crash loop — even when someone runs Compose manually.

The exception is SELinux-labelled binds (:z / :Z), since the Mount API path doesn't support relabeling. Those still rely on the pre-up [ -f ] check and our rmdir-only cleanup.

Also, for custom Compose files where there's no upstream listing to consult, we now report the likely missing bind source when a service crash-loops and the directory created by the pre-up check is still empty.

These changes are in 0.1.0. Thanks again for the practical suggestions!