2016-01-05 02:26:13 +03:00
|
|
|
package client
|
2015-12-03 22:14:36 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2016-01-05 02:26:13 +03:00
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/docker/engine-api/types/container"
|
2016-01-07 22:55:51 +03:00
|
|
|
"github.com/docker/engine-api/types/network"
|
2016-03-08 07:38:46 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-12-03 22:14:36 +03:00
|
|
|
)
|
|
|
|
|
2015-12-18 21:36:17 +03:00
|
|
|
type configWrapper struct {
|
|
|
|
*container.Config
|
2016-01-07 22:55:51 +03:00
|
|
|
HostConfig *container.HostConfig
|
|
|
|
NetworkingConfig *network.NetworkingConfig
|
2015-12-18 21:36:17 +03:00
|
|
|
}
|
|
|
|
|
2015-12-03 22:14:36 +03:00
|
|
|
// ContainerCreate creates a new container based in the given configuration.
|
|
|
|
// It can be associated with a name, but it's not mandatory.
|
2016-03-08 07:38:46 +03:00
|
|
|
func (cli *Client) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (types.ContainerCreateResponse, error) {
|
2015-12-05 05:06:12 +03:00
|
|
|
var response types.ContainerCreateResponse
|
|
|
|
query := url.Values{}
|
2015-12-03 22:14:36 +03:00
|
|
|
if containerName != "" {
|
|
|
|
query.Set("name", containerName)
|
|
|
|
}
|
|
|
|
|
2015-12-18 21:36:17 +03:00
|
|
|
body := configWrapper{
|
2016-01-07 22:55:51 +03:00
|
|
|
Config: config,
|
|
|
|
HostConfig: hostConfig,
|
|
|
|
NetworkingConfig: networkingConfig,
|
2015-12-18 21:36:17 +03:00
|
|
|
}
|
|
|
|
|
2016-03-08 07:38:46 +03:00
|
|
|
serverResp, err := cli.post(ctx, "/containers/create", query, body, nil)
|
2015-12-03 22:14:36 +03:00
|
|
|
if err != nil {
|
2016-01-11 23:05:28 +03:00
|
|
|
if serverResp != nil && serverResp.statusCode == 404 && strings.Contains(err.Error(), "No such image") {
|
2015-12-03 22:14:36 +03:00
|
|
|
return response, imageNotFoundError{config.Image}
|
|
|
|
}
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2016-01-25 09:18:56 +03:00
|
|
|
err = json.NewDecoder(serverResp.body).Decode(&response)
|
|
|
|
ensureReaderClosed(serverResp)
|
|
|
|
return response, err
|
2015-12-03 22:14:36 +03:00
|
|
|
}
|