container_create.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package client
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "strings"
  6. "github.com/docker/engine-api/types"
  7. "github.com/docker/engine-api/types/container"
  8. "github.com/docker/engine-api/types/network"
  9. "golang.org/x/net/context"
  10. )
  11. type configWrapper struct {
  12. *container.Config
  13. HostConfig *container.HostConfig
  14. NetworkingConfig *network.NetworkingConfig
  15. }
  16. // ContainerCreate creates a new container based in the given configuration.
  17. // It can be associated with a name, but it's not mandatory.
  18. func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (types.ContainerCreateResponse, error) {
  19. var response types.ContainerCreateResponse
  20. query := url.Values{}
  21. if containerName != "" {
  22. query.Set("name", containerName)
  23. }
  24. body := configWrapper{
  25. Config: config,
  26. HostConfig: hostConfig,
  27. NetworkingConfig: networkingConfig,
  28. }
  29. serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
  30. if err != nil {
  31. if serverResp != nil && serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
  32. return response, imageNotFoundError{config.Image}
  33. }
  34. return response, err
  35. }
  36. err = json.NewDecoder(serverResp.body).Decode(&response)
  37. ensureReaderClosed(serverResp)
  38. return response, err
  39. }