How the docker container id is generated

2020-03-15 05:56发布

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?

标签: docker
1条回答
萌系小妹纸
2楼-- · 2020-03-15 06:32

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}
查看更多
登录 后发表回答