Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the docker container id is generated

Tags:

docker

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?

like image 844
Sowndarya K Avatar asked Oct 08 '15 10:10

Sowndarya K


People also ask

How is docker container ID generated?

When creating a container, the docker daemon creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT . This is similar to docker run -d except the container is never started.

What is the container ID in docker?

CONTAINER ID is the container unique identifier. This identifier is the truncated version of a pretty long SHA-256 hash. IMAGE is the container image name and its tag separated by a colon, such as postgres:11. COMMAND is the command responsible for running the container.


1 Answers

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}
like image 199
Stanislav Avatar answered Oct 27 '22 12:10

Stanislav