Skip to content

Commit

Permalink
Merge pull request #2045 from hashicorp/b-docker-create-container
Browse files Browse the repository at this point in the history
Returning a container if it exists instead of creating a new one
  • Loading branch information
diptanu authored Nov 30, 2016
2 parents b7083f4 + 3f825bf commit a601256
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,22 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle

d.logger.Printf("[INFO] driver.docker: created container %s", container.ID)

// Start the container
err = client.StartContainer(container.ID, container.HostConfig)
if err != nil {
d.logger.Printf("[ERR] driver.docker: failed to start container %s: %s", container.ID, err)
pluginClient.Kill()
return nil, fmt.Errorf("Failed to start container %s: %s", container.ID, err)
// We don't need to start the container if the container is already running
// since we don't create containers which are already present on the host
// and are running
if !container.State.Running {
// Start the container
err = client.StartContainer(container.ID, container.HostConfig)
if err != nil {
d.logger.Printf("[ERR] driver.docker: failed to start container %s: %s", container.ID, err)
pluginClient.Kill()
return nil, fmt.Errorf("Failed to start container %s: %s", container.ID, err)
}
d.logger.Printf("[INFO] driver.docker: started container %s", container.ID)
} else {
d.logger.Printf("[DEBUG] driver.docker: re-attaching to container %s with status %q",
container.ID, container.State.String())
}
d.logger.Printf("[INFO] driver.docker: started container %s", container.ID)

// Return a driver handle
maxKill := d.DriverContext.config.MaxKillTimeout
Expand Down Expand Up @@ -992,8 +1000,19 @@ CREATE:
continue
}

// Inspect the container and if the container isn't dead then return
// the container
container, err := client.InspectContainer(container.ID)
if err != nil {
return nil, recoverable(fmt.Errorf("Failed to inspect container %s: %s", container.ID, err))
}
if container != nil && (container.State.Running || container.State.FinishedAt.IsZero()) {
return container, nil
}

err = client.RemoveContainer(docker.RemoveContainerOptions{
ID: container.ID,
ID: container.ID,
Force: true,
})
if err != nil {
d.logger.Printf("[ERR] driver.docker: failed to purge container %s", container.ID)
Expand Down

0 comments on commit a601256

Please sign in to comment.