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