ZENSHINZO全心像Truth Heart Vision

Signal offlineSoutheast Asia · UTC+7

Words

7 Habits for Running Everything in Docker Compose

tech

How I run services on my machines, and the mistakes behind the rules

7 Habits for Running Everything in Docker Compose

I've worked as a sysadmin, network engineer and developer for years. My machines are a few always-on Linux servers, a GPU box, small boards, a test box and a Mac. Claude Code, my main AI agent, works on them under my rules.

Two of those rules, in my own words: "docker first unless there is a reason why it NEEDS to run native", and "we should standardize that docker is always compose ya? unless it cant use compose".

These are the seven habits behind them.

Habit 1: Docker first. Native needs a named reason

New services default to Docker. To run something natively, as a systemd service on the host, name what it can't do in a container: direct hardware, a kernel interface, a host-level hook. Convenience isn't a reason.

On my GPU box, the host has the GPU driver, Docker and the NVIDIA Container Toolkit, and nothing else. Each AI generator tool runs in its own Compose stack.

Habit 2: Every container lives in a Compose file

A Compose file records the image, ports, volumes, environment and restart policy. You can version it, diff it and rebuild from it. A container started with docker run -d keeps all of that only inside Docker.

So a service started with a bare docker run is drift. Every container gets a file. An example, with a placeholder image:

services:
  app:
    image: example/app:1.2.3
    restart: unless-stopped
    ports:
      - "127.0.0.1:8080:8080"
    volumes:
      - ./data:/data
    mem_limit: 512m
    cpus: 1.0
    pids_limit: 200

Habit 3: One layout, data on volumes, a copy off the box

Every stack lives in /opt/docker/<stack>/, with its compose file. Scripts the host runs go in /opt/services.

Data lives on volumes, not inside the container. The AI tools keep their models and outputs on volumes on the fast disk, so a rebuild never loses them.

Compose files get copied into my notes vault: every AI tool's, and others like the camera relays'. The file alone isn't a backup. It doesn't hold the data on the volumes, and secrets don't belong in the copy.

That layout makes a move simple. When the stacks for my other AI agents (Grok, Codex and Gemini) moved to another server, they were copied, the images were rebuilt there, and each one answered a test prompt with its login intact. The old copies were moved aside, not deleted. How those agents run in Docker is in 7 Habits of My AI Agent Setup.

Habit 4: Back up and write the rollback before you change it

Before a compose file is edited, a dated copy goes next to it, like docker-compose.yml.bak-20261119.

When a camera relay job moved to a new small board, the old board ran one native systemd unit per camera. The new board got one compose file with one service per camera. The rollback was written down before the cutover: docker compose down on the new board, then re-enable and start the old units on the old one.

Then prove it comes back by itself. After its power was moved, the board came back on its own with every container started. After a cold reboot of the GPU box, the mounts, the data and the GPU were all checked. That proves the start-up, not the rollback.

Habit 5: "Up" doesn't mean it's working

One of those camera relays stopped sending. Its container stayed "Up". There was no error and no restart. Docker saw nothing wrong.

Now a watchdog, itself a Compose stack, asks the stream server the relays send to which streams have data. A running relay that has been up for more than 90 seconds, and shows no data for three checks in a row (about 90 seconds), gets restarted. A stopped relay is never touched. If the stream server can't be reached, the watchdog does nothing.

The watchdog had a bug of its own. It only matched one stream-name format. When the stream server changed the format, no healthy relay matched, so each one was restarted about every three minutes until it was fixed. Check the watchdog too.

Habit 6: Pin images and cap resources

Image tags are pinned. The monitoring agent is pinned to one version. A camera stream image is pinned by digest. With a placeholder image and digest:

image: example/stream@sha256:<digest>

An update happens when I change the tag or the digest in the file, then pull and recreate the service.

The limits I use are mem_limit, cpus and pids_limit. They cap host memory, CPU and processes, not the GPU. My GPU box has one 16 GB GPU, so the heavy AI tools run one at a time.

Habit 7: An old native service is drift, not precedent

Claude once surveyed a server and found services running natively beside the Docker stacks. It read the split as a choice, and recommended native for a new service because a related one was native. That one was never supposed to be native.

A native service already on a box is not a template. Moving one is a separate job, done when I decide.

The checklist

  • New services in Docker. Native only with a named reason.
  • Every container in a Compose file. No bare docker run.
  • Stacks in one folder layout. Data on volumes. Compose files copied off the machine, secrets kept out of the copy.
  • Dated backup and a written rollback before a change. Start-up proved after a power move or a cold reboot.
  • Output checked, not just "Up". The watchdog checked too.
  • Image tags or digests pinned. Memory, CPU and process limits set.
  • An old native service treated as drift, never copied.

Routine Compose work on my machines is pre-approved for Claude: creating, starting, stopping, restarting and updating stacks. Installers, credentials and core changes still need a yes. Those rules are in 7 Habits for Letting an AI Agent Work on Your Machines. Why the ports are bound to 127.0.0.1 or Tailscale is in 7 Habits for Securing Your AI Agents and Systems.