Back to personal projects

Personal project. Running since [YEAR].

Home lab

A custom PC and a Synology NAS that handle my storage, backups and self-hosting. Cinema Club DC runs on the NAS in Docker and reaches the internet through a Cloudflare Tunnel. Tailscale connects my devices when I'm away from home, and Homebridge runs the home automation.

5
Docker containers run cinemaclubdc.com on the NAS
0
ports forwarded on my router for the site, because of the Cloudflare Tunnel
6 hrs
between full scrapes of every theater, plus a 30-minute check on one that posts films early
Hardware
[NAS MODEL] with [DRIVES AND RAID TYPE]. A custom PC with [CPU, GPU, RAM].
Software
Synology DSM, Container Manager, Docker Compose, Cloudflare Tunnel, Unbound, Tailscale, Homebridge
Code
Cinema Club DC on GitHub, including the deploy guide

How it fits together

Three kinds of traffic reach the NAS, and none of them need an open port. Pick one to follow it through.

Synology NAS ([NAS MODEL])VisitorDiscordMy devicesCloudflareTailscalecloudflaredfrontendbotbackenddnsSQLiteDSMCustom PCAny browserThe club's serverAway from homeDNS, HTTPS, cachePrivate meshThe tunnelReact on nginxDiscord botFlask, scrapersCaching resolverDocker volumeAdmin and files[PC SPECS]
  1. 1. Cloudflare
    DNS, HTTPS, cache

    cinemaclubdc.com points at Cloudflare, which handles HTTPS and caches the frontend's files.

  2. 2. The tunnel
    cloudflared container

    cloudflared keeps an outbound connection open to Cloudflare, so requests reach the NAS with no open ports. On Synology it has to run over HTTP/2.

  3. 3. nginx
    frontend container

    Serves the React build and passes anything under /api to the backend.

  4. 4. Flask
    backend container

    Gunicorn runs the app. The data is one SQLite file on a Docker volume, so rebuilding the containers never touches it.

Simplified. Internal addresses and admin settings are left out on purpose.

Running Cinema Club DC on a NAS

The site is one Docker Compose file. Three containers are the app itself: a Flask backend under Gunicorn that also runs the theater scrapers, the React frontend on nginx, and the Discord bot. A fourth is a small caching DNS resolver, and a fifth runs cloudflared for the tunnel.

DSM's Task Scheduler runs the scrapers. Every theater is checked every 6 hours, and one is checked every 30 minutes because it posts films a week before tickets go on sale, and members want the on-sale alert quickly. To ship an update I pull the code on the NAS, rebuild, and clear Cloudflare's cache so visitors get the new frontend.

ContainerWhat it does
frontendThe React build on nginx. Serves the site and passes API calls to the backend.
backendFlask under Gunicorn. The API, the theater scrapers and the SQLite database.
botThe Discord bot. Slash commands and schedule posts.
dnsAn Unbound caching resolver, added to fix failed lookups (below).
cloudflaredThe Cloudflare Tunnel. Connects out to Cloudflare so nothing has to connect in.
# Shipping an update
ssh nas
cd /volume1/docker/cinema-club-dc
git pull
sudo docker-compose up -d --build

# DSM Task Scheduler, every 6 hours: every theater
docker exec cinemaclub-backend python scraper.py
# every 30 minutes: the theater that posts early
docker exec cinemaclub-backend python scraper.py --only suns

Problems I had to fix

Lookups failing inside Docker

On the NAS, Docker's built-in DNS forwarder failed about 1 lookup in 5. That broke sign-in emails and calls to the movie data APIs (TMDB and OMDb). Pointing the containers straight at the router helped until the router started failing about 1 in 6 as well.

So I added a small Unbound container that asks the router and caches the answers, and serves a cached answer when the router doesn't respond. It has a fixed address on its own Docker network that the backend and bot join, and the backend retries any lookup for about 3 seconds before giving up. A diagnostic script counts failures so I can tell which layer is at fault.

# docker-compose.yml (excerpt): the caching resolver
dns:
  build:
    context: .
    dockerfile: Dockerfile.dns   # alpine + unbound
  environment:
    - DNS_UPSTREAM=<router>
  networks:
    lookups:
      ipv4_address: <fixed address>
  restart: unless-stopped

The tunnel on Synology

cloudflared connects over QUIC by default, and on Synology that fails with a socket error. Running the tunnel over HTTP/2 fixed it. The tunnel container joins the site's Docker network so it can reach the frontend by name.

Slow first replies after quiet hours

Discord gives a bot 3 seconds to answer a slash command. After a quiet spell the bot sometimes missed that, because the NAS had spun its drives down and waking them took longer than Discord would wait. Turning off drive hibernation fixed it, and the bot now logs where every slow reply spent its time.

Backups

The site's database is one SQLite file on a Docker volume. The backend saves a copy before any migration that rebuilds a table, and I take my own copies with SQLite's VACUUM INTO while the site is running. [HOW THE NAS ITSELF IS BACKED UP, FOR EXAMPLE HYPER BACKUP TO AN EXTERNAL DRIVE OR CLOUD STORAGE.]

Tailscale and Homebridge

Tailscale puts [MY DEVICES] on one private WireGuard network, so I can reach the NAS's admin page, my files and the PC from anywhere without opening ports. Homebridge runs [WHERE IT RUNS] and brings [ACCESSORIES] into Apple Home.