diff --git a/docs/user/run.md b/docs/user/run.md index 86f10a059..c7aa891dd 100644 --- a/docs/user/run.md +++ b/docs/user/run.md @@ -35,6 +35,8 @@ Option | Description ------ | ----------- `-internal` | Use exposed ports instead of published ports `-ip ` | Force IP address used for registering services +`-retry-attempts` | Max retry attempts to establish a connection with the backend +`-retry-interval` | Interval (in millisecond) between retry-attempts `-tags ` | Force comma-separated tags on all registered services `-deregister ` | Deregister existed services "always" or "on-success". Default: always `-ttl ` | TTL for services. Default: 0, no expiry (supported backends only) @@ -52,6 +54,8 @@ argument. For registry backends that support TTL expiry, Registrator can both set and refresh service TTLs with `-ttl` and `-ttl-refresh`. +If you want unlimited retry-attempts use `-retry-attempts -1`. + The `-resync` options controls how often Registrator will query Docker for all containers and reregister all services. This allows Registrator and the service registry to get back in sync if they fall out of sync. diff --git a/registrator.go b/registrator.go index 995d443c3..95702a727 100644 --- a/registrator.go +++ b/registrator.go @@ -23,6 +23,8 @@ var refreshTtl = flag.Int("ttl", 0, "TTL for services (default is no expiry)") var forceTags = flag.String("tags", "", "Append tags for all registered services") var resyncInterval = flag.Int("resync", 0, "Frequency with which services are resynchronized") var deregister = flag.String("deregister", "always", "Deregister exited services \"always\" or \"on-success\"") +var retryAttempts = flag.Int("retry-attempts", 0, "Max retry attempts to establish a connection with the backend. Use -1 for infinite retries") +var retryInterval = flag.Int("retry-interval", 2000, "Interval (in millisecond) between retry-attempts.") func getopt(name, def string) string { if env := os.Getenv(name); env != "" { @@ -49,12 +51,17 @@ func main() { if *hostIp != "" { log.Println("Forcing host IP to", *hostIp) } + if (*refreshTtl == 0 && *refreshInterval > 0) || (*refreshTtl > 0 && *refreshInterval == 0) { assert(errors.New("-ttl and -ttl-refresh must be specified together or not at all")) } else if *refreshTtl > 0 && *refreshTtl <= *refreshInterval { assert(errors.New("-ttl must be greater than -ttl-refresh")) } + if *retryInterval <= 0 { + assert(errors.New("-retry-interval must be greater than 0")) + } + docker, err := dockerapi.NewClient(getopt("DOCKER_HOST", "unix:///tmp/docker.sock")) assert(err) @@ -72,7 +79,23 @@ func main() { }) assert(err) - assert(b.Ping()) + + attempt := 0 + for *retryAttempts == -1 || attempt <= *retryAttempts { + log.Printf("Connecting to backend (%v/%v)", attempt, *retryAttempts) + + err = b.Ping() + if err == nil { + break + } + + if err != nil && attempt == *retryAttempts { + assert(err) + } + + time.Sleep(time.Duration(*retryInterval) * time.Millisecond) + attempt++ + } // Start event listener before listing containers to avoid missing anything events := make(chan *dockerapi.APIEvents)