docker-gen
Generate files from docker container meta-data
A Go program that watches your running Docker containers and regenerates config files from templates whenever containers start, stop, or change, most commonly used to auto-update an nginx reverse proxy without any manual editing.
docker-gen is a Go program that watches your Docker containers and automatically generates text files whenever containers start, stop, or change. You provide a template file written with Go's template syntax, and docker-gen fills it in with live data about your running containers: their names, IP addresses, exposed ports, environment variables, and more.
The most common use is generating a reverse proxy configuration. For example, when you start a new web application in a Docker container with an environment variable like VIRTUAL_HOST=example.com, docker-gen can detect that container, rebuild an nginx configuration file that includes that host name, and signal nginx to reload, all without any manual editing. This is how the popular nginx-proxy project works under the hood.
Other built-in templates cover centralized logging (sending container log output to fluentd or logstash), log rotation (automatically rotating the JSON log files Docker creates for each container), and service discovery (registering containers with external systems like etcd so other services can find them).
There are three ways to run docker-gen: directly on the server, bundled inside the same container as the application it configures, or as a separate standalone container that shares a volume with another container. The separate-container approach is common when you want to keep the Docker socket (which grants broad system access) away from a publicly exposed service.
The command accepts a template file and an optional output destination. A -watch flag keeps it running and regenerates files whenever container state changes. The -notify option lets you specify a command or signal to send to a container after each regeneration, which is how nginx gets told to reload its configuration.
Where it fits
- Auto-generate and reload an nginx reverse proxy config whenever a new container starts with a VIRTUAL_HOST environment variable
- Keep log rotation config up to date as containers are added or removed without manual edits
- Register containers with etcd or another service discovery system automatically on startup