Skip to content

Commit

Permalink
Flag Addition: --type flag added for docker inspect command. Permissi…
Browse files Browse the repository at this point in the history
…ble values are image or container.

Signed-off-by: Shishir Mahajan <shishir.mahajan@redhat.com>
  • Loading branch information
Shishir Mahajan committed May 18, 2015
1 parent aac645a commit 27491c4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
40 changes: 35 additions & 5 deletions api/client/inspect.go
Expand Up @@ -18,42 +18,72 @@ import (
func (cli *DockerCli) CmdInspect(args ...string) error {
cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template")
inspectType := cmd.String([]string{"-type"}, "", "Return JSON for specified type, (e.g image or container)")

cmd.Require(flag.Min, 1)

cmd.ParseFlags(args, true)

var tmpl *template.Template
var err error
var obj []byte

if *tmplStr != "" {
var err error
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
return StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()}
}
}

if *inspectType != "" && *inspectType != "container" && *inspectType != "image" {
return fmt.Errorf("%s is not a valid value for --type", *inspectType)
}

indented := new(bytes.Buffer)
indented.WriteString("[\n")
status := 0
isImage := false
isContainer := false

for _, name := range cmd.Args() {
obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
if err != nil {

isContainer = false

if *inspectType == "" || *inspectType == "container" {
obj, _, err = readBody(cli.call("GET", "/containers/"+name+"/json", nil, nil))
if err == nil {
isContainer = true
} else {
if *inspectType == "container" {
if strings.Contains(err.Error(), "No such") {
fmt.Fprintf(cli.err, "Error: No such container: %s\n", name)
} else {
fmt.Fprintf(cli.err, "%s", err)
}
status = 1
continue
}
}

}

if !isContainer {
obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, nil))
isImage = true
if err != nil {
if strings.Contains(err.Error(), "No such") {
fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
fmt.Fprintf(cli.err, "Error: No such image: %s\n", name)
} else {
fmt.Fprintf(cli.err, "%s", err)
}
status = 1
continue
}

}

if tmpl == nil {
if err = json.Indent(indented, obj, "", " "); err != nil {
if err := json.Indent(indented, obj, "", " "); err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
Expand Down
4 changes: 2 additions & 2 deletions contrib/completion/bash/docker
Expand Up @@ -514,14 +514,14 @@ _docker_info() {

_docker_inspect() {
case "$prev" in
--format|-f)
--format|-f|--type)
return
;;
esac

case "$cur" in
-*)
COMPREPLY=( $( compgen -W "--format -f --help" -- "$cur" ) )
COMPREPLY=( $( compgen -W "--format -f --type --help" -- "$cur" ) )
;;
*)
__docker_containers_and_images
Expand Down
77 changes: 77 additions & 0 deletions docs/man/docker-inspect.1.md
Expand Up @@ -24,8 +24,85 @@ each result.
**-f**, **--format**=""
Format the output using the given go template.

**--type**=""
Return JSON for specified type, permissible values are image or container

# EXAMPLES

## Getting information on an image where image name conflict with the container name.
## e,g both image and container are named rhel7.

$ docker inspect --type=image rhel7
[
{
"Id": "fe01a428b9d9de35d29531e9994157978e8c48fa693e1bf1d221dffbbb67b170",
"Parent": "10acc31def5d6f249b548e01e8ffbaccfd61af0240c17315a7ad393d022c5ca2",
"Comment": "",
"Created": "2015-04-22T19:54:08.324108658Z",
"Container": "bc5cbf25339075df52055146149f68f7fb01a712da961b3d8564f9f643f576d0",
"ContainerConfig": {
"Hostname": "bc5cbf253390",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"PortSpecs": null,
"ExposedPorts": null,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"container=docker"
],
"Cmd": [
"/bin/bash"
],
"Image": "rhel7",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "1.7.0-dev",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"PortSpecs": null,
"ExposedPorts": null,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"container=docker"
],
"Cmd": [
"/bin/bash"
],
"Image": "",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 12,
"VirtualSize": 154114329
}
]

## Getting information on a container

To get information on a container use its ID or instance name:
Expand Down
2 changes: 2 additions & 0 deletions docs/sources/reference/commandline/cli.md
Expand Up @@ -1509,6 +1509,8 @@ ensure we know how your setup is configured.
Return low-level information on a container or image

-f, --format="" Format the output using the given go template
--type="" Return JSON for specified type, permissible values are
image or container

By default, this will render all results in a JSON array. If a format is
specified, the given template will be executed for each result.
Expand Down

0 comments on commit 27491c4

Please sign in to comment.