gopkg.in/moby/moby.v1@v1.13.1/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.IDResponse, error) {
    12  	var response types.IDResponse
    13  
    14  	if err := cli.NewVersionError("1.25", "env"); len(config.Env) != 0 && err != nil {
    15  		return response, err
    16  	}
    17  
    18  	resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
    19  	if err != nil {
    20  		return response, err
    21  	}
    22  	err = json.NewDecoder(resp.body).Decode(&response)
    23  	ensureReaderClosed(resp)
    24  	return response, err
    25  }
    26  
    27  // ContainerExecStart starts an exec process already created in the docker host.
    28  func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
    29  	resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
    30  	ensureReaderClosed(resp)
    31  	return err
    32  }
    33  
    34  // ContainerExecAttach attaches a connection to an exec process in the server.
    35  // It returns a types.HijackedConnection with the hijacked connection
    36  // and the a reader to get output. It's up to the called to close
    37  // the hijacked connection by calling types.HijackedResponse.Close.
    38  func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error) {
    39  	headers := map[string][]string{"Content-Type": {"application/json"}}
    40  	return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, headers)
    41  }
    42  
    43  // ContainerExecInspect returns information about a specific exec process on the docker host.
    44  func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
    45  	var response types.ContainerExecInspect
    46  	resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
    47  	if err != nil {
    48  		return response, err
    49  	}
    50  
    51  	err = json.NewDecoder(resp.body).Decode(&response)
    52  	ensureReaderClosed(resp)
    53  	return response, err
    54  }