2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type commitOptions struct {
|
|
|
|
container string
|
|
|
|
reference string
|
|
|
|
|
|
|
|
pause bool
|
|
|
|
comment string
|
|
|
|
author string
|
2017-05-15 08:45:19 -04:00
|
|
|
changes opts.ListOpts
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommitCommand creates a new cobra.Command for `docker commit`
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewCommitCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
var options commitOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]",
|
|
|
|
Short: "Create a new image from a container's changes",
|
|
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.container = args[0]
|
2016-09-08 13:11:39 -04:00
|
|
|
if len(args) > 1 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.reference = args[1]
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-15 08:45:19 -04:00
|
|
|
return runCommit(dockerCli, &options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SetInterspersed(false)
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.pause, "pause", "p", true, "Pause container during commit")
|
|
|
|
flags.StringVarP(&options.comment, "message", "m", "", "Commit message")
|
|
|
|
flags.StringVarP(&options.author, "author", "a", "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
options.changes = opts.NewListOpts(nil)
|
|
|
|
flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runCommit(dockerCli command.Cli, options *commitOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
name := options.container
|
|
|
|
reference := options.reference
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
commitOptions := types.ContainerCommitOptions{
|
2016-09-08 13:11:39 -04:00
|
|
|
Reference: reference,
|
2017-05-15 08:45:19 -04:00
|
|
|
Comment: options.comment,
|
|
|
|
Author: options.author,
|
|
|
|
Changes: options.changes.GetAll(),
|
|
|
|
Pause: options.pause,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
response, err := dockerCli.Client().ContainerCommit(ctx, name, commitOptions)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), response.ID)
|
|
|
|
return nil
|
|
|
|
}
|