Subscribe

Migrating a Ghost-CLI blog to Docker Compose with Caddy and MySQL 8

This blog ran on a hand-built 2020 server with Ghost-CLI, Nginx, acme.sh and MySQL 5.7. Here is how I moved it to Docker Compose with Caddy, Ghost 6 and MySQL 8.4, checked the data and cut over by moving the Elastic IP.

Migrating a Ghost-CLI blog to Docker Compose with Caddy and MySQL 8

The server behind fenixara.com was built by hand in 2020. A t3.small with Ubuntu 18.04, Ghost 4.1.2 installed with Ghost-CLI, MySQL 5.7 on the same box, Nginx in front and three acme.sh certificates renewed by a root cron job. No backups. I had not really looked at it in years, and it kept working, so I left it.

When I finally looked at it properly this month, Node was gone. It had been uninstalled at some point and nobody noticed, because the Ghost process was still running from a deleted /usr/bin/node. It would serve the blog happily until the next restart or reboot, and then it would never come back.

That changed the plan from "some day" to "this week". In this blog we will look at how I moved it to Docker Compose with Caddy, Ghost 6 and MySQL 8.4, and how I checked nothing got lost on the way.

What the old box looked like

You can check for the deleted binary problem yourself. If the process is running from a file that no longer exists, ls on its exe link says so.

pid=$(pgrep -f "ghost run" | head -1)
sudo ls -l /proc/$pid/exe
# lrwxrwxrwx 1 ghost ghost 0 ... /proc/1234/exe -> /usr/bin/node (deleted)

The rest of the inventory were not great either. Ubuntu 18.04 is end of life. The database was about 285 MB. The content folder had 35 MB of images and 2.5 GB of logs. The earliest certificate expiry was 2026-12-10, so I had a few weeks before that would bite too.

Protect what exists first

Before writing any Terraform, I took an EBS snapshot of the old volume and a database dump. If the old Ghost died in the middle of all this, I wanted to have something.

Root login to MySQL was denied, and I didn't want to go poking at the root password on a box I was scared to restart. Ghost's own database user can read everything Ghost needs though. The credentials are in Ghost-CLI's config.production.json, so I put them in a temporary option file, ran the dump and deleted the file.

cd /var/www/fenix
umask 077
jq -r '"[client]\nuser=\(.database.connection.user)\npassword=\(.database.connection.password)"' \
  config.production.json > /tmp/dump.cnf

mysqldump --defaults-extra-file=/tmp/dump.cnf --single-transaction --routines \
  --no-tablespaces "$(jq -r .database.connection.database config.production.json)" \
  | gzip > ~/ghost_db.sql.gz
rm -f /tmp/dump.cnf

zcat ~/ghost_db.sql.gz | tail -1   # should say "-- Dump completed on ..."
tar czf ~/content.tar.gz --exclude=content/logs content

The option file keeps the password out of the process list and out of shell history. The --no-tablespaces flag is there because an app user usually doesn't have the PROCESS privilege that newer 5.7 mysqldump wants for tablespace info.

Postgres was not an option

I did ask whether this was the time to move to postgres. It isn't. Ghost only supports MySQL 8 in production, and running it on anything else means a fork you can never upgrade. So MySQL stayed. I picked 8.4 LTS, because 8.0 reached end of life in April 2026 and there is no point migrating onto something that is already dead.

The new host in Terraform

The new instance is in Terraform this time: Ubuntu 24.04, 40 GB encrypted gp3, IMDSv2 required and an instance profile with SSM so I don't need SSH for day to day work.

resource "aws_instance" "ghost" {
  ami                  = data.aws_ami.ubuntu_2404.id
  instance_type        = "t3.small"
  iam_instance_profile = aws_iam_instance_profile.ghost.name
  user_data            = file("${path.module}/ghost/cloud-init.sh")

  metadata_options {
    http_endpoint = "enabled"
    http_tokens   = "required"
  }

  root_block_device {
    volume_type = "gp3"
    volume_size = 40
    encrypted   = true
  }
}

I first wrote it as a t3.medium. MySQL 8 plus Ghost 6 on 2 GB sounded tight. Then I remembered I already had a t3.small reservation running until 2027, so I tuned MySQL down and added 2 GB of swap instead of paying for a bigger box. It has been fine.

The Compose stack

Three services. Caddy on 80 and 443, Ghost on 2368 inside the network only, and MySQL 8.4 inside the network only. This is a trimmed copy of the real file.

name: ghost

services:
  caddy:
    image: caddy:2.10-alpine
    restart: unless-stopped
    ports: ["80:80", "443:443", "443:443/udp"]
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - ./data/caddy/data:/data
    depends_on: [ghost]

  ghost:
    image: ghost:6-alpine
    restart: unless-stopped
    environment:
      url: https://fenixara.com
      database__client: mysql
      database__connection__host: db
      database__connection__user: ${MYSQL_USER}
      database__connection__password: ${MYSQL_PASSWORD}
      database__connection__database: ${MYSQL_DATABASE}
    volumes:
      - ./data/ghost:/var/lib/ghost/content
    depends_on:
      db: { condition: service_healthy }

  db:
    image: mysql:8.4
    restart: unless-stopped
    env_file: .env
    command:
      - --innodb-buffer-pool-size=256M
      - --performance-schema=OFF
      - --max-connections=50
    volumes:
      - ./data/mysql:/var/lib/mysql
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p\"$$MYSQL_ROOT_PASSWORD\" --silent"]
      interval: 5s
      retries: 30

Every service also gets json-file logging with max-size: 10m and max-file: 3. After seeing 2.5 GB of logs on the old disk, that was the first thing I added.

A small systemd unit starts the stack at boot. It's a oneshot that just runs docker compose up -d.

[Unit]
Description=Ghost docker compose stack
Requires=docker.service
After=docker.service network-online.target

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/ghost
User=ubuntu
ExecStart=/usr/bin/docker compose up -d --remove-orphans
ExecStop=/usr/bin/docker compose down

[Install]
WantedBy=multi-user.target

The Caddyfile is about ten lines and replaces Nginx, acme.sh and the cron job. I wrote that part up separately in the post on automatic HTTPS and www redirects with Caddy.

Getting the data across

The dump was restored into MySQL 8.4, then Ghost had to go 4.1.2 to 4.48.9 to 5.130.6 to 6.75.0, one major at a time, in throwaway containers. Two things made this harder than it sounds. Ghost 4 can't talk to MySQL 8.4 with the default auth plugin, and Ghost logs "Ghost booted" before its migrations are actually done. The first one cost me a bit of reading. The second one cost me a full rerun. Both have their own posts, so I'll skip the details here.

Checking nothing got lost

I didn't want to eyeball a few posts and call it done. I compared the old and new databases properly.

Row counts first, for every content table, run against both databases.

for t in posts tags posts_tags users members; do
  printf '%-12s ' "$t"
  mysql -N -e "select count(*) from $t" ghost_db
done

Counts can match while content differs, so I also hashed the actual content. Ordering by id makes the output stable on both sides.

mysql -N -e "select id, md5(concat_ws('|', html, plaintext)) from posts order by id" ghost_db | md5sum
mysql -N -e "select id, name, slug from tags order by id" ghost_db | md5sum
mysql -N -e "select post_id, tag_id from posts_tags order by post_id, tag_id" ghost_db | md5sum

All equal, revisions too. A couple of things are expected to differ. The Ghost 5 migration turns user id 1 into an ObjectId, the old tokens table gets emptied, and Ghost 6 adds its own roles and settings rows. Don't panic when you see those.

For the files, a sorted list of checksums from each side is enough. All 319 files were byte identical.

cd content && find images themes data settings -type f -exec md5sum {} + | sort -k2 > /tmp/files.md5
diff /tmp/old-files.md5 /tmp/files.md5 && echo "files match"

Finally every URL in the sitemap, after cutover, over HTTPS.

locs() { curl -s "$1" | grep -o '<loc>[^<]*' | sed 's/<loc>//'; }

for sm in $(locs https://fenixara.com/sitemap.xml); do locs "$sm"; done > urls.txt
while read -r u; do
  echo "$(curl -s -o /dev/null -w '%{http_code}' "$u") $u"
done < urls.txt > status.txt

wc -l < urls.txt
grep -v '^200 ' status.txt   # prints nothing when every URL is fine

93 URLs, all 200.

Cutover by moving the Elastic IP

DNS for the domain is at GoDaddy, not Route 53, and I didn't want to wait on TTLs. The old box already had an Elastic IP. So the cutover was just pointing that EIP at the new instance in Terraform.

resource "aws_eip" "blog" {
  domain   = "vpc"
  instance = aws_instance.ghost.id
}

The DNS records never changed. Caddy saw the traffic arrive and had Let's Encrypt certificates for the apex, www and blog in about a minute.

There was about 5 minutes of downtime and it was my own fault. Same IP, but a new host, so a new SSH host key. My SSH client refused to connect with the big "REMOTE HOST IDENTIFICATION HAS CHANGED" warning, and I spent those minutes sorting that out instead of starting Caddy. If I did it again, I'd remove old key with ssh-keygen -R 203.0.113.10 before moving the IP, or better just use SSM and not have SSH in the path at all.

Decommission

I kept the old host for a day in case something was missing. Nothing was. Then a final snapshot of its disk, terraform to remove it, and it was gone along with its security group.

The new host has nightly backups to S3 and daily EBS snapshots, which the old one never had in six years. That alone was worth the week.