2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
2023-07-10 14:44:59 +00:00
|
|
|
"net/http"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2022-06-15 07:28:20 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerExecCreate creates a new exec configuration to run an exec process.
|
2016-10-18 22:56:45 +00:00
|
|
|
func (cli *Client) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
|
|
|
|
var response types.IDResponse
|
2016-11-03 00:43:32 +00:00
|
|
|
|
2023-09-12 12:08:54 +00:00
|
|
|
// Make sure we negotiated (if the client is configured to do so),
|
|
|
|
// as code below contains API-version specific handling of options.
|
|
|
|
//
|
|
|
|
// Normally, version-negotiation (if enabled) would not happen until
|
|
|
|
// the API request is made.
|
2024-02-23 11:20:06 +00:00
|
|
|
if err := cli.checkVersion(ctx); err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
2023-09-12 12:08:54 +00:00
|
|
|
|
|
|
|
if err := cli.NewVersionError(ctx, "1.25", "env"); len(config.Env) != 0 && err != nil {
|
2016-11-03 00:43:32 +00:00
|
|
|
return response, err
|
|
|
|
}
|
2022-06-15 07:28:20 +00:00
|
|
|
if versions.LessThan(cli.ClientVersion(), "1.42") {
|
|
|
|
config.ConsoleSize = nil
|
|
|
|
}
|
2016-11-03 00:43:32 +00:00
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
resp, err := cli.post(ctx, "/containers/"+container+"/exec", nil, config, nil)
|
2019-02-11 12:26:12 +00:00
|
|
|
defer ensureReaderClosed(resp)
|
2016-09-06 18:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerExecStart starts an exec process already created in the docker host.
|
|
|
|
func (cli *Client) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
|
2022-06-15 07:28:20 +00:00
|
|
|
if versions.LessThan(cli.ClientVersion(), "1.42") {
|
|
|
|
config.ConsoleSize = nil
|
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
resp, err := cli.post(ctx, "/exec/"+execID+"/start", nil, config, nil)
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerExecAttach attaches a connection to an exec process in the server.
|
|
|
|
// It returns a types.HijackedConnection with the hijacked connection
|
|
|
|
// and the a reader to get output. It's up to the called to close
|
|
|
|
// the hijacked connection by calling types.HijackedResponse.Close.
|
2017-10-08 22:08:11 +00:00
|
|
|
func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error) {
|
2022-06-15 07:28:20 +00:00
|
|
|
if versions.LessThan(cli.ClientVersion(), "1.42") {
|
|
|
|
config.ConsoleSize = nil
|
|
|
|
}
|
2023-07-10 14:44:59 +00:00
|
|
|
return cli.postHijacked(ctx, "/exec/"+execID+"/start", nil, config, http.Header{
|
2019-08-28 11:46:32 +00:00
|
|
|
"Content-Type": {"application/json"},
|
2023-07-10 14:44:59 +00:00
|
|
|
})
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerExecInspect returns information about a specific exec process on the docker host.
|
|
|
|
func (cli *Client) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
|
|
|
|
var response types.ContainerExecInspect
|
|
|
|
resp, err := cli.get(ctx, "/exec/"+execID+"/json", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.body).Decode(&response)
|
|
|
|
ensureReaderClosed(resp)
|
|
|
|
return response, err
|
|
|
|
}
|