2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-03-16 15:19:22 -04:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-12-06 18:34:25 -05:00
|
|
|
"github.com/docker/docker/api/client/inspect"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/client"
|
2015-03-24 23:57:23 -04:00
|
|
|
)
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
// CmdInspect displays low-level information on one or more containers, images or tasks.
|
2015-03-25 13:34:41 -04:00
|
|
|
//
|
2016-06-13 22:56:23 -04:00
|
|
|
// Usage: docker inspect [OPTIONS] CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdInspect(args ...string) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
cmd := Cli.Subcmd("inspect", []string{"CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]"}, Cli.DockerCommands["inspect"].Description, true)
|
2015-11-09 09:37:24 -05:00
|
|
|
tmplStr := cmd.String([]string{"f", "-format"}, "", "Format the output using the given go template")
|
2016-06-13 22:56:23 -04:00
|
|
|
inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image, container or task)")
|
2015-09-25 08:49:02 -04:00
|
|
|
size := cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes if the type is container")
|
2015-03-24 23:57:23 -04:00
|
|
|
cmd.Require(flag.Min, 1)
|
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
if *inspectType != "" && *inspectType != "container" && *inspectType != "image" && *inspectType != "task" {
|
2015-12-06 18:34:25 -05:00
|
|
|
return fmt.Errorf("%q is not a valid value for --type", *inspectType)
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:57:57 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-05-10 10:58:07 -04:00
|
|
|
var elementSearcher inspect.GetRefFunc
|
2015-12-06 18:34:25 -05:00
|
|
|
switch *inspectType {
|
|
|
|
case "container":
|
2016-05-21 09:57:57 -04:00
|
|
|
elementSearcher = cli.inspectContainers(ctx, *size)
|
2015-12-07 22:04:38 -05:00
|
|
|
case "image":
|
2016-05-21 09:57:57 -04:00
|
|
|
elementSearcher = cli.inspectImages(ctx, *size)
|
2016-06-13 22:56:23 -04:00
|
|
|
case "task":
|
|
|
|
if *size {
|
|
|
|
fmt.Fprintln(cli.err, "WARNING: --size ignored for tasks")
|
|
|
|
}
|
|
|
|
elementSearcher = cli.inspectTasks(ctx)
|
2015-12-06 18:34:25 -05:00
|
|
|
default:
|
2016-05-21 09:57:57 -04:00
|
|
|
elementSearcher = cli.inspectAll(ctx, *size)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-05-10 10:58:07 -04:00
|
|
|
return inspect.Inspect(cli.out, cmd.Args(), *tmplStr, elementSearcher)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
2015-09-25 08:49:02 -04:00
|
|
|
|
2016-05-10 10:58:07 -04:00
|
|
|
func (cli *DockerCli) inspectContainers(ctx context.Context, getSize bool) inspect.GetRefFunc {
|
2015-12-07 22:04:38 -05:00
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
2016-05-21 09:57:57 -04:00
|
|
|
return cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-26 10:47:31 -04:00
|
|
|
|
2016-05-10 10:58:07 -04:00
|
|
|
func (cli *DockerCli) inspectImages(ctx context.Context, getSize bool) inspect.GetRefFunc {
|
2015-12-07 22:04:38 -05:00
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
2016-05-21 09:57:57 -04:00
|
|
|
return cli.client.ImageInspectWithRaw(ctx, ref, getSize)
|
2015-12-06 18:34:25 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
func (cli *DockerCli) inspectTasks(ctx context.Context) inspect.GetRefFunc {
|
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
|
|
|
return cli.client.TaskInspectWithRaw(ctx, ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 10:58:07 -04:00
|
|
|
func (cli *DockerCli) inspectAll(ctx context.Context, getSize bool) inspect.GetRefFunc {
|
2015-12-07 22:04:38 -05:00
|
|
|
return func(ref string) (interface{}, []byte, error) {
|
2016-05-21 09:57:57 -04:00
|
|
|
c, rawContainer, err := cli.client.ContainerInspectWithRaw(ctx, ref, getSize)
|
2015-12-07 22:04:38 -05:00
|
|
|
if err != nil {
|
2015-12-06 18:34:25 -05:00
|
|
|
// Search for image with that id if a container doesn't exist.
|
2016-01-04 19:05:26 -05:00
|
|
|
if client.IsErrContainerNotFound(err) {
|
2016-05-21 09:57:57 -04:00
|
|
|
i, rawImage, err := cli.client.ImageInspectWithRaw(ctx, ref, getSize)
|
2015-12-07 22:04:38 -05:00
|
|
|
if err != nil {
|
2016-01-04 19:05:26 -05:00
|
|
|
if client.IsErrImageNotFound(err) {
|
2016-06-13 22:56:23 -04:00
|
|
|
// Search for task with that id if an image doesn't exists.
|
|
|
|
t, rawTask, err := cli.client.TaskInspectWithRaw(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("Error: No such image, container or task: %s", ref)
|
|
|
|
}
|
|
|
|
if getSize {
|
|
|
|
fmt.Fprintln(cli.err, "WARNING: --size ignored for tasks")
|
|
|
|
}
|
|
|
|
return t, rawTask, nil
|
2015-05-11 20:39:21 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return nil, nil, err
|
2015-03-26 15:43:00 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return i, rawImage, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return nil, nil, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-12-07 22:04:38 -05:00
|
|
|
return c, rawContainer, err
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
}
|