我想知道容器ID的生成方式,因此请提供执行docker run时提供容器ID的源代码?
这是docker守护程序创建容器的代码片段:
func (daemon *Daemon) newContainer(name string, config *runconfig.Config, imgID string) (*Container, error) { var ( id string err error ) id, name, err = daemon.generateIDAndName(name) if err != nil { return nil, err } … base := daemon.newBaseContainer(id) … base.ExecDriver = daemon.execDriver.Name() return &base, err }
因此,创建ID和名称的逻辑是在generateIDAndName函数中:
func (daemon *Daemon) generateIDAndName(name string) (string, string, error) { var ( err error id = stringid.GenerateNonCryptoID() ) if name == "" { if name, err = daemon.generateNewName(id); err != nil { return "", "", err } return id, name, nil } if name, err = daemon.reserveName(id, name); err != nil { return "", "", err } return id, name, nil }
这是字符串源,具体方法是使用false作为输入参数的generateID:
func generateID(crypto bool) string { b := make([]byte, 32) var r io.Reader = random.Reader if crypto { r = rand.Reader } for { if _, err := io.ReadFull(r, b); err != nil { panic(err) // This shouldn't happen } id := hex.EncodeToString(b) // if we try to parse the truncated for as an int and we don't have // an error then the value is all numberic and causes issues when // used as a hostname. ref #3869 if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil { continue } return id } }
正如你所看到的,该值是随机与此产生的随机
// Reader is a global, shared instance of a pseudorandom bytes generator. // It doesn't consume entropy. var Reader io.Reader = &reader{rnd: Rand}