How ares-17/docker-awakening-gateway works
1. What this service is
- This repository builds a single Go binary that acts as a reverse proxy + container wake-up controller for Docker.
- It sits in front of application containers, matches requests by Host, and starts stopped containers on demand before proxying traffic through.
2. Top-level runtime shape
- main.go is the entrypoint.
- The core implementation lives in the gateway/ package, which is where routing, Docker control, discovery, auth, metrics, and scheduling are coordinated.
- The shipped image is intentionally minimal/distroless, so the operational boundary is basically “one gateway process talking to Docker and HTTP clients.”
3. Request path / user-facing flow
- Incoming HTTP requests hit the gateway first.
- If the target container is already running, the gateway proxies directly to it.
- If the container is stopped, the gateway returns a loading page (gateway/templates/loading.html), starts the container asynchronously, probes readiness, and then lets the browser continue to the real app.
- The docs also show live log output on the loading page, so there is an observable bootstrapping experience, not just a blind spinner.
4. Routing and container model
- gateway/server.go and gateway/manager.go appear to be the main request-routing and orchestration layers.
- Routing is host-based: each container definition has a host that maps to a Host header.
- The system supports both single containers and groups (gateway/group.go) with a strategy such as round-robin, so the routing model is not one-container-per-host only.
5. Configuration sources
- The gateway accepts configuration from a static YAML file (config.yaml) and from Docker labels for auto-discovery.
- The README and gateway/config.go imply these sources are merged into one runtime config model.
- Hot reload is supported via SIGHUP, so config changes can be applied without restarting the gateway.
6. Docker control plane
- gateway/docker.go is the boundary to the Docker Engine API.
- This layer is responsible for inspecting container state, starting containers, stopping idle containers, and likely resolving container networking details.
- gateway/discovery.go periodically polls Docker to find label-enabled containers, so discovery is a control-plane loop separate from request handling.
7. Wake-up orchestration and readiness
- gateway/scheduler.go manages the timing aspects of wake and idle behavior.
- A wake sequence starts the container, then performs readiness checks using either TCP dial or an HTTP health path.
- There is also an idle timeout mechanism that stops containers after inactivity, which is part of the same orchestration boundary rather than the app containers themselves.
8. Dependencies and grouped startup
- Containers can declare depends_on, and the gateway coordinates startup order so that prerequisite containers are running before dependents are served.
- This creates a small internal dependency graph in the gateway, which matters for architecture diagrams because “wake request” may fan out into multiple Docker starts.
9. Admin and observability surfaces
- The gateway exposes admin endpoints under /_status, /_status/api, and /_status/wake, plus /_metrics for Prometheus.
- gateway/admin_auth.go adds optional protection for these endpoints using none/basic/bearer auth.
- gateway/metrics.go emits request, start, latency, and idle-stop metrics, so observability is built into the gateway rather than added externally.
10. Security and network boundary
- The gateway is designed to run behind a TLS-terminating proxy in production; auth itself is HTTP-level and not sufficient alone.
- trusted_proxies in config suggests the server is aware of forwarded client IPs and proxy chains.
- gateway/security_test.go indicates security-sensitive handling around these boundaries.
11. External dependencies
- The main external runtime dependency is Docker, via the local Docker socket or Docker API access.
- Optional integration points include Prometheus scraping and a fronting reverse proxy like Nginx/Caddy/Traefik.
- Everything else is self-contained in the gateway process; there is no separate database or message broker in the repo.
12. Architectural diagram hint
- A good diagram would show three planes:
- Client plane: browser/Prometheus/admin tools
- Gateway control plane: config, discovery, scheduler, auth, metrics, manager
- Docker/app plane: Docker daemon plus managed containers/groups
- The key data flow is: request arrives → gateway resolves host → if sleeping, gateway starts container and probes readiness → gateway proxies to the app → idle watcher may later stop it.
Main entry points: server.go HTTP server · config.go Config · manager.go Manager · scheduler.go Scheduler
Components 17
Client plane
Browsers, operators, and scrapers that talk to the gateway
- Browser · End-user HTTP client reaching host-based routes
- Admin tool · Operator using status and wake endpoints
- Prometheus · Scrapes gateway metrics
Gateway control plane
Single Go process that resolves hosts, wakes containers, and proxies traffic
- Main · Bootstraps the gateway process
main.go - HTTP server · Handles incoming requests, host routing, proxying, and admin endpoints
gateway/server.go - Manager · Coordinates request handling, wake-up flow, and container lifecycle decisions
gateway/manager.go - Scheduler · Runs wake sequencing, readiness probing, and idle-stop timing
gateway/scheduler.go - Discovery · Polls Docker for label-based container discovery
gateway/discovery.go - Config · Merges YAML and Docker-label configuration into one model
gateway/config.go - Groups · Models grouped containers and strategies such as round-robin
gateway/group.go - Admin auth · Protects status and wake admin endpoints with optional auth
gateway/admin_auth.go - Metrics · Emits request, wake, latency, and idle-stop metrics
gateway/metrics.go - Loading page · Renders the wait experience while containers wake up
gateway/templates/loading.html - Status UI · Renders admin/status views for operational introspection
gateway/templates/status.html
Docker/app plane
Docker Engine and managed application containers
- Docker API · Boundary to the Docker Engine for inspect/start/stop and networking details
gateway/docker.go - App containers · Application containers and groups started, probed, and stopped on demand
- Docker daemon · Underlying Docker Engine accessed by the gateway
Connections 25
- Browser → HTTP server: HTTP request · User traffic reaches the gateway
- Admin tool → HTTP server: admin/status · Operator calls status and wake endpoints
- Prometheus → Metrics: scrape · Metrics are scraped over HTTP
- Main → Config: load · Process startup reads runtime configuration
- Main → HTTP server: start · Entrypoint launches the HTTP gateway
- HTTP server → Admin auth: protect · Admin endpoints may require authentication
- HTTP server → Manager: route · Request handling delegates orchestration decisions
- HTTP server → Loading page: render · Sleeping targets return the loading experience
- HTTP server → Status UI: render · Status endpoints serve operational views
- HTTP server → Metrics: emit · HTTP activity contributes to metrics
- Manager → Scheduler: orchestrate · Manager hands wake and idle timing to the scheduler
- Manager → Docker API: control · Manager uses Docker operations to inspect and change state
- Manager → Groups: resolve · Grouped backends are resolved for routing and wake order
- Manager → Discovery: sync · Configured and discovered containers are reconciled
- Discovery → Docker API: poll · Discovery reads container metadata from Docker
- Scheduler → Docker API: start/stop · Wake sequences and idle stops act on Docker containers
- Scheduler → App containers: probe · Readiness checks validate application availability
- Docker API → Docker daemon: api · Adapter talks to the Docker Engine
- Docker daemon → App containers: manage · Docker runtime hosts the application workloads
- Config → HTTP server: configure · Routing, proxy, and proxy-chain behavior are configured here
- Config → Manager: configure · Wake, dependency, and grouping settings feed orchestration
- Config → Discovery: configure · Discovery relies on label and polling settings
- Config → Scheduler: configure · Idle and readiness timing come from config
- Config → Admin auth: configure · Admin authentication mode is selected from config
- Config → Metrics: configure · Metrics exposure and labels are configured