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