Docker swarm: enable unix socket
Fixes
@@ -88,16 +88,12 @@ type Discovery struct {
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
+ var err error
d := &Discovery{
port: conf.Port,
role: conf.Role,
}
- rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
- if err != nil {
- return nil, err
- }
-
// This is used in tests. In normal situations, it is set when Unmarshaling.
if conf.url == nil {
conf.url, err = url.Parse(conf.Host)
@@ -106,15 +102,29 @@ func NewDiscovery(conf *SDConfig, logger log.Logger) (*Discovery, error) {
}
}
- d.client, err = client.NewClientWithOpts(
+ opts := []client.Opt{
client.WithHost(conf.Host),
+ client.WithAPIVersionNegotiation(),
+ }
+
+ // There are other protocols that HTTP supported by the Docker daemon, like
+ // unix, which do not require an http client. Passing an http client to the
+ // docker client makes those non-http requests fail.
+ if conf.url.Scheme == "http" || conf.url.Scheme == "https" {
+ rt, err := config_util.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", false)
+ if err != nil {
+ return nil, err
+ }
+ opts = append(opts,
client.WithHTTPClient(&http.Client{
Transport: rt,
Timeout: time.Duration(conf.RefreshInterval),
}),
client.WithScheme(conf.url.Scheme),
- client.WithAPIVersionNegotiation(),
)
+ }
+
+ d.client, err = client.NewClientWithOpts(opts...)
if err != nil {
return nil, fmt.Errorf("error setting up docker swarm client: %w", err)
}
