github.com/nguyentm83/docker@v1.5.0/daemon/rename.go (about)

     1  package daemon
     2  
     3  import (
     4  	"github.com/docker/docker/engine"
     5  )
     6  
     7  func (daemon *Daemon) ContainerRename(job *engine.Job) engine.Status {
     8  	if len(job.Args) != 2 {
     9  		return job.Errorf("usage: %s OLD_NAME NEW_NAME", job.Name)
    10  	}
    11  	oldName := job.Args[0]
    12  	newName := job.Args[1]
    13  
    14  	container := daemon.Get(oldName)
    15  	if container == nil {
    16  		return job.Errorf("No such container: %s", oldName)
    17  	}
    18  
    19  	oldName = container.Name
    20  
    21  	container.Lock()
    22  	defer container.Unlock()
    23  	if _, err := daemon.reserveName(container.ID, newName); err != nil {
    24  		return job.Errorf("Error when allocating new name: %s", err)
    25  	}
    26  
    27  	container.Name = newName
    28  
    29  	if err := daemon.containerGraph.Delete(oldName); err != nil {
    30  		return job.Errorf("Failed to delete container %q: %v", oldName, err)
    31  	}
    32  
    33  	return engine.StatusOK
    34  }