github.com/a4a881d4/docker@v1.9.0-rc2/api/client/commit.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/url"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	Cli "github.com/docker/docker/cli"
    10  	"github.com/docker/docker/opts"
    11  	flag "github.com/docker/docker/pkg/mflag"
    12  	"github.com/docker/docker/pkg/parsers"
    13  	"github.com/docker/docker/registry"
    14  	"github.com/docker/docker/runconfig"
    15  )
    16  
    17  // CmdCommit creates a new image from a container's changes.
    18  //
    19  // Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
    20  func (cli *DockerCli) CmdCommit(args ...string) error {
    21  	cmd := Cli.Subcmd("commit", []string{"CONTAINER [REPOSITORY[:TAG]]"}, Cli.DockerCommands["commit"].Description, true)
    22  	flPause := cmd.Bool([]string{"p", "-pause"}, true, "Pause container during commit")
    23  	flComment := cmd.String([]string{"m", "-message"}, "", "Commit message")
    24  	flAuthor := cmd.String([]string{"a", "#author", "-author"}, "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")")
    25  	flChanges := opts.NewListOpts(nil)
    26  	cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
    27  	// FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands.
    28  	flConfig := cmd.String([]string{"#run", "#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands")
    29  	cmd.Require(flag.Max, 2)
    30  	cmd.Require(flag.Min, 1)
    31  
    32  	cmd.ParseFlags(args, true)
    33  
    34  	var (
    35  		name            = cmd.Arg(0)
    36  		repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1))
    37  	)
    38  
    39  	//Check if the given image name can be resolved
    40  	if repository != "" {
    41  		if err := registry.ValidateRepositoryName(repository); err != nil {
    42  			return err
    43  		}
    44  	}
    45  
    46  	v := url.Values{}
    47  	v.Set("container", name)
    48  	v.Set("repo", repository)
    49  	v.Set("tag", tag)
    50  	v.Set("comment", *flComment)
    51  	v.Set("author", *flAuthor)
    52  	for _, change := range flChanges.GetAll() {
    53  		v.Add("changes", change)
    54  	}
    55  
    56  	if *flPause != true {
    57  		v.Set("pause", "0")
    58  	}
    59  
    60  	var (
    61  		config   *runconfig.Config
    62  		response types.ContainerCommitResponse
    63  	)
    64  
    65  	if *flConfig != "" {
    66  		config = &runconfig.Config{}
    67  		if err := json.Unmarshal([]byte(*flConfig), config); err != nil {
    68  			return err
    69  		}
    70  	}
    71  	serverResp, err := cli.call("POST", "/commit?"+v.Encode(), config, nil)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	defer serverResp.body.Close()
    77  
    78  	if err := json.NewDecoder(serverResp.body).Decode(&response); err != nil {
    79  		return err
    80  	}
    81  
    82  	fmt.Fprintln(cli.out, response.ID)
    83  	return nil
    84  }