Johannes
Johannes

Reputation: 6717

How to specify a comment for the detailed info in docker

I am asking here how to write a comment in a dockerfile. So I need to be clear that I am not talking about writing

#This is a comment

into a dockerfile.

What I am looking for is how to set the comment field in the detailed description.

docker inspect ubuntu -f {{.Comment}}
docker inspect ubuntu

with either of these two commands you can see a comment entry in the detailed description. The current ubuntu container has an empty comment.

I checked the builder reference and did not find anything about it. I googled and stack-overflowed - unfortunately the keyword "comment" leads the search towards how to use #comment which is not what I am after.

My question is how can I populate a comment field in the detailed description?

Upvotes: 2

Views: 1318

Answers (2)

BMitch
BMitch

Reputation: 264761

Typically this is saved for the underlying tools to manage. It's part of the layer history spec:

// History describes the history of a layer.
type History struct {
    // Created is the combined date and time at which the layer was created, formatted as defined by RFC 3339, section 5.6.
    Created *time.Time `json:"created,omitempty"`

    // CreatedBy is the command which created the layer.
    CreatedBy string `json:"created_by,omitempty"`

    // Author is the author of the build point.
    Author string `json:"author,omitempty"`

    // Comment is a custom message set when creating the layer.
    Comment string `json:"comment,omitempty"`

    // EmptyLayer is used to mark if the history item created a filesystem diff.
    EmptyLayer bool `json:"empty_layer,omitempty"`
}

You'll see that layer history in the docker image history, and the docker image inspect appears to pull the value from the last layer.

Some tools like buildkit it gets set to a hardcoded value:

if len(diffs) > historyLayers {
    // some history items are missing. add them based on the ref metadata
    for _, md := range refMeta[historyLayers:] {
        history = append(history, ocispec.History{
            Created:   &md.createdAt,
            CreatedBy: md.description,
            Comment:   "buildkit.exporter.image.v0",
        })
    }
}

With a Dockerfile built by the classic docker build command, I'm not aware of any way to set this Comment field on the individual steps.

As mentioned by Rajesh, you can set the comment with the docker commit command which I don't recommend using for any images going to production since the image needs to be manually created in an error prone method.


Instead, the typical way to add metadata to a docker image is to set a label in the Dockefile.

LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

These labels are also copied into the container metadata when it's spawned from that image, and they are inherited from the parent image (set in your FROM line) so be sure to replace the label in child images to avoid confusion.

Upvotes: 1

rajesh-nitc
rajesh-nitc

Reputation: 5549

Use docker commit command. See docs here:

$ docker pull nginx
$ docker image inspect -f {{.Comment}} nginx

$ docker run -d --name mycontainer nginx
$ docker commit -m "my comment" mycontainer nginx
$ docker image inspect -f {{.Comment}} nginx
my comment

Upvotes: 2

Related Questions