func (c *Container) renameOld(client *dockerClient.Client, opts *dockerClient.CreateContainerOptions) error { if len(opts.Name) == 0 { return nil } existing, err := inspect(client, opts.Name) if _, ok := err.(*dockerClient.NoSuchContainer); ok { return nil } if err != nil { return nil } if c.Container != nil && existing.ID == c.Container.ID { return nil } var newName string if label, ok := existing.Config.Labels[config.HASH]; ok { newName = fmt.Sprintf("%s-%s", existing.Name, label) } else { newName = fmt.Sprintf("%s-unknown-%s", existing.Name, util.RandSeq(12)) } if existing.State.Running { err := client.StopContainer(existing.ID, 2) if err != nil { return err } _, err = client.WaitContainer(existing.ID) if err != nil { return err } } log.Debugf("Renaming %s to %s", existing.Name, newName) return client.RenameContainer(dockerClient.RenameContainerOptions{ID: existing.ID, Name: newName}) }