MCPcopy
hub / github.com/moby/moby / GetContainer

Method GetContainer

daemon/container.go:38–70  ·  view source on GitHub ↗

GetContainer looks for a container using the provided information, which could be one of the following inputs from the caller: - A full container ID, which will exact match a container in daemon's list - A container name, which will only exact match via the GetByName() function - A partial container

(prefixOrName string)

Source from the content-addressed store, hash-verified

36// unique enough to only return a single container object
37// If none of these searches succeed, an error is returned
38func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
39 if prefixOrName == "" {
40 return nil, errors.WithStack(invalidIdentifier(prefixOrName))
41 }
42
43 if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
44 // prefix is an exact match to a full container ID
45 return containerByID, nil
46 }
47
48 // GetByName will match only an exact name provided; we ignore errors
49 if containerByName, _ := daemon.GetByName(prefixOrName); containerByName != nil {
50 // prefix is an exact match to a full container Name
51 return containerByName, nil
52 }
53
54 containerID, err := daemon.containersReplica.GetByPrefix(prefixOrName)
55 if err != nil {
56 return nil, err
57 }
58 ctr := daemon.containers.Get(containerID)
59 if ctr == nil {
60 // Updates to the daemon.containersReplica ViewDB are not atomic
61 // or consistent w.r.t. the live daemon.containers Store so
62 // while reaching this code path may be indicative of a bug,
63 // it is not _necessarily_ the case.
64 log.G(context.TODO()).WithField("prefixOrName", prefixOrName).
65 WithField("id", containerID).
66 Debugf("daemon.GetContainer: container is known to daemon.containersReplica but not daemon.containers")
67 return nil, containerNotFound(prefixOrName)
68 }
69 return ctr, nil
70}
71
72// Load reads the contents of a container from disk
73// This is typically done at startup.

Callers 15

ContainerLogsMethod · 0.95
ContainerStopMethod · 0.95
ContainerRenameMethod · 0.95
ContainerExportMethod · 0.95
ContainerStatPathMethod · 0.95
ContainerArchivePathMethod · 0.95
ContainerExtractToDirMethod · 0.95
ContainerStartMethod · 0.95
ContainerAttachMethod · 0.95
ContainerAttachRawMethod · 0.95
ContainerInspectMethod · 0.95
ContainerRestartMethod · 0.95

Calls 6

GetByNameMethod · 0.95
invalidIdentifierTypeAlias · 0.85
containerNotFoundFunction · 0.85
GetByPrefixMethod · 0.80
DebugfMethod · 0.80
GetMethod · 0.65

Tested by 1

TestGetContainerFunction · 0.76