Subscribe

Running Ghost 6 and MySQL 8 on a 2 GB t3.small

I planned the new blog host on a t3.medium, then moved it to a t3.small to use a reservation I already had. These are the MySQL, swap and Docker log settings that let Ghost 6 and MySQL 8.4 share 2 GB.

Running Ghost 6 and MySQL 8 on a 2 GB t3.small

When I planned the move of this blog to Docker Compose, the new host was a t3.medium. 4 GB of memory, Ghost 6, MySQL 8.4 and Caddy, and nothing to worry about. Then I remembered I already pay for a regional t3.small reservation, and that it runs for a few more months.

A t3.medium is twice the price of a t3.small. Running one on demand next to a reservation that's already paid for made no sense. So the plan changed: same stack, 2 GB of memory.

Let us look at what I changed so that fits.

What runs on the box

The old host was a t3.small too, with Ghost 4 and MySQL 5.7 installed straight on Ubuntu. The new one runs three containers.

services:
  caddy:
    image: caddy:2.10-alpine
  ghost:
    image: ghost:6-alpine
  db:
    image: mysql:8.4

Caddy barely uses anything. Ghost is a Node process and takes what it takes, there isn't much to tune there. MySQL is the one with knobs, and its defaults are written for a server that has the machine to itself. So most of the work went into MySQL.

Tuning MySQL 8.4

All of it is passed as command line flags in compose.yml. I like this better than a separate my.cnf file, because everything is in one place and shows up in the diff.

  db:
    image: mysql:8.4
    restart: unless-stopped
    command:
      - --character-set-server=utf8mb4
      - --collation-server=utf8mb4_0900_ai_ci
      # Tuned for a 2 GB t3.small shared with Ghost
      - --innodb-buffer-pool-size=256M
      - --performance-schema=OFF
      - --max-connections=50
    volumes:
      - ./data/mysql:/var/lib/mysql

Three settings matter here.

innodb-buffer-pool-size

This is the big memory cache for table data and indexes. In MySQL 8.4 the default is 128M, so 256M is actually a bit more than default. I set it on purpose anyway. A blog database is small, 256M is plenty to keep the whole thing in memory, and I'd rather have the number written down than depend on a default that may change in a later version.

If you copy this for a bigger site, check how big your data really is first.

SELECT table_schema,
       ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) AS size_mb
FROM information_schema.tables
GROUP BY table_schema;

If Ghost schema is well under the buffer pool, you're fine.

performance-schema=OFF

Performance schema is MySQL's internal instrumentation. It's useful when you're chasing a slow query on a busy server. On a small box it holds a fair amount of memory just by being on, and I have no use for it on this blog. Off.

max-connections=50

The default is 151. Every connection gets its own buffers, so a limit that's far higher than needed is just room for memory to run away. Ghost talks to MySQL through a small connection pool and is the only client. 50 is still a lot more than it needs.

You can confirm all three took effect after the container starts.

docker compose exec db sh -c 'mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e "
  SELECT @@innodb_buffer_pool_size / 1024 / 1024 AS buffer_pool_mb,
         @@performance_schema,
         @@max_connections;"'

The single quotes matter. $MYSQL_ROOT_PASSWORD has to be expanded inside the container, where it's set, and not on the host where it isn't.

Making Ghost wait for MySQL

On a small instance MySQL takes a while to come up, specially on first boot when it creates its data directory. Ghost starting before the database is ready means a crash and a restart loop for no reason. A healthcheck on db and a condition on ghost fixes that.

  db:
    healthcheck:
      test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p\"$$MYSQL_ROOT_PASSWORD\" --silent"]
      interval: 5s
      timeout: 5s
      retries: 30
      start_period: 30s

  ghost:
    depends_on:
      db:
        condition: service_healthy

The $$ is Compose escaping. It passes a literal $ through, so the shell in the container expands it.

Swap as a safety net

Cloud-init creates a 2 GB swap file the first time the instance boots.

if [ ! -f /swapfile ]; then
  fallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
  echo '/swapfile none swap sw 0 0' >> /etc/fstab
fi

Swap does not make a 2 GB machine into a 4 GB one. If the box is swapping all the time, it's too small and the site will feel slow. What swap does is stop the kernel OOM killer from picking MySQL during a short spike, like a Ghost upgrade running migrations or docker compose pull unpacking a new image. Without swap that spike kills a process. With swap it's a slow minute and then everything settles.

The if makes it safe to run twice. The fstab line is what keeps swap on after a reboot. Easy to forget, and then the swap is quietly gone after the next restart.

To see where memory actually goes, these two are enough.

free -m
docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}\t{{.CPUPerc}}'

free -m tells you if swap is being used. docker stats tells you which container is using the memory. Check them once right after start and again after the blog has been running for a day, the numbers after a day are the ones that matter.

Logs were the other problem

This one was disk space, not memory. When I looked at the old host before the migration, the Ghost content folder had about 35 MB of images and themes and 2.5 GB of logs. Years of logs that nobody read, sitting on a 30 GB disk.

So on the new host Ghost logs only to stdout,

      logging__transports: '["stdout"]'

and Docker's json-file driver caps what it keeps. I wrote it once as a YAML anchor and reused it on all three services.

services:
  caddy:
    logging: &logging
      driver: json-file
      options:
        max-size: "10m"
        max-file: "3"

  ghost:
    logging: *logging

  db:
    logging: *logging

That's at most 30 MB per container, three containers, so under 100 MB of logs ever. You can check the real size anytime.

sudo du -sh /var/lib/docker/containers/*/*-json.log

The other way to do this is in /etc/docker/daemon.json for every container on the host. I kept it in the compose file, so it moves with the stack if I ever rebuild the host.

Where the cost lands

The reservation is regional, which means it isn't tied to one instance or one availability zone. It applies to any t3.small running in the region. When the old host was terminated and the new one kept running, the discount just moved over. Nothing to change in the console.

If I'd stayed on t3.medium, I'd have paid on demand for the medium while the reservation either covered nothing or kept covering the old box I was trying to get rid of. And instance_type is just a variable in Terraform, so moving down was one line.

variable "ghost_instance_type" {
  description = "Instance type for the Docker Compose Ghost host"
  type        = string
  default     = "t3.small" # matches the regional t3.small reservation
}

When the reservation ends I'll look at the numbers from docker stats and free -m again before renewing it. If the blog is still happy on 2 GB, it'll be another t3.small.