2015-03-25 03:57:23 +00:00
package client
import (
2015-12-09 17:03:09 +00:00
"encoding/json"
2015-11-18 22:20:54 +00:00
"errors"
2015-03-25 03:57:23 +00:00
"fmt"
2016-03-16 19:19:22 +00:00
"golang.org/x/net/context"
2015-05-05 04:18:28 +00:00
Cli "github.com/docker/docker/cli"
2015-03-25 03:57:23 +00:00
"github.com/docker/docker/opts"
flag "github.com/docker/docker/pkg/mflag"
2015-12-04 21:55:15 +00:00
"github.com/docker/docker/reference"
2016-01-05 00:05:26 +00:00
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
2015-03-25 03:57:23 +00:00
)
2015-03-25 23:26:28 +00:00
// CmdCommit creates a new image from a container's changes.
2015-03-25 17:34:41 +00:00
//
2015-03-25 23:26:28 +00:00
// Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
2015-03-25 03:57:23 +00:00
func ( cli * DockerCli ) CmdCommit ( args ... string ) error {
2015-10-08 12:46:21 +00:00
cmd := Cli . Subcmd ( "commit" , [ ] string { "CONTAINER [REPOSITORY[:TAG]]" } , Cli . DockerCommands [ "commit" ] . Description , true )
2015-03-25 03:57:23 +00:00
flPause := cmd . Bool ( [ ] string { "p" , "-pause" } , true , "Pause container during commit" )
flComment := cmd . String ( [ ] string { "m" , "-message" } , "" , "Commit message" )
2015-11-09 14:37:24 +00:00
flAuthor := cmd . String ( [ ] string { "a" , "-author" } , "" , "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")" )
2015-03-25 03:57:23 +00:00
flChanges := opts . NewListOpts ( nil )
cmd . Var ( & flChanges , [ ] string { "c" , "-change" } , "Apply Dockerfile instruction to the created image" )
// FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands.
2015-11-09 14:37:24 +00:00
flConfig := cmd . String ( [ ] string { "#-run" } , "" , "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands" )
2015-03-25 03:57:23 +00:00
cmd . Require ( flag . Max , 2 )
cmd . Require ( flag . Min , 1 )
2015-07-03 09:26:09 +00:00
2015-03-29 01:22:46 +00:00
cmd . ParseFlags ( args , true )
2015-03-25 03:57:23 +00:00
var (
2015-11-18 22:20:54 +00:00
name = cmd . Arg ( 0 )
repositoryAndTag = cmd . Arg ( 1 )
repositoryName string
tag string
2015-03-25 03:57:23 +00:00
)
//Check if the given image name can be resolved
2015-11-18 22:20:54 +00:00
if repositoryAndTag != "" {
ref , err := reference . ParseNamed ( repositoryAndTag )
if err != nil {
2015-03-25 03:57:23 +00:00
return err
}
2015-11-18 22:20:54 +00:00
repositoryName = ref . Name ( )
switch x := ref . ( type ) {
2015-12-04 21:55:15 +00:00
case reference . Canonical :
2015-11-18 22:20:54 +00:00
return errors . New ( "cannot commit to digest reference" )
2015-12-04 21:55:15 +00:00
case reference . NamedTagged :
2015-11-18 22:20:54 +00:00
tag = x . Tag ( )
}
2015-03-25 03:57:23 +00:00
}
2015-12-18 18:36:17 +00:00
var config * container . Config
2015-12-09 17:03:09 +00:00
if * flConfig != "" {
2015-12-18 18:36:17 +00:00
config = & container . Config { }
2015-12-09 17:03:09 +00:00
if err := json . Unmarshal ( [ ] byte ( * flConfig ) , config ) ; err != nil {
return err
}
}
2015-12-04 22:02:06 +00:00
options := types . ContainerCommitOptions {
2015-12-03 04:55:07 +00:00
ContainerID : name ,
RepositoryName : repositoryName ,
Tag : tag ,
Comment : * flComment ,
Author : * flAuthor ,
Changes : flChanges . GetAll ( ) ,
Pause : * flPause ,
2015-12-09 17:03:09 +00:00
Config : config ,
2015-03-25 03:57:23 +00:00
}
2016-03-16 19:19:22 +00:00
response , err := cli . client . ContainerCommit ( context . Background ( ) , options )
2015-03-25 03:57:23 +00:00
if err != nil {
return err
}
2015-03-28 16:39:24 +00:00
fmt . Fprintln ( cli . out , response . ID )
2015-03-25 03:57:23 +00:00
return nil
}