A series on building a company's IT with containers and open source
When people see the lab's docker-compose.yml, they usually scroll for a while, go quiet, and then ask: "Why isn't this split up?"
Fair question. It's a long file. It has the scroll bar of a Terms of Service page.
But one file, one project and one network is the best decision I made in the whole lab. Let me explain before you close the tab.
One project, one network
Everything runs in a single Compose project, home-lab, on one network, home-lab-net. That gets me three things for free.
Every service can reach every other one by name. Grafana talks to zabbix-web. Keycloak talks to postgres. Apps send mail to mailserver:25. There isn't a single IP address in the configuration, which is exactly how many IP addresses I want to remember.
One command shows everything:
docker compose -p home-lab ps
One command applies everything:
docker compose -p home-lab up -d --remove-orphans
Delete a service from the file, and the next deploy removes it from the host. No orphaned containers haunting the place.
The repository is small:
lab/
└── home-lab/
├── docs/home-lab-handbook.md the ONLY lab document
└── docker/
├── docker-compose.yml every service
├── .env.example template for the secrets file
├── scripts/ bootstrap, configure-*, repair-*
├── deploy/hatchery/ the automation
├── patched/ locally patched images
└── <app>/ per-app config
Note "the ONLY lab document". I started with a README per app. They drifted apart, contradicted each other, and eventually I trusted them about as much as a vendor's "this update requires no restart". Now there's one handbook, and changing the lab means changing the handbook in the same commit.
YAML anchors: copy-paste, but respectable
Seventy services share a lot of settings. YAML anchors let you write them once:
x-hardening: &hardening
security_opt:
- no-new-privileges:true
pids_limit: 1024
grafana:
<<: *hardening
mem_limit: 2g
Every service merges that block. Every service also gets a memory limit of about twice what it normally uses.
That sounds like bureaucracy until the day one container goes rogue. Without limits, one misbehaving app eats the whole Docker VM and takes everyone with it. With limits, it suffers alone, as nature intended.
(Spoiler for post 8: limits can also be too tight, and that's how I met Grafana at 1000% CPU.)
Two databases, not twenty-five
Most vendor Compose files come with their own database container. With 25+ apps, that's 25 database servers to patch, tune and back up. No thank you.
Instead, there are two:
- MariaDB for the PHP gang: Drupal, Nextcloud, iTop, Matomo, HumHub, Moodle, LimeSurvey, EspoCRM.
- PostgreSQL for everyone else: Keycloak, Zabbix, Grafana, SonarQube, n8n, LiteLLM, Sympa, Plane.
A script, bootstrap-databases.ps1, creates every database and user and reconciles passwords with the secrets file on every start. It never drops anything. When an app says "database unreachable", running that script is step one, and usually also the last step.
There are exceptions. GitLab uses its own bundled PostgreSQL, because it wanted a newer major version than the shared server runs. That's fine. The rule is "share by default", not "share or die".
One .env to rule them all
Every setting and every secret lives in one file: home-lab/docker/.env. Git ignores it. The committed .env.example is the template.
The Compose file has no password fallbacks. If a variable is missing, this fails before anything starts:
docker compose -p home-lab config -q # silence is golden
The trick that makes this bearable is a script called ensure-app-secrets.ps1. Values left blank in the template (database passwords, OIDC secrets, API keys, encryption keys) are generated on first start and on every deploy. It only fills blank values, so it never overwrites anything.
Adding an app with five new secrets means adding five empty lines to the template. The next deploy fills them in. I haven't typed a password in months, and I don't miss it.
Two rules I learned the expensive way:
Some keys can never change. Grafana's secret key, n8n's encryption key, LiteLLM's salt key: they encrypt data already stored. "Rotate" one, and you get to meet your data again in its new, unreadable form.
Admin passwords are first-run only. Almost every app reads its ADMIN_PASSWORD exactly once: when it creates the account on an empty volume. Change .env later and the app politely ignores you. The handbook now has a table with the reset command for every app, because I needed every single one of them. Some twice.
Configuration is a script, or it didn't happen
Containers handle installation. They don't handle "connect Nextcloud to the office server", "create the Keycloak clients" or "set Moodle's sender address". In VM-land, that's an afternoon of clicking through admin consoles and a promise to document it later. (You won't. I didn't.)
Here, each of those is a configure-*.ps1 script that uses the app's API or CLI:
| Script | Does |
|---|---|
configure-keycloak.ps1 |
Realm, groups, every OIDC/SAML client, SMTP |
configure-app-auth.ps1 |
Tells each app to sign in through Keycloak |
configure-mail.ps1 |
Mail server, relay rules, every app's sender |
configure-zabbix-monitoring.ps1 |
Hosts, checks, alerting |
configure-proxy-hosts.ps1 |
Trusted domains and proxies |
There are about thirty. Every one of them is:
- Idempotent. Run it once or a hundred times, same result.
- Run on every start, after
docker compose up. - Self-documenting, through PowerShell's
Get-Help.
So the lab heals itself toward whatever is in Git. Change something in a web UI, and the next deploy quietly puts it back. That annoyed me exactly once. Now I'd fight anyone who tried to take it away.
One gotcha: some images reapply their own settings from environment variables in the entrypoint, on every start. Moodle's did that with its mail settings. My script set them, the nightly rebuild restarted Moodle, the entrypoint overwrote them, and Moodle went back to sending mail as someone else. If a setting "reverts by itself", read the image's entrypoint before you accuse your own script. Your script is probably innocent. This time.
Squeezing a company into 32 GB
Here's a confession: I'm slightly obsessed with resource usage. Every container is on probation, and every megabyte has to justify itself. Seventy containers, a local AI stack and a GitLab on a 32 GB desktop is a game of Tetris that never ends, and I'm weirdly happy playing it.
The moves that made the biggest difference:
- Memory limits from real data. Each limit is about twice what the container actually uses, measured, not guessed. Too generous wastes RAM; too tight, and you get the Grafana story in post 8.
- Two shared database servers instead of twenty-five private ones. Probably the single biggest saving in the lab.
- Taming the heavyweights. GitLab is capped at 4 GB: two Puma workers, Sidekiq concurrency 10, its built-in Prometheus switched off. ClickHouse runs a low-resource config. Cognee is limited to 10 AI calls a minute so the GPU isn't monopolised.
- Kernel tuning on every start. Docker Desktop resets its VM's settings on boot, so the start script sets
vm.swappiness=10(fewer pointless swap storms) andvm.max_map_count(SonarQube's Elasticsearch insists) every single time. - Retention with a budget. Logs and metrics keep 30 days, traces 31 days (about 1 GB), and each container's own log is capped at 20 MB × 5. Watchtower deletes superseded images. Nothing grows forever.
- Replacing fat with lean. Greenbone's ~57 GB of volumes became a scanner container measured in megabytes. Two database admin tools became one. Dremio's 5 GB of Java became Trino and Superset.
- The GPU stays on the host. Ollama runs natively on Windows, so the models get the GPU directly instead of fighting through another layer.
Optimising is never "done". Zabbix and Grafana tell me where the memory and CPU are going, and every few weeks something gets trimmed, tuned or replaced.
Adding a service, in five steps
- Add it to
docker-compose.yml, with its Traefik labels. - Add a DNS record to the zone file.
- Add its secrets as blank lines to
.env.example. - Add its Keycloak client and sign-in settings to the configure scripts.
- Push to
main.
Step 5 deploys it. No step involves RDP.
Next: how a request actually finds its way in, featuring four reverse proxies, a DNS server, a VPN and a tunnel.
Read next: Company in a Box, Part 3 of 11: Private by default, public by exception
Comments
Post a Comment