Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -format to 'docker inspect' #2921

Merged
merged 3 commits into from Dec 2, 2013
Merged

Add -format to 'docker inspect' #2921

merged 3 commits into from Dec 2, 2013

Conversation

dustin
Copy link
Contributor

@dustin dustin commented Nov 27, 2013

This makes it a lot easier to script with docker instances as one can
ask for details about running instances more easily without having to
have additional JSON processing tools installed.

Note that I did minimal work in separating the current JSON output from the templated output.

#734

Closes #734

@vieux
Copy link
Contributor

vieux commented Nov 27, 2013

Can you give us some example ?

@dustin
Copy link
Contributor Author

dustin commented Nov 27, 2013

I imagine we'd want a proper docs page with some recipes, but, for example:

% rs=`docker run -P -d dustin/r-studio-server`

Now if you want to know the address it's bound to:

% docker inspect -format='{{.NetworkSettings.IPAddress}}' $rs
172.17.0.3

The structure of the networking section is, IMO, awkward, but workable (port maps to an array of objects). If you wanted a textual list of all portmappings, you could do this:

% docker inspect -format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' $rs

which yields

8787/tcp -> 49153

If you wanted the specific external port for that, you could do this:

% docker inspect -format='{{(index (index .NetworkSettings.Ports "8787/tcp") 0).HostPort}}' $rs

yields

49153

That one is particularly awkward, but, again, it's how the structure was defined.

@dustin
Copy link
Contributor Author

dustin commented Nov 28, 2013

The previous commit update just adds a newline to the error message when your template can't be parsed. No functional change.

@tianon
Copy link
Member

tianon commented Nov 28, 2013

Definitely huge +1 from me! I've been wanting someone to implement this one for a while. :)

Getting at these values with nothing but bash as we are right now is cumbersome or requires extra tooling I don't even have installed (and can't justify for just this, especially given that the scripts I write usually run on the machines of other people who definitely won't install jq just for one or two little scripts).

@crosbymichael
Copy link
Contributor

@dustin Can you add the examples to the docs page for inspect?

@dustin
Copy link
Contributor Author

dustin commented Nov 29, 2013

@crosbymichael I'm happy to provide docs. Do you want them in docs/sources/commandline/cli.rst?

@crosbymichael
Copy link
Contributor

Yes, a few examples under this command in cli.rst would be perfect.

var err error
if tmpl, err = template.New("").Parse(*tmplStr); err != nil {
fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
return &utils.StatusError{Status: 64}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Status's type changed to be a string with a new field StatusCode which is an int.

This makes it a lot easier to script with docker instances as one can
ask for details about running instances more easily without having to
have additional JSON processing tools installed.

#734
@dustin
Copy link
Contributor Author

dustin commented Dec 2, 2013

rebased

@crosbymichael
Copy link
Contributor

LGTM

1 similar comment
@vieux
Copy link
Contributor

vieux commented Dec 2, 2013

LGTM

vieux pushed a commit that referenced this pull request Dec 2, 2013
Add -format to 'docker inspect'
@vieux vieux merged commit 4fdc117 into moby:master Dec 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optional formatting of "docker inspect" with selector
4 participants