container_create.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package lib
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "strings"
  6. "github.com/docker/docker/api/types"
  7. "github.com/docker/docker/runconfig"
  8. )
  9. // ContainerCreate creates a new container based in the given configuration.
  10. // It can be associated with a name, but it's not mandatory.
  11. func (cli *Client) ContainerCreate(config *runconfig.ContainerConfigWrapper, containerName string) (types.ContainerCreateResponse, error) {
  12. var (
  13. query url.Values
  14. response types.ContainerCreateResponse
  15. )
  16. if containerName != "" {
  17. query.Set("name", containerName)
  18. }
  19. serverResp, err := cli.POST("/containers/create", query, config, nil)
  20. if err != nil {
  21. if serverResp != nil && serverResp.statusCode == 404 && strings.Contains(err.Error(), config.Image) {
  22. return response, imageNotFoundError{config.Image}
  23. }
  24. return response, err
  25. }
  26. if serverResp.statusCode == 404 && strings.Contains(err.Error(), config.Image) {
  27. }
  28. if err != nil {
  29. return response, err
  30. }
  31. if err := json.NewDecoder(serverResp.body).Decode(&response); err != nil {
  32. return response, err
  33. }
  34. return response, nil
  35. }