Skip to content

About

Expose a read-only slice of the Docker Engine API (container list + logs) over the network, behind a bearer token and a path allowlist

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

docker-api-gateway

Expose a read-only slice of the Docker Engine API over the network, to a client that is not on the host.

Two endpoints are reachable and nothing else:

method path why
GET /containers/json container list — names, images, state
GET /containers/{id}/logs logs, including follow=1 streaming

Plus GET /healthz, unauthenticated, which returns ok and does not touch the Docker socket. The image also ships a HEALTHCHECK that probes it, so docker ps reports the container healthy or not without any extra wiring.

Why an allowlist and not just a token

Because GET /containers/{id}/json — plain Docker inspect — returns Config.Env.

On the container that motivated this, that is 157 environment variables including SERVER_PASSWORD and ADMIN_PASSWORD. A read-only socket proxy still exposes them, and so does any gateway that authenticates but proxies everything.

The container list carries no Env, which is what makes the two separable. Blocking inspect at the gateway means a leaked token still cannot reach those credentials. That property is asserted by tests, not just intended.

POST is rejected outright, so container lifecycle actions (restart, stop, kill) are unavailable by design. If you need those, this is the wrong tool.

Configuration

GATEWAY_TOKEN (env, required) bearer token; requests need Authorization: Bearer <token>
-socket Docker socket path (default /var/run/docker.sock)
-addr listen address (default :9375)
-tls-cert, -tls-key enable HTTPS; omit both to serve plaintext
-healthcheck probe /healthz on the local listener and exit non-zero on failure; what the image's HEALTHCHECK runs

About the healthcheck

The image is distroless, so there is no shell, curl or wget to write a conventional HEALTHCHECK against — the binary probes itself instead.

It detects http-vs-https rather than being told, because a HEALTHCHECK process does not inherit the flags the server was started with: -tls-cert reaches the entrypoint, not the healthcheck command. A probe that assumed plaintext would report every TLS deployment unhealthy forever while the gateway answered perfectly well. It also sends no token, so rotating GATEWAY_TOKEN cannot make a healthy container look unhealthy.

A non-default -addr is the one case that needs the HEALTHCHECK overridden in your compose file alongside it.

Without -tls-cert/-tls-key the bearer token crosses the network in cleartext on every request. Only do that on a network you would already trust with the logs.

The key pair is re-read on every handshake, so a renewal takes effect without a restart. This is deliberate: with the usual ListenAndServeTLS the pair is read once at startup, and a process would keep serving the old certificate after renewal — serving an expired cert for up to 90 days while every renewal reported success.

Example

services:
  gateway:
    image: ghcr.io/sharkops-io/docker-api-gateway:latest
    restart: unless-stopped
    ports:
      - "9375:9375"
    environment:
      GATEWAY_TOKEN: ${GATEWAY_TOKEN:?must be set}
    command:
      - -tls-cert=/etc/letsencrypt/live/example.com/fullchain.pem
      - -tls-key=/etc/letsencrypt/live/example.com/privkey.pem
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/etc/letsencrypt:ro

The :ro socket mount is defence in depth, not the control — a read-only mount still permits POST /containers/{id}/restart. The allowlist is what stops that.

Tests

go test ./...

The suite asserts the security boundary rather than the happy path: inspect is unreachable with a valid token, writes are refused, unknown paths 404, a token that is a prefix of the real one fails, a path traversal that normalises onto a blocked path does not smuggle through, /healthz does not proxy, and a renewed certificate is picked up without a restart.

License

Apache License 2.0. See LICENSE.

About

Expose a read-only slice of the Docker Engine API (container list + logs) over the network, behind a bearer token and a path allowlist

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages