github.com/rish1988/moby@v25.0.2+incompatible/client/container_exec.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/api/types/versions"
    10  )
    11  
    12  // ContainerExecCreate creates a new exec configuration to run an exec process.
    13  func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
    14  	var response types.IDResponse
    15  
    16  	// Make sure we negotiated (if the client is configured to do so),
    17  	// as code below contains API-version specific handling of options.
    18  	//
    19  	// Normally, version-negotiation (if enabled) would not happen until
    20  	// the API request is made.
    21  	cli.checkVersion(ctx)
    22  
    23  	if err := cli.NewVersionError(ctx, "1.25", "env"); len(config.Env) != 0 && err != nil {
    24  		return response, err
    25  	}
    26  	if versions.LessThan(cli.ClientVersion(), "1.42") {
    27  		config.ConsoleSize = nil
    28  	}
    29  
    30  	resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
    31  	defer ensureReaderClosed(resp)
    32  	if err != nil {
    33  		return response, err
    34  	}
    35  	err = json.NewDecoder(resp.body).Decode(&response)
    36  	return response, err
    37  }
    38  
    39  // ContainerExecStart starts an exec process already created in the docker host.
    40  func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
    41  	if versions.LessThan(cli.ClientVersion(), "1.42") {
    42  		config.ConsoleSize = nil
    43  	}
    44  	resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
    45  	ensureReaderClosed(resp)
    46  	return err
    47  }
    48  
    49  // ContainerExecAttach attaches a connection to an exec process in the server.
    50  // It returns a types.HijackedConnection with the hijacked connection
    51  // and the a reader to get output. It's up to the called to close
    52  // the hijacked connection by calling types.HijackedResponse.Close.
    53  func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
    54  	if versions.LessThan(cli.ClientVersion(), "1.42") {
    55  		config.ConsoleSize = nil
    56  	}
    57  	return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, http.Header{
    58  		"Content-Type": {"application/json"},
    59  	})
    60  }
    61  
    62  // ContainerExecInspect returns information about a specific exec process on the docker host.
    63  func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
    64  	var response types.ContainerExecInspect
    65  	resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
    66  	if err != nil {
    67  		return response, err
    68  	}
    69  
    70  	err = json.NewDecoder(resp.body).Decode(&response)
    71  	ensureReaderClosed(resp)
    72  	return response, err
    73  }