A small server can host more than one service, and paying for two when one is nearly idle makes no sense. But sharing carries an obvious risk: a mistake while deploying one can take the other offline. When I set up Clic'n Done PDF on the same server as TeamLedger, which was already in production, the requirement was clear: TeamLedger must not notice a thing.
1. One doorman, one file per site
Only one program can listen on ports 80 and 443, so both services come in through the same Caddy, which also obtains and renews HTTPS certificates on its own. The main configuration imports one file per site, and each project only touches its own.
Two details matter more than they seem:
- Validate before applying. If the new file has an error, the deploy catches it, restores the previous one and stops. A broken configuration would take both sites down.
- Reload, never restart. A reload applies the configuration without dropping open connections; a restart would leave TeamLedger offline for a few seconds.
2. Each on its own port, and only locally
Each service listens on a different port and only on the server's local address, so from outside you can only reach them through Caddy. Nothing needs to be opened in the firewall, and the deploy checks that the port is free before it starts.
3. Limits so a heavy conversion can't eat the machine
Converting a hundred-page PDF keeps a CPU busy for several seconds. So that it never slows TeamLedger down, each conversion runs in its own container with strict limits:
- capped memory, CPU and number of processes;
- no network: the container can't connect to anything, not even the other service;
- a read-only disk except for a temporary folder, and no privileges;
- logs with a maximum size, so they can never fill the disk;
- its own system user, separate from TeamLedger's.
We checked it with a load test: three 80-page documents converting at once used about one core and under 80 MB each, and TeamLedger's response time stayed at around two tenths of a second, the same as before.
4. The trap: the neighbour's image cleanup
This one nearly slipped through. TeamLedger's deploy ends with a Docker cleanup
(docker image prune -a) that deletes every image not used by a container at that
moment. It's a good habit to keep the disk from filling up… and a problem for us: the
converter's image is only in use while a conversion is running. Any TeamLedger deploy
during a quiet moment would have deleted it, and the next conversion would have failed.
The fix is small: a sleeping container that uses that image and does nothing else. It has no network, 32 MB of memory and at most 5% of one core, and its only job is to keep the image marked "in use". The neighbour's cleanup keeps working for everything else.
What is forbidden, on the other hand, are global cleanups: no deleting volumes, which is where TeamLedger's database lives, and no restarting Docker.
5. Check, don't assume
After the deploy it isn't enough that our site responds. We compared the neighbour before and after: its containers hadn't restarted, its volumes and images were unchanged, its Caddy files hadn't been touched, Caddy and Docker hadn't restarted, and the firewall had the same rules.
The short list
- One proxy in front of everything and one file per site; validate and reload, never restart.
- Each service on its own port, and only locally.
- CPU, memory and log limits for anything that can grow.
- Find out which automatic cleanups the others run and protect yourself from them.
- Nothing global: no general cleanups, no restarts, no firewall or SSH changes.
- Afterwards, check that the other service is exactly as it was.
None of this is sophisticated, but written down as a list it prevents the silly mistake that takes someone else's service down on a Friday afternoon.
Comments or ideas? Message me on LinkedIn. LinkedIn →