mirror of https://github.com/docker/docs.git
Merge pull request #105 from aaronfeng/dockerclient-create-container
This commit is contained in:
commit
4652b01762
|
@ -107,21 +107,25 @@ func (b *dockerClientBackend) ls() ([]string, error) {
|
||||||
return names, nil
|
return names, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *dockerClientBackend) spawn(cmd ...string) (libswarm.Sender, error) {
|
func (b *dockerClientBackend) createContainer(cmd ...string) (libswarm.Sender, error) {
|
||||||
if len(cmd) != 1 {
|
if len(cmd) != 1 {
|
||||||
return nil, fmt.Errorf("dockerclient: spawn takes exactly 1 argument, got %d", len(cmd))
|
return nil, fmt.Errorf("dockerclient: spawn takes exactly 1 argument, got %d", len(cmd))
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := b.client.call("POST", "/containers/create", cmd[0])
|
resp, err := b.client.call("POST", "/containers/create", cmd[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
respBody, err := ioutil.ReadAll(resp.Body)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode != 201 {
|
|
||||||
|
if resp.StatusCode == 404 {
|
||||||
return nil, fmt.Errorf("expected status code 201, got %d:\n%s", resp.StatusCode, respBody)
|
return nil, fmt.Errorf("expected status code 201, got %d:\n%s", resp.StatusCode, respBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
var respJson struct{ Id string }
|
var respJson struct{ Id string }
|
||||||
if err = json.Unmarshal(respBody, &respJson); err != nil {
|
if err = json.Unmarshal(respBody, &respJson); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -129,6 +133,57 @@ func (b *dockerClientBackend) spawn(cmd ...string) (libswarm.Sender, error) {
|
||||||
return b.newContainer(respJson.Id), nil
|
return b.newContainer(respJson.Id), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *dockerClientBackend) createImage(cmd ...string) error {
|
||||||
|
if len(cmd) != 1 {
|
||||||
|
return fmt.Errorf("dockerclient: image name is the only expected argument, got %d", len(cmd))
|
||||||
|
}
|
||||||
|
|
||||||
|
var respJson struct{ Image string }
|
||||||
|
if err := json.Unmarshal([]byte(cmd[0]), &respJson); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
imageTag := strings.Split(respJson.Image, ":")
|
||||||
|
|
||||||
|
var tag = "latest"
|
||||||
|
image := imageTag[0]
|
||||||
|
|
||||||
|
if len(imageTag) > 1 {
|
||||||
|
tag = imageTag[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("/images/create?fromImage=%s&tag=%s", image, tag)
|
||||||
|
|
||||||
|
resp, err := b.client.call("POST", url, "")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *dockerClientBackend) spawn(cmd ...string) (libswarm.Sender, error) {
|
||||||
|
sender, err := b.createContainer(cmd...)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err = b.createImage(cmd...)
|
||||||
|
if err != nil {
|
||||||
|
return sender, err
|
||||||
|
}
|
||||||
|
sender, err = b.createContainer(cmd...)
|
||||||
|
if err != nil {
|
||||||
|
return sender, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sender, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *dockerClientBackend) newContainer(id string) libswarm.Sender {
|
func (b *dockerClientBackend) newContainer(id string) libswarm.Sender {
|
||||||
c := &container{backend: b, id: id}
|
c := &container{backend: b, id: id}
|
||||||
instance := libswarm.NewServer()
|
instance := libswarm.NewServer()
|
||||||
|
|
Loading…
Reference in New Issue