How the docker container id is generated

前端 未结 1 1903
走了就别回头了
走了就别回头了 2021-01-12 05:26

I wanted to know how the container id is generated so please provide the source code that provides the container id when the docker run is executed?

相关标签:
1条回答
  • 2021-01-12 06:11

    Here is a code snippet from docker daemon's function for creating Containers:

    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
    }
    

    So, the logic of creating ID and Name is in generateIDAndName function:

    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
    }
    

    Here is stringid sources and the concrete method is generateID with false as input parameter:

    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
        }
    }
    

    As you can see, the value is randomly generated with this random

    // Reader is a global, shared instance of a pseudorandom bytes generator.
    // It doesn't consume entropy.
    var Reader io.Reader = &reader{rnd: Rand}
    
    0 讨论(0)
提交回复
热议问题