Use environment to connect to Docker by default

Uses the environment definition for docker by default. Docker will
default to the unix/tcp socket if the environment is not set.
This commit is contained in:
Carlos Diaz-Padron 2015-09-28 16:54:32 -07:00 committed by Carlos Diaz-Padron
parent 8d67884f87
commit aaddc7bf6c
1 changed files with 20 additions and 8 deletions

View File

@ -39,9 +39,10 @@ func NewDockerDriver(ctx *DriverContext) Driver {
func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Initialize docker API client
dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock")
client, err := docker.NewClient(dockerEndpoint)
client, err := d.dockerClient()
if err != nil {
d.logger.Printf("[DEBUG] driver.docker: could not connect to docker daemon: %v", err)
return false, nil
}
@ -56,6 +57,7 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool
env, err := client.Version()
if err != nil {
d.logger.Printf("[DEBUG] driver.docker: could not read version from daemon: %v", err)
// Check the "no such file" error if the unix file is missing
if strings.Contains(err.Error(), "no such file") {
return false, nil
@ -195,10 +197,9 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
}
// Initialize docker API client
dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock")
client, err := docker.NewClient(dockerEndpoint)
client, err := d.dockerClient()
if err != nil {
return nil, fmt.Errorf("Failed to connect to docker.endpoint (%s): %s", dockerEndpoint, err)
return nil, fmt.Errorf("Failed to connect to docker.endpoint (%s): %s", client.Endpoint(), err)
}
repo, tag := docker.ParseRepositoryTag(image)
@ -292,10 +293,9 @@ func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, er
d.logger.Printf("[INFO] driver.docker: re-attaching to docker process: %s", handleID)
// Initialize docker API client
dockerEndpoint := d.config.ReadDefault("docker.endpoint", "unix:///var/run/docker.sock")
client, err := docker.NewClient(dockerEndpoint)
client, err := d.dockerClient()
if err != nil {
return nil, fmt.Errorf("Failed to connect to docker.endpoint (%s): %s", dockerEndpoint, err)
return nil, fmt.Errorf("Failed to connect to docker.endpoint (%s): %s", client.Endpoint(), err)
}
// Look for a running container with this ID
@ -333,6 +333,18 @@ func (d *DockerDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, er
return h, nil
}
// dockerClient returns a configured *docker.Client from the ClientConfig
func (d *DockerDriver) dockerClient() (*docker.Client, error) {
dockerEndpoint := d.config.Read("docker.endpoint")
if dockerEndpoint == "" {
client, err := docker.NewClientFromEnv()
return client, err
} else {
client, err := docker.NewClient(dockerEndpoint)
return client, err
}
}
func (h *dockerHandle) ID() string {
// Return a handle to the PID
pid := dockerPID{