Skip to content

Two Silent Traps When Verifying a WordPress Migration on Docker

Adityo Guni Waluyo

Rollback dies at rm -rf because uploads are owned by the container user, and title parity fails on entity encoding. Two real traps, both verified.

TL;DR

A rollback script died because Docker-owned files inside wp-content/uploads couldn't be deleted from the host, so deletion now runs through the container via docker compose exec -T and fails closed when the stack is down. A second bug compared decoded titles against entity-encoded pages, fixed by normalizing both sides with html.unescape. The re-run finished clean: 58,959 files, exact parity.

The rollback script stopped at the least suspicious line in the whole flow: deleting the wp-content/uploads directory. The rm -rf command exited with code 1, and the script died immediately because it runs under set -e. The database snapshot had already been restored; the uploads half of the rollback never ran.

The first guess pointed at stale entries at the top level locking the directory. The cleanup scan was limited with -maxdepth 1, it came back clean, and the same failure reappeared on the next run. Container-owned entries do not stop at the top level. The WordPress runtime creates year and month subdirectories inside uploads, and entries deep in that tree are owned by the container-side user too.

The root cause sits in Docker's file-ownership model. The USER instruction in a Dockerfile sets the default user used for RUN instructions and for the ENTRYPOINT and CMD processes at runtime [2]. Files written by processes inside the container end up owned by that user. From the host side, a regular user has no right to delete them, and every rm attempt is refused by the operating system.

Clear container-owned uploads through the container

The fix moves deletion to the container side. The flow: scan the whole uploads tree for entries the host user cannot write to with find ! -writable at any depth, then delete each entry through the running WordPress container with docker compose exec -T [1]. The -T flag matters in scripts because it disables interactive TTY allocation.

stale="$(find "${UPLOADS_DIR}" ! -writable 2>/dev/null || true)"
[ -n "${stale}" ] || exit 0

cid="$(docker compose ps -q wordpress || true)"
if [ -z "${cid}" ]; then
  echo "uploads contain entries owned by the container and the stack is down:" >&2
  echo "${stale}" >&2
  exit 1
fi

while IFS= read -r entry; do
  rel="${entry#"${UPLOADS_DIR}"}"
  docker compose exec -T wordpress \
    sh -c "rm -rf '/var/www/html/wp-content/uploads${rel}'"
done <<< "${stale}"

When the container is down, the script stops with a message listing the problematic entries instead of attempting deletion and failing silently. The principle is fail closed: a cleanup operation is destructive, so it must never run rm through a path it cannot remove.

The logic lives in one shared library file and is called at two points: before the media rsync as a pre-sync repair, and in the middle of the rollback before the uploads snapshot is restored. One implementation, two critical call sites, no duplicated logic to drift apart.

Normalize titles before comparing them

The second trap comes from a different direction: the title parity check. The verifier compares titles from the extraction with titles on the served pages, and several titles failed while the pages looked perfectly right. Titles containing special characters, ampersands, quotes, and en dashes, were the most recognizable victims.

The cause is a representation mismatch. The extraction script stores titles already decoded with PHP's html_entity_decode [4]. The served page, on the other hand, writes the title element entity-encoded: &amp; for the ampersand character and &#8211; for an en dash. The HTML standard allows text to be mixed with character references precisely to escape characters that could not otherwise legally appear in the text [5]. A raw string comparison between those two representations will always fail, even when the content is identical.

The fix normalizes both sides before comparing. The line pulled from the served page is processed with Python's html.unescape, which converts all named and numeric character references to Unicode characters under HTML 5 rules [3]:

normalize_title() {
  python3 -c 'import html, sys; print(html.unescape(sys.stdin.read().strip()))'
}

A quick demonstration: the string A &amp; B &#8211; C decodes to A & B – C, and now both sides are compared in the same form.

Re-run until parity is exact

With both fixes in place, the migration flow was re-run starting from the media sync step. It finished with exit 0: 58,959 files, 8.6 GB in total, exact parity with no differences. The title check passed completely once normalization was applied, including titles containing ampersands, quotes, and en dashes.

Two lessons from this incident reach well beyond WordPress migrations. First, a verifier is only valid when both sides of the comparison live in the same representation; mismatched encoding produces false failures. Second, destructive operations must fail closed; stopping with clear instructions is far safer than executing a deletion on a path you cannot control.

Sources

  1. docker compose exec, Docker Docs
  2. Dockerfile reference, Docker Docs
  3. html.unescape, Python 3 documentation
  4. html_entity_decode, PHP Manual
  5. 13.1.4 Character references, HTML Standard

Related articles