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)
| 36 | // unique enough to only return a single container object |
| 37 | // If none of these searches succeed, an error is returned |
| 38 | func (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. |