diff --git a/README.md b/README.md index a42dfc762..da7d7d470 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Registrator assumes the default Docker socket at `file:///var/run/docker.sock` o By default, when registering a service, registrator will assign the service address by attempting to resolve the current hostname. If you would like to force the service address to be a specific address, you can specify the `-ip` argument. +If the argument `-internal` is passed, registrator will register the docker0 internal ip and port instead of the host mapped ones. (etcd only for now) + The consul backend does not support automatic expiry of stale registrations after some TTL. Instead, TTL checks must be configured (see below). For backends that do support TTL expiry, registrator can be started with the `-ttl` and `-ttl-refresh` arguments (both disabled by default). Registrator was designed to just be run as a container. You must pass the Docker socket file as a mount to `/tmp/docker.sock`, and it's a good idea to set the hostname to the machine host: diff --git a/bridge.go b/bridge.go index 568aaee87..ec16de35c 100644 --- a/bridge.go +++ b/bridge.go @@ -15,19 +15,22 @@ import ( type PublishedPort struct { HostPort string HostIP string + HostName string ExposedPort string + ExposedIP string PortType string Container *dockerapi.Container } type Service struct { - ID string - Name string - Port int - IP string - Tags []string - Attrs map[string]string - TTL int + ID string + Name string + // HostName string + Port int + IP string + Tags []string + Attrs map[string]string + TTL int pp PublishedPort } @@ -66,9 +69,16 @@ func NewService(port PublishedPort, isgroup bool) *Service { service.pp = port service.ID = hostname + ":" + container.Name[1:] + ":" + port.ExposedPort service.Name = mapdefault(metadata, "name", defaultName) - p, _ := strconv.Atoi(port.HostPort) + var p int + if *internal == true { + service.IP = port.ExposedIP + p, _ = strconv.Atoi(port.ExposedPort) + // service.HostName = port.HostName + } else { + service.IP = port.HostIP + p, _ = strconv.Atoi(port.HostPort) + } service.Port = p - service.IP = port.HostIP service.Tags = make([]string, 0) tags := mapdefault(metadata, "tags", "") @@ -134,24 +144,29 @@ func (b *RegistryBridge) Add(containerId string) { ports := make([]PublishedPort, 0) for port, published := range container.NetworkSettings.Ports { + var hp, hip string if len(published) > 0 { + hp = published[0].HostPort + hip = published[0].HostIp + } p := strings.Split(string(port), "/") ports = append(ports, PublishedPort{ - HostPort: published[0].HostPort, - HostIP: published[0].HostIp, + HostPort: hp, + HostIP: hip, + HostName: container.Config.Hostname, ExposedPort: p[0], + ExposedIP: container.NetworkSettings.IPAddress, PortType: p[1], Container: container, }) - } - } - - if len(ports) == 0 { - log.Println("registrator: ignored:", container.ID[:12], "no published ports") - return + // } } for _, port := range ports { + if *internal != true && port.HostPort == "" { + log.Println("registrator: ignored", container.ID[:12], "port", port.ExposedPort, "not published on host") + continue + } service := NewService(port, len(ports) > 1) if service == nil { log.Println("registrator: ignored:", container.ID[:12], "service on port", port.ExposedPort) @@ -167,6 +182,10 @@ func (b *RegistryBridge) Add(containerId string) { b.services[container.ID] = append(b.services[container.ID], service) log.Println("registrator: added:", container.ID[:12], service.ID) } + + if len(b.services) == 0 { + log.Println("registrator: ignored:", container.ID[:12], "no published ports") + } } func (b *RegistryBridge) Remove(containerId string) { diff --git a/registrator.go b/registrator.go index 0ab6108b8..750e9e88a 100644 --- a/registrator.go +++ b/registrator.go @@ -13,6 +13,7 @@ import ( ) var hostIp = flag.String("ip", "", "IP for ports mapped to the host") +var internal = flag.Bool("internal", false, "Use internal ports instead of published ones") var refreshInterval = flag.Int("ttl-refresh", 0, "Frequency with which service TTLs are refreshed") var refreshTtl = flag.Int("ttl", 0, "TTL for services (default is no expiry)") diff --git a/stage/registrator b/stage/registrator index 7ab5cd98d..e2b906f44 100755 Binary files a/stage/registrator and b/stage/registrator differ