mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
7ac4232e70
- Make the API client library completely standalone. - Move windows partition isolation detection to the client, so the driver doesn't use external types. Signed-off-by: David Calavera <david.calavera@gmail.com>
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/container"
|
|
)
|
|
|
|
type configWrapper struct {
|
|
*container.Config
|
|
HostConfig *container.HostConfig
|
|
}
|
|
|
|
// ContainerCreate creates a new container based in the given configuration.
|
|
// It can be associated with a name, but it's not mandatory.
|
|
func (cli *Client) ContainerCreate(config *container.Config, hostConfig *container.HostConfig, containerName string) (types.ContainerCreateResponse, error) {
|
|
var response types.ContainerCreateResponse
|
|
query := url.Values{}
|
|
if containerName != "" {
|
|
query.Set("name", containerName)
|
|
}
|
|
|
|
body := configWrapper{
|
|
Config: config,
|
|
HostConfig: hostConfig,
|
|
}
|
|
|
|
serverResp, err := cli.post("/containers/create", query, body, nil)
|
|
if err != nil {
|
|
if serverResp != nil && serverResp.statusCode == 404 && strings.Contains(err.Error(), config.Image) {
|
|
return response, imageNotFoundError{config.Image}
|
|
}
|
|
return response, err
|
|
}
|
|
|
|
if serverResp.statusCode == 404 && strings.Contains(err.Error(), config.Image) {
|
|
return response, imageNotFoundError{config.Image}
|
|
}
|
|
|
|
if err != nil {
|
|
return response, err
|
|
}
|
|
defer ensureReaderClosed(serverResp)
|
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&response); err != nil {
|
|
return response, err
|
|
}
|
|
|
|
return response, nil
|
|
}
|