github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/client/container_commit.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"net/url"
     8  
     9  	"github.com/docker/distribution/reference"
    10  	"github.com/docker/docker/api/types"
    11  )
    12  
    13  // ContainerCommit applies changes to a container and creates a new tagged image.
    14  func (cli *Client) ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error) {
    15  	var repository, tag string
    16  	if options.Reference != "" {
    17  		ref, err := reference.ParseNormalizedNamed(options.Reference)
    18  		if err != nil {
    19  			return types.IDResponse{}, err
    20  		}
    21  
    22  		if _, isCanonical := ref.(reference.Canonical); isCanonical {
    23  			return types.IDResponse{}, errors.New("refusing to create a tag with a digest reference")
    24  		}
    25  		ref = reference.TagNameOnly(ref)
    26  
    27  		if tagged, ok := ref.(reference.Tagged); ok {
    28  			tag = tagged.Tag()
    29  		}
    30  		repository = reference.FamiliarName(ref)
    31  	}
    32  
    33  	query := url.Values{}
    34  	query.Set("container", container)
    35  	query.Set("repo", repository)
    36  	query.Set("tag", tag)
    37  	query.Set("comment", options.Comment)
    38  	query.Set("author", options.Author)
    39  	for _, change := range options.Changes {
    40  		query.Add("changes", change)
    41  	}
    42  	if !options.Pause {
    43  		query.Set("pause", "0")
    44  	}
    45  
    46  	var response types.IDResponse
    47  	resp, err := cli.post(ctx, "/commit", query, options.Config, nil)
    48  	defer ensureReaderClosed(resp)
    49  	if err != nil {
    50  		return response, err
    51  	}
    52  
    53  	err = json.NewDecoder(resp.body).Decode(&response)
    54  	return response, err
    55  }