github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/client/container_exec.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	"golang.org/x/net/context"
     8  )
     9  
    10  // ContainerExecCreate creates a new exec configuration to run an exec process.
    11  func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.ContainerExecCreateResponse, error) {
    12  	var response types.ContainerExecCreateResponse
    13  	resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
    14  	if err != nil {
    15  		return response, err
    16  	}
    17  	err = json.NewDecoder(resp.body).Decode(&response)
    18  	ensureReaderClosed(resp)
    19  	return response, err
    20  }
    21  
    22  // ContainerExecStart starts an exec process already created in the docker host.
    23  func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
    24  	resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
    25  	ensureReaderClosed(resp)
    26  	return err
    27  }
    28  
    29  // ContainerExecAttach attaches a connection to an exec process in the server.
    30  // It returns a types.HijackedConnection with the hijacked connection
    31  // and the a reader to get output. It's up to the called to close
    32  // the hijacked connection by calling types.HijackedResponse.Close.
    33  func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error) {
    34  	headers := map[string][]string{"Content-Type": {"application/json"}}
    35  	return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers)
    36  }
    37  
    38  // ContainerExecInspect returns information about a specific exec process on the docker host.
    39  func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
    40  	var response types.ContainerExecInspect
    41  	resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
    42  	if err != nil {
    43  		return response, err
    44  	}
    45  
    46  	err = json.NewDecoder(resp.body).Decode(&response)
    47  	ensureReaderClosed(resp)
    48  	return response, err
    49  }