mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #22878 from vdemeester/better-default-context
Rework usage of context.Context in api/client
This commit is contained in:
commit
5f95750ab4
28 changed files with 199 additions and 150 deletions
|
@ -27,7 +27,9 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
c, err := cli.client.ContainerInspect(context.Background(), cmd.Arg(0))
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := cli.client.ContainerInspect(ctx, cmd.Arg(0))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -64,11 +66,11 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
|||
}
|
||||
|
||||
if *proxy && !c.Config.Tty {
|
||||
sigc := cli.forwardAllSignals(container)
|
||||
sigc := cli.forwardAllSignals(ctx, container)
|
||||
defer signal.StopCatch(sigc)
|
||||
}
|
||||
|
||||
resp, errAttach := cli.client.ContainerAttach(context.Background(), container, options)
|
||||
resp, errAttach := cli.client.ContainerAttach(ctx, container, options)
|
||||
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
||||
// ContainerAttach returns an ErrPersistEOF (connection closed)
|
||||
// means server met an error and put it in Hijacked connection
|
||||
|
@ -83,15 +85,15 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
|||
// terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
|
||||
// resize it, then go back to normal. Without this, every attach after the first will
|
||||
// require the user to manually resize or hit enter.
|
||||
cli.resizeTtyTo(cmd.Arg(0), height+1, width+1, false)
|
||||
cli.resizeTtyTo(ctx, cmd.Arg(0), height+1, width+1, false)
|
||||
|
||||
// After the above resizing occurs, the call to monitorTtySize below will handle resetting back
|
||||
// to the actual size.
|
||||
if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
|
||||
if err := cli.monitorTtySize(ctx, cmd.Arg(0), false); err != nil {
|
||||
logrus.Debugf("Error monitoring TTY size: %s", err)
|
||||
}
|
||||
}
|
||||
if err := cli.holdHijackedConnection(context.Background(), c.Config.Tty, in, cli.out, cli.err, resp); err != nil {
|
||||
if err := cli.holdHijackedConnection(ctx, c.Config.Tty, in, cli.out, cli.err, resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -99,7 +101,7 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
|||
return errAttach
|
||||
}
|
||||
|
||||
_, status, err := getExitCode(cli, container)
|
||||
_, status, err := cli.getExitCode(ctx, container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ import (
|
|||
"github.com/docker/go-units"
|
||||
)
|
||||
|
||||
type translatorFunc func(reference.NamedTagged) (reference.Canonical, error)
|
||||
type translatorFunc func(context.Context, reference.NamedTagged) (reference.Canonical, error)
|
||||
|
||||
// CmdBuild builds a new image from the source code at a given path.
|
||||
//
|
||||
|
@ -77,8 +77,8 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
cmd.ParseFlags(args, true)
|
||||
|
||||
var (
|
||||
ctx io.ReadCloser
|
||||
err error
|
||||
buildCtx io.ReadCloser
|
||||
err error
|
||||
)
|
||||
|
||||
specifiedContext := cmd.Arg(0)
|
||||
|
@ -100,11 +100,11 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
|
||||
switch {
|
||||
case specifiedContext == "-":
|
||||
ctx, relDockerfile, err = builder.GetContextFromReader(cli.in, *dockerfileName)
|
||||
buildCtx, relDockerfile, err = builder.GetContextFromReader(cli.in, *dockerfileName)
|
||||
case urlutil.IsGitURL(specifiedContext):
|
||||
tempDir, relDockerfile, err = builder.GetContextFromGitURL(specifiedContext, *dockerfileName)
|
||||
case urlutil.IsURL(specifiedContext):
|
||||
ctx, relDockerfile, err = builder.GetContextFromURL(progBuff, specifiedContext, *dockerfileName)
|
||||
buildCtx, relDockerfile, err = builder.GetContextFromURL(progBuff, specifiedContext, *dockerfileName)
|
||||
default:
|
||||
contextDir, relDockerfile, err = builder.GetContextFromLocalDir(specifiedContext, *dockerfileName)
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
contextDir = tempDir
|
||||
}
|
||||
|
||||
if ctx == nil {
|
||||
if buildCtx == nil {
|
||||
// And canonicalize dockerfile name to a platform-independent one
|
||||
relDockerfile, err = archive.CanonicalTarNameForPath(relDockerfile)
|
||||
if err != nil {
|
||||
|
@ -159,7 +159,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
includes = append(includes, ".dockerignore", relDockerfile)
|
||||
}
|
||||
|
||||
ctx, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
|
||||
buildCtx, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
|
||||
Compression: archive.Uncompressed,
|
||||
ExcludePatterns: excludes,
|
||||
IncludeFiles: includes,
|
||||
|
@ -169,17 +169,19 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var resolvedTags []*resolvedTag
|
||||
if isTrusted() {
|
||||
// Wrap the tar archive to replace the Dockerfile entry with the rewritten
|
||||
// Dockerfile which uses trusted pulls.
|
||||
ctx = replaceDockerfileTarWrapper(ctx, relDockerfile, cli.trustedReference, &resolvedTags)
|
||||
buildCtx = replaceDockerfileTarWrapper(ctx, buildCtx, relDockerfile, cli.trustedReference, &resolvedTags)
|
||||
}
|
||||
|
||||
// Setup an upload progress bar
|
||||
progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(progBuff, true)
|
||||
|
||||
var body io.Reader = progress.NewProgressReader(ctx, progressOutput, 0, "", "Sending build context to Docker daemon")
|
||||
var body io.Reader = progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
|
||||
|
||||
var memory int64
|
||||
if *flMemoryString != "" {
|
||||
|
@ -235,7 +237,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
|
||||
}
|
||||
|
||||
response, err := cli.client.ImageBuild(context.Background(), body, options)
|
||||
response, err := cli.client.ImageBuild(ctx, body, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -271,7 +273,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
|||
// Since the build was successful, now we must tag any of the resolved
|
||||
// images from the above Dockerfile rewrite.
|
||||
for _, resolved := range resolvedTags {
|
||||
if err := cli.tagTrusted(resolved.digestRef, resolved.tagRef); err != nil {
|
||||
if err := cli.tagTrusted(ctx, resolved.digestRef, resolved.tagRef); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -303,7 +305,7 @@ type resolvedTag struct {
|
|||
// "FROM <image>" instructions to a digest reference. `translator` is a
|
||||
// function that takes a repository name and tag reference and returns a
|
||||
// trusted digest reference.
|
||||
func rewriteDockerfileFrom(dockerfile io.Reader, translator translatorFunc) (newDockerfile []byte, resolvedTags []*resolvedTag, err error) {
|
||||
func rewriteDockerfileFrom(ctx context.Context, dockerfile io.Reader, translator translatorFunc) (newDockerfile []byte, resolvedTags []*resolvedTag, err error) {
|
||||
scanner := bufio.NewScanner(dockerfile)
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
||||
|
@ -320,7 +322,7 @@ func rewriteDockerfileFrom(dockerfile io.Reader, translator translatorFunc) (new
|
|||
}
|
||||
ref = reference.WithDefaultTag(ref)
|
||||
if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
|
||||
trustedRef, err := translator(ref)
|
||||
trustedRef, err := translator(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
@ -346,7 +348,7 @@ func rewriteDockerfileFrom(dockerfile io.Reader, translator translatorFunc) (new
|
|||
// replaces the entry with the given Dockerfile name with the contents of the
|
||||
// new Dockerfile. Returns a new tar archive stream with the replaced
|
||||
// Dockerfile.
|
||||
func replaceDockerfileTarWrapper(inputTarStream io.ReadCloser, dockerfileName string, translator translatorFunc, resolvedTags *[]*resolvedTag) io.ReadCloser {
|
||||
func replaceDockerfileTarWrapper(ctx context.Context, inputTarStream io.ReadCloser, dockerfileName string, translator translatorFunc, resolvedTags *[]*resolvedTag) io.ReadCloser {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
go func() {
|
||||
tarReader := tar.NewReader(inputTarStream)
|
||||
|
@ -373,7 +375,7 @@ func replaceDockerfileTarWrapper(inputTarStream io.ReadCloser, dockerfileName st
|
|||
// generated from a directory on the local filesystem, the
|
||||
// Dockerfile will only appear once in the archive.
|
||||
var newDockerfile []byte
|
||||
newDockerfile, *resolvedTags, err = rewriteDockerfileFrom(content, translator)
|
||||
newDockerfile, *resolvedTags, err = rewriteDockerfileFrom(ctx, content, translator)
|
||||
if err != nil {
|
||||
pipeWriter.CloseWithError(err)
|
||||
return
|
||||
|
|
|
@ -81,11 +81,13 @@ func (cli *DockerCli) CmdCp(args ...string) error {
|
|||
followLink: *followLink,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
switch direction {
|
||||
case fromContainer:
|
||||
return cli.copyFromContainer(srcContainer, srcPath, dstPath, cpParam)
|
||||
return cli.copyFromContainer(ctx, srcContainer, srcPath, dstPath, cpParam)
|
||||
case toContainer:
|
||||
return cli.copyToContainer(srcPath, dstContainer, dstPath, cpParam)
|
||||
return cli.copyToContainer(ctx, srcPath, dstContainer, dstPath, cpParam)
|
||||
case acrossContainers:
|
||||
// Copying between containers isn't supported.
|
||||
return fmt.Errorf("copying between containers is not supported")
|
||||
|
@ -126,8 +128,8 @@ func splitCpArg(arg string) (container, path string) {
|
|||
return parts[0], parts[1]
|
||||
}
|
||||
|
||||
func (cli *DockerCli) statContainerPath(containerName, path string) (types.ContainerPathStat, error) {
|
||||
return cli.client.ContainerStatPath(context.Background(), containerName, path)
|
||||
func (cli *DockerCli) statContainerPath(ctx context.Context, containerName, path string) (types.ContainerPathStat, error) {
|
||||
return cli.client.ContainerStatPath(ctx, containerName, path)
|
||||
}
|
||||
|
||||
func resolveLocalPath(localPath string) (absPath string, err error) {
|
||||
|
@ -138,7 +140,7 @@ func resolveLocalPath(localPath string) (absPath string, err error) {
|
|||
return archive.PreserveTrailingDotOrSeparator(absPath, localPath), nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) copyFromContainer(srcContainer, srcPath, dstPath string, cpParam *cpConfig) (err error) {
|
||||
func (cli *DockerCli) copyFromContainer(ctx context.Context, srcContainer, srcPath, dstPath string, cpParam *cpConfig) (err error) {
|
||||
if dstPath != "-" {
|
||||
// Get an absolute destination path.
|
||||
dstPath, err = resolveLocalPath(dstPath)
|
||||
|
@ -150,7 +152,7 @@ func (cli *DockerCli) copyFromContainer(srcContainer, srcPath, dstPath string, c
|
|||
// if client requests to follow symbol link, then must decide target file to be copied
|
||||
var rebaseName string
|
||||
if cpParam.followLink {
|
||||
srcStat, err := cli.statContainerPath(srcContainer, srcPath)
|
||||
srcStat, err := cli.statContainerPath(ctx, srcContainer, srcPath)
|
||||
|
||||
// If the destination is a symbolic link, we should follow it.
|
||||
if err == nil && srcStat.Mode&os.ModeSymlink != 0 {
|
||||
|
@ -167,7 +169,7 @@ func (cli *DockerCli) copyFromContainer(srcContainer, srcPath, dstPath string, c
|
|||
|
||||
}
|
||||
|
||||
content, stat, err := cli.client.CopyFromContainer(context.Background(), srcContainer, srcPath)
|
||||
content, stat, err := cli.client.CopyFromContainer(ctx, srcContainer, srcPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -199,7 +201,7 @@ func (cli *DockerCli) copyFromContainer(srcContainer, srcPath, dstPath string, c
|
|||
return archive.CopyTo(preArchive, srcInfo, dstPath)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string, cpParam *cpConfig) (err error) {
|
||||
func (cli *DockerCli) copyToContainer(ctx context.Context, srcPath, dstContainer, dstPath string, cpParam *cpConfig) (err error) {
|
||||
if srcPath != "-" {
|
||||
// Get an absolute source path.
|
||||
srcPath, err = resolveLocalPath(srcPath)
|
||||
|
@ -215,7 +217,7 @@ func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string, cpP
|
|||
|
||||
// Prepare destination copy info by stat-ing the container path.
|
||||
dstInfo := archive.CopyInfo{Path: dstPath}
|
||||
dstStat, err := cli.statContainerPath(dstContainer, dstPath)
|
||||
dstStat, err := cli.statContainerPath(ctx, dstContainer, dstPath)
|
||||
|
||||
// If the destination is a symbolic link, we should evaluate it.
|
||||
if err == nil && dstStat.Mode&os.ModeSymlink != 0 {
|
||||
|
@ -227,7 +229,7 @@ func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string, cpP
|
|||
}
|
||||
|
||||
dstInfo.Path = linkTarget
|
||||
dstStat, err = cli.statContainerPath(dstContainer, linkTarget)
|
||||
dstStat, err = cli.statContainerPath(ctx, dstContainer, linkTarget)
|
||||
}
|
||||
|
||||
// Ignore any error and assume that the parent directory of the destination
|
||||
|
@ -291,5 +293,5 @@ func (cli *DockerCli) copyToContainer(srcPath, dstContainer, dstPath string, cpP
|
|||
AllowOverwriteDirWithFile: false,
|
||||
}
|
||||
|
||||
return cli.client.CopyToContainer(context.Background(), dstContainer, resolvedDstPath, content, options)
|
||||
return cli.client.CopyToContainer(ctx, dstContainer, resolvedDstPath, content, options)
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ import (
|
|||
networktypes "github.com/docker/engine-api/types/network"
|
||||
)
|
||||
|
||||
func (cli *DockerCli) pullImage(image string, out io.Writer) error {
|
||||
func (cli *DockerCli) pullImage(ctx context.Context, image string, out io.Writer) error {
|
||||
ref, err := reference.ParseNamed(image)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -31,7 +31,7 @@ func (cli *DockerCli) pullImage(image string, out io.Writer) error {
|
|||
return err
|
||||
}
|
||||
|
||||
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
||||
authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
|
||||
encodedAuth, err := encodeAuthToBase64(authConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -41,7 +41,7 @@ func (cli *DockerCli) pullImage(image string, out io.Writer) error {
|
|||
RegistryAuth: encodedAuth,
|
||||
}
|
||||
|
||||
responseBody, err := cli.client.ImageCreate(context.Background(), image, options)
|
||||
responseBody, err := cli.client.ImageCreate(ctx, image, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ func newCIDFile(path string) (*cidFile, error) {
|
|||
return &cidFile{path: path, file: f}, nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) createContainer(config *container.Config, hostConfig *container.HostConfig, networkingConfig *networktypes.NetworkingConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
|
||||
func (cli *DockerCli) createContainer(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *networktypes.NetworkingConfig, cidfile, name string) (*types.ContainerCreateResponse, error) {
|
||||
var containerIDFile *cidFile
|
||||
if cidfile != "" {
|
||||
var err error
|
||||
|
@ -89,7 +89,7 @@ func (cli *DockerCli) createContainer(config *container.Config, hostConfig *cont
|
|||
|
||||
if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
|
||||
var err error
|
||||
trustedRef, err = cli.trustedReference(ref)
|
||||
trustedRef, err = cli.trustedReference(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (cli *DockerCli) createContainer(config *container.Config, hostConfig *cont
|
|||
}
|
||||
|
||||
//create the container
|
||||
response, err := cli.client.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, name)
|
||||
response, err := cli.client.ContainerCreate(ctx, config, hostConfig, networkingConfig, name)
|
||||
|
||||
//if image not found try to pull it
|
||||
if err != nil {
|
||||
|
@ -106,17 +106,17 @@ func (cli *DockerCli) createContainer(config *container.Config, hostConfig *cont
|
|||
fmt.Fprintf(cli.err, "Unable to find image '%s' locally\n", ref.String())
|
||||
|
||||
// we don't want to write to stdout anything apart from container.ID
|
||||
if err = cli.pullImage(config.Image, cli.err); err != nil {
|
||||
if err = cli.pullImage(ctx, config.Image, cli.err); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ref, ok := ref.(reference.NamedTagged); ok && trustedRef != nil {
|
||||
if err := cli.tagTrusted(trustedRef, ref); err != nil {
|
||||
if err := cli.tagTrusted(ctx, trustedRef, ref); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// Retry
|
||||
var retryErr error
|
||||
response, retryErr = cli.client.ContainerCreate(context.Background(), config, hostConfig, networkingConfig, name)
|
||||
response, retryErr = cli.client.ContainerCreate(ctx, config, hostConfig, networkingConfig, name)
|
||||
if retryErr != nil {
|
||||
return nil, retryErr
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ func (cli *DockerCli) CmdCreate(args ...string) error {
|
|||
cmd.Usage()
|
||||
return nil
|
||||
}
|
||||
response, err := cli.createContainer(config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
||||
response, err := cli.createContainer(context.Background(), config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -34,7 +34,9 @@ func (cli *DockerCli) CmdExec(args ...string) error {
|
|||
// Send client escape keys
|
||||
execConfig.DetachKeys = cli.configFile.DetachKeys
|
||||
|
||||
response, err := cli.client.ContainerExecCreate(context.Background(), container, *execConfig)
|
||||
ctx := context.Background()
|
||||
|
||||
response, err := cli.client.ContainerExecCreate(ctx, container, *execConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -56,7 +58,7 @@ func (cli *DockerCli) CmdExec(args ...string) error {
|
|||
Tty: execConfig.Tty,
|
||||
}
|
||||
|
||||
if err := cli.client.ContainerExecStart(context.Background(), execID, execStartCheck); err != nil {
|
||||
if err := cli.client.ContainerExecStart(ctx, execID, execStartCheck); err != nil {
|
||||
return err
|
||||
}
|
||||
// For now don't print this - wait for when we support exec wait()
|
||||
|
@ -85,17 +87,17 @@ func (cli *DockerCli) CmdExec(args ...string) error {
|
|||
}
|
||||
}
|
||||
|
||||
resp, err := cli.client.ContainerExecAttach(context.Background(), execID, *execConfig)
|
||||
resp, err := cli.client.ContainerExecAttach(ctx, execID, *execConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Close()
|
||||
errCh = promise.Go(func() error {
|
||||
return cli.holdHijackedConnection(context.Background(), execConfig.Tty, in, out, stderr, resp)
|
||||
return cli.holdHijackedConnection(ctx, execConfig.Tty, in, out, stderr, resp)
|
||||
})
|
||||
|
||||
if execConfig.Tty && cli.isTerminalIn {
|
||||
if err := cli.monitorTtySize(execID, true); err != nil {
|
||||
if err := cli.monitorTtySize(ctx, execID, true); err != nil {
|
||||
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +108,7 @@ func (cli *DockerCli) CmdExec(args ...string) error {
|
|||
}
|
||||
|
||||
var status int
|
||||
if _, status, err = getExecExitCode(cli, execID); err != nil {
|
||||
if _, status, err = cli.getExecExitCode(ctx, execID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -38,5 +38,4 @@ func (cli *DockerCli) CmdExport(args ...string) error {
|
|||
}
|
||||
|
||||
return copyToFile(*outfile, responseBody)
|
||||
|
||||
}
|
||||
|
|
|
@ -28,38 +28,40 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
|
|||
return fmt.Errorf("%q is not a valid value for --type", *inspectType)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var elementSearcher inspectSearcher
|
||||
switch *inspectType {
|
||||
case "container":
|
||||
elementSearcher = cli.inspectContainers(*size)
|
||||
elementSearcher = cli.inspectContainers(ctx, *size)
|
||||
case "image":
|
||||
elementSearcher = cli.inspectImages(*size)
|
||||
elementSearcher = cli.inspectImages(ctx, *size)
|
||||
default:
|
||||
elementSearcher = cli.inspectAll(*size)
|
||||
elementSearcher = cli.inspectAll(ctx, *size)
|
||||
}
|
||||
|
||||
return cli.inspectElements(*tmplStr, cmd.Args(), elementSearcher)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) inspectContainers(getSize bool) inspectSearcher {
|
||||
func (cli *DockerCli) inspectContainers(ctx context.Context, getSize bool) inspectSearcher {
|
||||
return func(ref string) (interface{}, []byte, error) {
|
||||
return cli.client.ContainerInspectWithRaw(context.Background(), ref, getSize)
|
||||
return cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *DockerCli) inspectImages(getSize bool) inspectSearcher {
|
||||
func (cli *DockerCli) inspectImages(ctx context.Context, getSize bool) inspectSearcher {
|
||||
return func(ref string) (interface{}, []byte, error) {
|
||||
return cli.client.ImageInspectWithRaw(context.Background(), ref, getSize)
|
||||
return cli.client.ImageInspectWithRaw(ctx, ref, getSize)
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *DockerCli) inspectAll(getSize bool) inspectSearcher {
|
||||
func (cli *DockerCli) inspectAll(ctx context.Context, getSize bool) inspectSearcher {
|
||||
return func(ref string) (interface{}, []byte, error) {
|
||||
c, rawContainer, err := cli.client.ContainerInspectWithRaw(context.Background(), ref, getSize)
|
||||
c, rawContainer, err := cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
|
||||
if err != nil {
|
||||
// Search for image with that id if a container doesn't exist.
|
||||
if client.IsErrContainerNotFound(err) {
|
||||
i, rawImage, err := cli.client.ImageInspectWithRaw(context.Background(), ref, getSize)
|
||||
i, rawImage, err := cli.client.ImageInspectWithRaw(ctx, ref, getSize)
|
||||
if err != nil {
|
||||
if client.IsErrImageNotFound(err) {
|
||||
return nil, nil, fmt.Errorf("Error: No such image or container: %s", ref)
|
||||
|
|
|
@ -40,12 +40,14 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
|
|||
cli.in = os.Stdin
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var serverAddress string
|
||||
var isDefaultRegistry bool
|
||||
if len(cmd.Args()) > 0 {
|
||||
serverAddress = cmd.Arg(0)
|
||||
} else {
|
||||
serverAddress = cli.electAuthServer()
|
||||
serverAddress = cli.electAuthServer(ctx)
|
||||
isDefaultRegistry = true
|
||||
}
|
||||
|
||||
|
@ -54,7 +56,7 @@ func (cli *DockerCli) CmdLogin(args ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
response, err := cli.client.RegistryLogin(context.Background(), authConfig)
|
||||
response, err := cli.client.RegistryLogin(ctx, authConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package client
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
Cli "github.com/docker/docker/cli"
|
||||
flag "github.com/docker/docker/pkg/mflag"
|
||||
)
|
||||
|
@ -22,7 +24,7 @@ func (cli *DockerCli) CmdLogout(args ...string) error {
|
|||
if len(cmd.Args()) > 0 {
|
||||
serverAddress = cmd.Arg(0)
|
||||
} else {
|
||||
serverAddress = cli.electAuthServer()
|
||||
serverAddress = cli.electAuthServer(context.Background())
|
||||
}
|
||||
|
||||
// check if we're logged in based on the records in the config file
|
||||
|
|
|
@ -33,7 +33,9 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
|
|||
|
||||
name := cmd.Arg(0)
|
||||
|
||||
c, err := cli.client.ContainerInspect(context.Background(), name)
|
||||
ctx := context.Background()
|
||||
|
||||
c, err := cli.client.ContainerInspect(ctx, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -51,7 +53,7 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
|
|||
Tail: *tail,
|
||||
Details: *details,
|
||||
}
|
||||
responseBody, err := cli.client.ContainerLogs(context.Background(), name, options)
|
||||
responseBody, err := cli.client.ContainerLogs(ctx, name, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -104,9 +104,11 @@ func (cli *DockerCli) CmdNetworkRm(args ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
status := 0
|
||||
for _, net := range cmd.Args() {
|
||||
if err := cli.client.NetworkRemove(context.Background(), net); err != nil {
|
||||
if err := cli.client.NetworkRemove(ctx, net); err != nil {
|
||||
fmt.Fprintf(cli.err, "%s\n", err)
|
||||
status = 1
|
||||
continue
|
||||
|
@ -239,8 +241,10 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
inspectSearcher := func(name string) (interface{}, []byte, error) {
|
||||
i, err := cli.client.NetworkInspect(context.Background(), name)
|
||||
i, err := cli.client.NetworkInspect(ctx, name)
|
||||
return i, nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,11 @@ func (cli *DockerCli) CmdPause(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, name := range cmd.Args() {
|
||||
if err := cli.client.ContainerPause(context.Background(), name); err != nil {
|
||||
if err := cli.client.ContainerPause(ctx, name); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
fmt.Fprintf(cli.out, "%s\n", name)
|
||||
|
|
|
@ -55,18 +55,20 @@ func (cli *DockerCli) CmdPull(args ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
||||
ctx := context.Background()
|
||||
|
||||
authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
|
||||
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "pull")
|
||||
|
||||
if isTrusted() && !registryRef.HasDigest() {
|
||||
// Check if tag is digest
|
||||
return cli.trustedPull(repoInfo, registryRef, authConfig, requestPrivilege)
|
||||
return cli.trustedPull(ctx, repoInfo, registryRef, authConfig, requestPrivilege)
|
||||
}
|
||||
|
||||
return cli.imagePullPrivileged(authConfig, distributionRef.String(), requestPrivilege, *allTags)
|
||||
return cli.imagePullPrivileged(ctx, authConfig, distributionRef.String(), requestPrivilege, *allTags)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
|
||||
func (cli *DockerCli) imagePullPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc, all bool) error {
|
||||
|
||||
encodedAuth, err := encodeAuthToBase64(authConfig)
|
||||
if err != nil {
|
||||
|
@ -78,7 +80,7 @@ func (cli *DockerCli) imagePullPrivileged(authConfig types.AuthConfig, ref strin
|
|||
All: all,
|
||||
}
|
||||
|
||||
responseBody, err := cli.client.ImagePull(context.Background(), ref, options)
|
||||
responseBody, err := cli.client.ImagePull(ctx, ref, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -33,15 +33,18 @@ func (cli *DockerCli) CmdPush(args ...string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Resolve the Auth config relevant for this server
|
||||
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Resolve the Auth config relevant for this server
|
||||
authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
|
||||
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(repoInfo.Index, "push")
|
||||
|
||||
if isTrusted() {
|
||||
return cli.trustedPush(repoInfo, ref, authConfig, requestPrivilege)
|
||||
return cli.trustedPush(ctx, repoInfo, ref, authConfig, requestPrivilege)
|
||||
}
|
||||
|
||||
responseBody, err := cli.imagePushPrivileged(authConfig, ref.String(), requestPrivilege)
|
||||
responseBody, err := cli.imagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -51,7 +54,7 @@ func (cli *DockerCli) CmdPush(args ...string) error {
|
|||
return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) imagePushPrivileged(authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc) (io.ReadCloser, error) {
|
||||
func (cli *DockerCli) imagePushPrivileged(ctx context.Context, authConfig types.AuthConfig, ref string, requestPrivilege types.RequestPrivilegeFunc) (io.ReadCloser, error) {
|
||||
encodedAuth, err := encodeAuthToBase64(authConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -61,5 +64,5 @@ func (cli *DockerCli) imagePushPrivileged(authConfig types.AuthConfig, ref strin
|
|||
PrivilegeFunc: requestPrivilege,
|
||||
}
|
||||
|
||||
return cli.client.ImagePush(context.Background(), ref, options)
|
||||
return cli.client.ImagePush(ctx, ref, options)
|
||||
}
|
||||
|
|
|
@ -23,6 +23,8 @@ func (cli *DockerCli) CmdRm(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, name := range cmd.Args() {
|
||||
if name == "" {
|
||||
|
@ -30,7 +32,7 @@ func (cli *DockerCli) CmdRm(args ...string) error {
|
|||
}
|
||||
name = strings.Trim(name, "/")
|
||||
|
||||
if err := cli.removeContainer(name, *v, *link, *force); err != nil {
|
||||
if err := cli.removeContainer(ctx, name, *v, *link, *force); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
fmt.Fprintf(cli.out, "%s\n", name)
|
||||
|
@ -42,13 +44,13 @@ func (cli *DockerCli) CmdRm(args ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) removeContainer(container string, removeVolumes, removeLinks, force bool) error {
|
||||
func (cli *DockerCli) removeContainer(ctx context.Context, container string, removeVolumes, removeLinks, force bool) error {
|
||||
options := types.ContainerRemoveOptions{
|
||||
RemoveVolumes: removeVolumes,
|
||||
RemoveLinks: removeLinks,
|
||||
Force: force,
|
||||
}
|
||||
if err := cli.client.ContainerRemove(context.Background(), container, options); err != nil {
|
||||
if err := cli.client.ContainerRemove(ctx, container, options); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -31,6 +31,8 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
|
|||
v.Set("noprune", "1")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, image := range cmd.Args() {
|
||||
options := types.ImageRemoveOptions{
|
||||
|
@ -38,7 +40,7 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
|
|||
PruneChildren: !*noprune,
|
||||
}
|
||||
|
||||
dels, err := cli.client.ImageRemove(context.Background(), image, options)
|
||||
dels, err := cli.client.ImageRemove(ctx, image, options)
|
||||
if err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
|
|
|
@ -147,20 +147,20 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = cli.getTtySize()
|
||||
}
|
||||
|
||||
createResponse, err := cli.createContainer(config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
||||
ctx, cancelFun := context.WithCancel(context.Background())
|
||||
|
||||
createResponse, err := cli.createContainer(ctx, config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, *flName)
|
||||
if err != nil {
|
||||
cmd.ReportError(err.Error(), true)
|
||||
return runStartContainerErr(err)
|
||||
}
|
||||
if sigProxy {
|
||||
sigc := cli.forwardAllSignals(createResponse.ID)
|
||||
sigc := cli.forwardAllSignals(ctx, createResponse.ID)
|
||||
defer signal.StopCatch(sigc)
|
||||
}
|
||||
var (
|
||||
waitDisplayID chan struct{}
|
||||
errCh chan error
|
||||
cancelFun context.CancelFunc
|
||||
ctx context.Context
|
||||
)
|
||||
if !config.AttachStdout && !config.AttachStderr {
|
||||
// Make this asynchronous to allow the client to write to stdin before having to read the ID
|
||||
|
@ -205,7 +205,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
DetachKeys: cli.configFile.DetachKeys,
|
||||
}
|
||||
|
||||
resp, errAttach := cli.client.ContainerAttach(context.Background(), createResponse.ID, options)
|
||||
resp, errAttach := cli.client.ContainerAttach(ctx, createResponse.ID, options)
|
||||
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
||||
// ContainerAttach returns an ErrPersistEOF (connection closed)
|
||||
// means server met an error and put it in Hijacked connection
|
||||
|
@ -214,7 +214,6 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
}
|
||||
defer resp.Close()
|
||||
|
||||
ctx, cancelFun = context.WithCancel(context.Background())
|
||||
errCh = promise.Go(func() error {
|
||||
errHijack := cli.holdHijackedConnection(ctx, config.Tty, in, out, stderr, resp)
|
||||
if errHijack == nil {
|
||||
|
@ -226,14 +225,16 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
|
||||
if *flAutoRemove {
|
||||
defer func() {
|
||||
if err := cli.removeContainer(createResponse.ID, true, false, true); err != nil {
|
||||
// Explicitly not sharing the context as it could be "Done" (by calling cancelFun)
|
||||
// and thus the container would not be removed.
|
||||
if err := cli.removeContainer(context.Background(), createResponse.ID, true, false, true); err != nil {
|
||||
fmt.Fprintf(cli.err, "%v\n", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
//start the container
|
||||
if err := cli.client.ContainerStart(context.Background(), createResponse.ID); err != nil {
|
||||
if err := cli.client.ContainerStart(ctx, createResponse.ID); err != nil {
|
||||
// If we have holdHijackedConnection, we should notify
|
||||
// holdHijackedConnection we are going to exit and wait
|
||||
// to avoid the terminal are not restored.
|
||||
|
@ -247,7 +248,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
}
|
||||
|
||||
if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && cli.isTerminalOut {
|
||||
if err := cli.monitorTtySize(createResponse.ID, false); err != nil {
|
||||
if err := cli.monitorTtySize(ctx, createResponse.ID, false); err != nil {
|
||||
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
@ -272,23 +273,23 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
|||
if *flAutoRemove {
|
||||
// Autoremove: wait for the container to finish, retrieve
|
||||
// the exit code and remove the container
|
||||
if status, err = cli.client.ContainerWait(context.Background(), createResponse.ID); err != nil {
|
||||
if status, err = cli.client.ContainerWait(ctx, createResponse.ID); err != nil {
|
||||
return runStartContainerErr(err)
|
||||
}
|
||||
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
||||
if _, status, err = cli.getExitCode(ctx, createResponse.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// No Autoremove: Simply retrieve the exit code
|
||||
if !config.Tty {
|
||||
// In non-TTY mode, we can't detach, so we must wait for container exit
|
||||
if status, err = cli.client.ContainerWait(context.Background(), createResponse.ID); err != nil {
|
||||
if status, err = cli.client.ContainerWait(ctx, createResponse.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// In TTY mode, there is a race: if the process dies too slowly, the state could
|
||||
// be updated after the getExitCode call and result in the wrong exit code being reported
|
||||
if _, status, err = getExitCode(cli, createResponse.ID); err != nil {
|
||||
if _, status, err = cli.getExitCode(ctx, createResponse.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,9 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
authConfig := cli.resolveAuthConfig(indexInfo)
|
||||
ctx := context.Background()
|
||||
|
||||
authConfig := cli.resolveAuthConfig(ctx, indexInfo)
|
||||
requestPrivilege := cli.registryAuthenticationPrivilegedFunc(indexInfo, "search")
|
||||
|
||||
encodedAuth, err := encodeAuthToBase64(authConfig)
|
||||
|
@ -72,7 +74,7 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
|
|||
Filters: filterArgs,
|
||||
}
|
||||
|
||||
unorderedResults, err := cli.client.ImageSearch(context.Background(), name, options)
|
||||
unorderedResults, err := cli.client.ImageSearch(ctx, name, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
"github.com/docker/engine-api/types"
|
||||
)
|
||||
|
||||
func (cli *DockerCli) forwardAllSignals(cid string) chan os.Signal {
|
||||
func (cli *DockerCli) forwardAllSignals(ctx context.Context, cid string) chan os.Signal {
|
||||
sigc := make(chan os.Signal, 128)
|
||||
signal.CatchAll(sigc)
|
||||
go func() {
|
||||
|
@ -37,7 +37,7 @@ func (cli *DockerCli) forwardAllSignals(cid string) chan os.Signal {
|
|||
continue
|
||||
}
|
||||
|
||||
if err := cli.client.ContainerKill(context.Background(), cid, sig); err != nil {
|
||||
if err := cli.client.ContainerKill(ctx, cid, sig); err != nil {
|
||||
logrus.Debugf("Error sending signal: %s", err)
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,8 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx, cancelFun := context.WithCancel(context.Background())
|
||||
|
||||
if *attach || *openStdin {
|
||||
// We're going to attach to a container.
|
||||
// 1. Ensure we only have one container.
|
||||
|
@ -66,13 +68,13 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
|
||||
// 2. Attach to the container.
|
||||
container := cmd.Arg(0)
|
||||
c, err := cli.client.ContainerInspect(context.Background(), container)
|
||||
c, err := cli.client.ContainerInspect(ctx, container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.Config.Tty {
|
||||
sigc := cli.forwardAllSignals(container)
|
||||
sigc := cli.forwardAllSignals(ctx, container)
|
||||
defer signal.StopCatch(sigc)
|
||||
}
|
||||
|
||||
|
@ -94,7 +96,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
in = cli.in
|
||||
}
|
||||
|
||||
resp, errAttach := cli.client.ContainerAttach(context.Background(), container, options)
|
||||
resp, errAttach := cli.client.ContainerAttach(ctx, container, options)
|
||||
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
|
||||
// ContainerAttach return an ErrPersistEOF (connection closed)
|
||||
// means server met an error and put it in Hijacked connection
|
||||
|
@ -102,7 +104,6 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
return errAttach
|
||||
}
|
||||
defer resp.Close()
|
||||
ctx, cancelFun := context.WithCancel(context.Background())
|
||||
cErr := promise.Go(func() error {
|
||||
errHijack := cli.holdHijackedConnection(ctx, c.Config.Tty, in, cli.out, cli.err, resp)
|
||||
if errHijack == nil {
|
||||
|
@ -112,7 +113,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
})
|
||||
|
||||
// 3. Start the container.
|
||||
if err := cli.client.ContainerStart(context.Background(), container); err != nil {
|
||||
if err := cli.client.ContainerStart(ctx, container); err != nil {
|
||||
cancelFun()
|
||||
<-cErr
|
||||
return err
|
||||
|
@ -120,14 +121,14 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
|
||||
// 4. Wait for attachment to break.
|
||||
if c.Config.Tty && cli.isTerminalOut {
|
||||
if err := cli.monitorTtySize(container, false); err != nil {
|
||||
if err := cli.monitorTtySize(ctx, container, false); err != nil {
|
||||
fmt.Fprintf(cli.err, "Error monitoring TTY size: %s\n", err)
|
||||
}
|
||||
}
|
||||
if attchErr := <-cErr; attchErr != nil {
|
||||
return attchErr
|
||||
}
|
||||
_, status, err := getExitCode(cli, container)
|
||||
_, status, err := cli.getExitCode(ctx, container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -137,16 +138,16 @@ func (cli *DockerCli) CmdStart(args ...string) error {
|
|||
} else {
|
||||
// We're not going to attach to anything.
|
||||
// Start as many containers as we want.
|
||||
return cli.startContainersWithoutAttachments(cmd.Args())
|
||||
return cli.startContainersWithoutAttachments(ctx, cmd.Args())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) startContainersWithoutAttachments(containers []string) error {
|
||||
func (cli *DockerCli) startContainersWithoutAttachments(ctx context.Context, containers []string) error {
|
||||
var failedContainers []string
|
||||
for _, container := range containers {
|
||||
if err := cli.client.ContainerStart(context.Background(), container); err != nil {
|
||||
if err := cli.client.ContainerStart(ctx, container); err != nil {
|
||||
fmt.Fprintf(cli.err, "%s\n", err)
|
||||
failedContainers = append(failedContainers, container)
|
||||
} else {
|
||||
|
|
|
@ -33,6 +33,8 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
showAll := len(names) == 0
|
||||
closeChan := make(chan error)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// monitorContainerEvents watches for container creation and removal (only
|
||||
// used when calling `docker stats` without arguments).
|
||||
monitorContainerEvents := func(started chan<- struct{}, c chan events.Message) {
|
||||
|
@ -41,7 +43,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
options := types.EventsOptions{
|
||||
Filters: f,
|
||||
}
|
||||
resBody, err := cli.client.Events(context.Background(), options)
|
||||
resBody, err := cli.client.Events(ctx, options)
|
||||
// Whether we successfully subscribed to events or not, we can now
|
||||
// unblock the main goroutine.
|
||||
close(started)
|
||||
|
@ -71,7 +73,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
options := types.ContainerListOptions{
|
||||
All: *all,
|
||||
}
|
||||
cs, err := cli.client.ContainerList(context.Background(), options)
|
||||
cs, err := cli.client.ContainerList(ctx, options)
|
||||
if err != nil {
|
||||
closeChan <- err
|
||||
}
|
||||
|
@ -79,7 +81,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
s := &containerStats{Name: container.ID[:12]}
|
||||
if cStats.add(s) {
|
||||
waitFirst.Add(1)
|
||||
go s.Collect(cli.client, !*noStream, waitFirst)
|
||||
go s.Collect(ctx, cli.client, !*noStream, waitFirst)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +98,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
s := &containerStats{Name: e.ID[:12]}
|
||||
if cStats.add(s) {
|
||||
waitFirst.Add(1)
|
||||
go s.Collect(cli.client, !*noStream, waitFirst)
|
||||
go s.Collect(ctx, cli.client, !*noStream, waitFirst)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -105,7 +107,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
s := &containerStats{Name: e.ID[:12]}
|
||||
if cStats.add(s) {
|
||||
waitFirst.Add(1)
|
||||
go s.Collect(cli.client, !*noStream, waitFirst)
|
||||
go s.Collect(ctx, cli.client, !*noStream, waitFirst)
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -131,7 +133,7 @@ func (cli *DockerCli) CmdStats(args ...string) error {
|
|||
s := &containerStats{Name: name}
|
||||
if cStats.add(s) {
|
||||
waitFirst.Add(1)
|
||||
go s.Collect(cli.client, !*noStream, waitFirst)
|
||||
go s.Collect(ctx, cli.client, !*noStream, waitFirst)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@ func (s *stats) isKnownContainer(cid string) (int, bool) {
|
|||
return -1, false
|
||||
}
|
||||
|
||||
func (s *containerStats) Collect(cli client.APIClient, streamStats bool, waitFirst *sync.WaitGroup) {
|
||||
func (s *containerStats) Collect(ctx context.Context, cli client.APIClient, streamStats bool, waitFirst *sync.WaitGroup) {
|
||||
logrus.Debugf("collecting stats for %s", s.Name)
|
||||
var (
|
||||
getFirst bool
|
||||
|
@ -80,7 +80,7 @@ func (s *containerStats) Collect(cli client.APIClient, streamStats bool, waitFir
|
|||
}
|
||||
}()
|
||||
|
||||
responseBody, err := cli.ContainerStats(context.Background(), s.Name, streamStats)
|
||||
responseBody, err := cli.ContainerStats(ctx, s.Name, streamStats)
|
||||
if err != nil {
|
||||
s.mu.Lock()
|
||||
s.err = err
|
||||
|
|
|
@ -22,9 +22,11 @@ func (cli *DockerCli) CmdStop(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, name := range cmd.Args() {
|
||||
if err := cli.client.ContainerStop(context.Background(), name, *nSeconds); err != nil {
|
||||
if err := cli.client.ContainerStop(ctx, name, *nSeconds); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
fmt.Fprintf(cli.out, "%s\n", name)
|
||||
|
|
|
@ -229,14 +229,14 @@ func (cli *DockerCli) getPassphraseRetriever() passphrase.Retriever {
|
|||
}
|
||||
}
|
||||
|
||||
func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Canonical, error) {
|
||||
func (cli *DockerCli) trustedReference(ctx context.Context, ref reference.NamedTagged) (reference.Canonical, error) {
|
||||
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Resolve the Auth config relevant for this server
|
||||
authConfig := cli.resolveAuthConfig(repoInfo.Index)
|
||||
authConfig := cli.resolveAuthConfig(ctx, repoInfo.Index)
|
||||
|
||||
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig, "pull")
|
||||
if err != nil {
|
||||
|
@ -262,14 +262,14 @@ func (cli *DockerCli) trustedReference(ref reference.NamedTagged) (reference.Can
|
|||
return reference.WithDigest(ref, r.digest)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) tagTrusted(trustedRef reference.Canonical, ref reference.NamedTagged) error {
|
||||
func (cli *DockerCli) tagTrusted(ctx context.Context, trustedRef reference.Canonical, ref reference.NamedTagged) error {
|
||||
fmt.Fprintf(cli.out, "Tagging %s as %s\n", trustedRef.String(), ref.String())
|
||||
|
||||
options := types.ImageTagOptions{
|
||||
Force: true,
|
||||
}
|
||||
|
||||
return cli.client.ImageTag(context.Background(), trustedRef.String(), ref.String(), options)
|
||||
return cli.client.ImageTag(ctx, trustedRef.String(), ref.String(), options)
|
||||
}
|
||||
|
||||
func notaryError(repoName string, err error) error {
|
||||
|
@ -302,7 +302,7 @@ func notaryError(repoName string, err error) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (cli *DockerCli) trustedPull(repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
||||
func (cli *DockerCli) trustedPull(ctx context.Context, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
||||
var refs []target
|
||||
|
||||
notaryRepo, err := cli.getNotaryRepository(repoInfo, authConfig, "pull")
|
||||
|
@ -364,7 +364,7 @@ func (cli *DockerCli) trustedPull(repoInfo *registry.RepositoryInfo, ref registr
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cli.imagePullPrivileged(authConfig, ref.String(), requestPrivilege, false); err != nil {
|
||||
if err := cli.imagePullPrivileged(ctx, authConfig, ref.String(), requestPrivilege, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -378,7 +378,7 @@ func (cli *DockerCli) trustedPull(repoInfo *registry.RepositoryInfo, ref registr
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cli.tagTrusted(trustedRef, tagged); err != nil {
|
||||
if err := cli.tagTrusted(ctx, trustedRef, tagged); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -386,8 +386,8 @@ func (cli *DockerCli) trustedPull(repoInfo *registry.RepositoryInfo, ref registr
|
|||
return nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) trustedPush(repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
||||
responseBody, err := cli.imagePushPrivileged(authConfig, ref.String(), requestPrivilege)
|
||||
func (cli *DockerCli) trustedPush(ctx context.Context, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
|
||||
responseBody, err := cli.imagePushPrivileged(ctx, authConfig, ref.String(), requestPrivilege)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -19,9 +19,11 @@ func (cli *DockerCli) CmdUnpause(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, name := range cmd.Args() {
|
||||
if err := cli.client.ContainerUnpause(context.Background(), name); err != nil {
|
||||
if err := cli.client.ContainerUnpause(ctx, name); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
fmt.Fprintf(cli.out, "%s\n", name)
|
||||
|
|
|
@ -99,10 +99,13 @@ func (cli *DockerCli) CmdUpdate(args ...string) error {
|
|||
RestartPolicy: restartPolicy,
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
names := cmd.Args()
|
||||
var errs []string
|
||||
|
||||
for _, name := range names {
|
||||
if err := cli.client.ContainerUpdate(context.Background(), name, updateConfig); err != nil {
|
||||
if err := cli.client.ContainerUpdate(ctx, name, updateConfig); err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
fmt.Fprintf(cli.out, "%s\n", name)
|
||||
|
|
|
@ -23,13 +23,13 @@ import (
|
|||
registrytypes "github.com/docker/engine-api/types/registry"
|
||||
)
|
||||
|
||||
func (cli *DockerCli) electAuthServer() string {
|
||||
func (cli *DockerCli) electAuthServer(ctx context.Context) string {
|
||||
// The daemon `/info` endpoint informs us of the default registry being
|
||||
// used. This is essential in cross-platforms environment, where for
|
||||
// example a Linux client might be interacting with a Windows daemon, hence
|
||||
// the default registry URL might be Windows specific.
|
||||
serverAddress := registry.IndexServer
|
||||
if info, err := cli.client.Info(context.Background()); err != nil {
|
||||
if info, err := cli.client.Info(ctx); err != nil {
|
||||
fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
|
||||
} else {
|
||||
serverAddress = info.IndexServerAddress
|
||||
|
@ -58,12 +58,12 @@ func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registrytypes.
|
|||
}
|
||||
}
|
||||
|
||||
func (cli *DockerCli) resizeTty(id string, isExec bool) {
|
||||
func (cli *DockerCli) resizeTty(ctx context.Context, id string, isExec bool) {
|
||||
height, width := cli.getTtySize()
|
||||
cli.resizeTtyTo(id, height, width, isExec)
|
||||
cli.resizeTtyTo(ctx, id, height, width, isExec)
|
||||
}
|
||||
|
||||
func (cli *DockerCli) resizeTtyTo(id string, height, width int, isExec bool) {
|
||||
func (cli *DockerCli) resizeTtyTo(ctx context.Context, id string, height, width int, isExec bool) {
|
||||
if height == 0 && width == 0 {
|
||||
return
|
||||
}
|
||||
|
@ -75,9 +75,9 @@ func (cli *DockerCli) resizeTtyTo(id string, height, width int, isExec bool) {
|
|||
|
||||
var err error
|
||||
if isExec {
|
||||
err = cli.client.ContainerExecResize(context.Background(), id, options)
|
||||
err = cli.client.ContainerExecResize(ctx, id, options)
|
||||
} else {
|
||||
err = cli.client.ContainerResize(context.Background(), id, options)
|
||||
err = cli.client.ContainerResize(ctx, id, options)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
@ -87,8 +87,8 @@ func (cli *DockerCli) resizeTtyTo(id string, height, width int, isExec bool) {
|
|||
|
||||
// getExitCode perform an inspect on the container. It returns
|
||||
// the running state and the exit code.
|
||||
func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
|
||||
c, err := cli.client.ContainerInspect(context.Background(), containerID)
|
||||
func (cli *DockerCli) getExitCode(ctx context.Context, containerID string) (bool, int, error) {
|
||||
c, err := cli.client.ContainerInspect(ctx, containerID)
|
||||
if err != nil {
|
||||
// If we can't connect, then the daemon probably died.
|
||||
if err != client.ErrConnectionFailed {
|
||||
|
@ -102,8 +102,8 @@ func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
|
|||
|
||||
// getExecExitCode perform an inspect on the exec command. It returns
|
||||
// the running state and the exit code.
|
||||
func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
|
||||
resp, err := cli.client.ContainerExecInspect(context.Background(), execID)
|
||||
func (cli *DockerCli) getExecExitCode(ctx context.Context, execID string) (bool, int, error) {
|
||||
resp, err := cli.client.ContainerExecInspect(ctx, execID)
|
||||
if err != nil {
|
||||
// If we can't connect, then the daemon probably died.
|
||||
if err != client.ErrConnectionFailed {
|
||||
|
@ -115,8 +115,8 @@ func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
|
|||
return resp.Running, resp.ExitCode, nil
|
||||
}
|
||||
|
||||
func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
|
||||
cli.resizeTty(id, isExec)
|
||||
func (cli *DockerCli) monitorTtySize(ctx context.Context, id string, isExec bool) error {
|
||||
cli.resizeTty(ctx, id, isExec)
|
||||
|
||||
if runtime.GOOS == "windows" {
|
||||
go func() {
|
||||
|
@ -126,7 +126,7 @@ func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
|
|||
h, w := cli.getTtySize()
|
||||
|
||||
if prevW != w || prevH != h {
|
||||
cli.resizeTty(id, isExec)
|
||||
cli.resizeTty(ctx, id, isExec)
|
||||
}
|
||||
prevH = h
|
||||
prevW = w
|
||||
|
@ -137,7 +137,7 @@ func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
|
|||
gosignal.Notify(sigchan, signal.SIGWINCH)
|
||||
go func() {
|
||||
for range sigchan {
|
||||
cli.resizeTty(id, isExec)
|
||||
cli.resizeTty(ctx, id, isExec)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
@ -185,10 +185,10 @@ func copyToFile(outfile string, r io.Reader) error {
|
|||
// resolveAuthConfig is like registry.ResolveAuthConfig, but if using the
|
||||
// default index, it uses the default index name for the daemon's platform,
|
||||
// not the client's platform.
|
||||
func (cli *DockerCli) resolveAuthConfig(index *registrytypes.IndexInfo) types.AuthConfig {
|
||||
func (cli *DockerCli) resolveAuthConfig(ctx context.Context, index *registrytypes.IndexInfo) types.AuthConfig {
|
||||
configKey := index.Name
|
||||
if index.Official {
|
||||
configKey = cli.electAuthServer()
|
||||
configKey = cli.electAuthServer(ctx)
|
||||
}
|
||||
|
||||
a, _ := getCredentials(cli.configFile, configKey)
|
||||
|
|
|
@ -110,8 +110,10 @@ func (cli *DockerCli) CmdVolumeInspect(args ...string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
inspectSearcher := func(name string) (interface{}, []byte, error) {
|
||||
i, err := cli.client.VolumeInspect(context.Background(), name)
|
||||
i, err := cli.client.VolumeInspect(ctx, name)
|
||||
return i, nil, err
|
||||
}
|
||||
|
||||
|
@ -161,8 +163,10 @@ func (cli *DockerCli) CmdVolumeRm(args ...string) error {
|
|||
|
||||
var status = 0
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
for _, name := range cmd.Args() {
|
||||
if err := cli.client.VolumeRemove(context.Background(), name); err != nil {
|
||||
if err := cli.client.VolumeRemove(ctx, name); err != nil {
|
||||
fmt.Fprintf(cli.err, "%s\n", err)
|
||||
status = 1
|
||||
continue
|
||||
|
|
|
@ -21,9 +21,11 @@ func (cli *DockerCli) CmdWait(args ...string) error {
|
|||
|
||||
cmd.ParseFlags(args, true)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
var errs []string
|
||||
for _, name := range cmd.Args() {
|
||||
status, err := cli.client.ContainerWait(context.Background(), name)
|
||||
status, err := cli.client.ContainerWait(ctx, name)
|
||||
if err != nil {
|
||||
errs = append(errs, err.Error())
|
||||
} else {
|
||||
|
|
Loading…
Add table
Reference in a new issue