container_commit.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package lib
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/runconfig"
  7. )
  8. // ContainerCommitOptions hods parameters to commit changes into a container.
  9. type ContainerCommitOptions struct {
  10. ContainerID string
  11. RepositoryName string
  12. Tag string
  13. Comment string
  14. Author string
  15. Changes []string
  16. Pause bool
  17. JSONConfig string
  18. }
  19. // ContainerCommit applies changes into a container and creates a new tagged image.
  20. func (cli *Client) ContainerCommit(options types.ContainerCommitOptions) (types.ContainerCommitResponse, error) {
  21. query := url.Values{}
  22. query.Set("container", options.ContainerID)
  23. query.Set("repo", options.RepositoryName)
  24. query.Set("tag", options.Tag)
  25. query.Set("comment", options.Comment)
  26. query.Set("author", options.Author)
  27. for _, change := range options.Changes {
  28. query.Add("changes", change)
  29. }
  30. if options.Pause != true {
  31. query.Set("pause", "0")
  32. }
  33. var (
  34. config *runconfig.Config
  35. response types.ContainerCommitResponse
  36. )
  37. if options.JSONConfig != "" {
  38. config = &runconfig.Config{}
  39. if err := json.Unmarshal([]byte(options.JSONConfig), config); err != nil {
  40. return response, err
  41. }
  42. }
  43. resp, err := cli.POST("/commit", query, config, nil)
  44. if err != nil {
  45. return response, err
  46. }
  47. defer resp.body.Close()
  48. if err := json.NewDecoder(resp.body).Decode(&response); err != nil {
  49. return response, err
  50. }
  51. return response, nil
  52. }