When Docker Turns Your Config File Into a Folder
I triple-checked the password. Copied it once again, straight from the vault, pasted it into the Terraform variable, verified it reached the cloud-init template. The credentials were correct. And yet, every time the VM came up, the staging application failed running as it could not connect to the repo - no authentication error, no “bad credentials.” What the heck!
The Setup
Here’s the situation: For a customer, we run VMs provisioned with cloud-init, backed by persistent data disks. The data disk survives VM recreation, which is the whole point. The application data, the Docker volumes, the container state - all persists even when we tear down and rebuild the VM itself.
Docker stores its data on that persistent disk too. And Docker containers are
configured to auto-start. Thus, when the VM boots, Docker finds its state on the
persistent disk and picks up where it left off. Containers that were running
before the VM was destroyed are running again, within seconds of boot - idillic,
minimum downtime for the reboot, no need to wait for the explicit
docker compose up command.
Cloud-init, meanwhile, is doing its thing: installing packages, writing config
files, and at some point, running docker login to authenticate against our
private container registry.
This works perfectly on a fresh VM. First boot, no persistent state, everything runs in order. On the second deployment, however, when you recreate the VM you get the problem. Exacerbated by the fact that this second run happens a lot of time after the initial creation of the script.
The Symptom
The docker login command was failing with an error that didn’t mention
passwords at all. Something about not being able to write to
/root/.docker/config.json. Which was strange, because that path shouldn’t
exist yet on a fresh VM.
So I SSH’d in and looked:
$ ls -la /root/.docker/
drwxr-xr-x 2 root root 4096 ... config.json
config.json was a directory. Not a file. A d i r e c t o r y.
The Race
Here’s what happens. On a recreated VM, the OS disk is fresh but the data disk
is not. Docker starts after the install and finds containers from the previous
deployment. Those containers have bind-mounts defined, including one that maps a
host path to /root/.docker/config.json.
In the previous life of the VM, that file existed. In its current life as a fresh VM, it doesn’t. So Docker does what Docker always does with a bind-mount pointing to a nonexistent host path: it creates it. And Docker’s default behavior when creating a missing bind-mount path is to create a directory (not a file, because it can’t know which one you intended).
By the time cloud-init reaches the docker login step, the path
/root/.docker/config.json already exists as an empty directory. Docker’s login
command tries to write a JSON file there, finds a directory instead, and fails.
The credentials were never the problem.
The Fix
The fix is almost anticlimactic. In cloud-init’s write_files section (which
runs early, before packages are installed and before Docker starts), pre-create
the file:
write_files:
- path: /root/.docker/config.json
content: |
{}
owner: root:root
permissions: "0600"
And that’s it. Now, when Docker starts and its containers try to bind-mount that
path, they find a file. The bind-mount works correctly. And when docker login
runs later, it overwrites the empty JSON with real credentials. No race, no
directory, no mystery.
For extra safety, we also added a fallback in the login step that checks whether
config.json is actually a file. If something still went sideways, it cleans up
the directory and recreates the file. Belt and suspenders.
There is a catch though. If the applications running on your containers need the contents of the file upon container startup, you will have to make sure the container gets restarted to read the new values.
The Broader Lesson
Persistent state across VM recreations creates assumptions that fresh provisioning doesn’t expect. Your cloud-init script is written for a clean machine, but the data disk remembers a machine that no longer exists. Anything that auto-starts from persistent state (Docker containers, systemd services configured on the data disk) can create side effects before your provisioning scripts get a chance to run.
The ordering of cloud-init phases matters more than you’d think. write_files
runs before package installation. runcmd runs after. Docker’s systemd unit
starts somewhere in between. If you need something to exist before Docker
touches it, write_files is your friend. runcmd is too late.
If you run into something similar and need a hand, we’re happy to help.