1package client // import "github.com/docker/docker/client"
2
3import (
4	"context"
5	"encoding/json"
6
7	"github.com/docker/docker/api/types"
8)
9
10// ContainerExecCreate creates a new exec configuration to run an exec process.
11func (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	defer ensureReaderClosed(resp)
20	if err != nil {
21		return response, err
22	}
23	err = json.NewDecoder(resp.body).Decode(&response)
24	return response, err
25}
26
27// ContainerExecStart starts an exec process already created in the docker host.
28func (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.
38func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (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.
44func (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}
55