How to extract mapped ports from `docker ps`'s output
According to the docker man pages you could try this:
sudo docker ps --format "{{.Ports}}"
or if you also need ID:
sudo docker ps --format "{{.ID}}: {{.Ports}}"
It is not mentioned in the documentation, but to format output you have to use {{}}
.
Quote from man docker-ps
:
--format="TEMPLATE"
Pretty-print containers using a Go template.
Valid placeholders:
.ID - Container ID
.Image - Image ID
.Command - Quoted command
.CreatedAt - Time when the container was created.
.RunningFor - Elapsed time since the container was started.
.Ports - Exposed ports.
.Status - Container status.
.Size - Container disk size.
.Labels - All labels asigned to the container.
.Label - Value of a specific label for this container. For example .Label "com.docker.swarm.cpu"
Docker 1.10.3
Nowadays, there are a few useful notes about {{}}
braces in man docker-ps
:
--format="TEMPLATE"
Pretty-print containers using a Go template.
Valid placeholders:
.ID - Container ID
.Image - Image ID
.Command - Quoted command
.CreatedAt - Time when the container was created.
.RunningFor - Elapsed time since the container was started.
.Ports - Exposed ports.
.Status - Container status.
.Size - Container disk size.
.Labels - All labels assigned to the container.
.Label - Value of a specific label for this container.
For example {{.Label "com.docker.swarm.cpu"}}
Display containers with their commands
# docker ps --format "{{.ID}}: {{.Command}}"
a87ecb4f327c: /bin/sh -c #(nop) MA
01946d9d34d8: /bin/sh -c #(nop) MA
c1d3b0166030: /bin/sh -c yum -y up
41d50ecd2f57: /bin/sh -c #(nop) MA
Display containers with their labels in a table
# docker ps --format "table {{.ID}}\t{{.Labels}}"
CONTAINER ID LABELS
a87ecb4f327c com.docker.swarm.node=ubuntu,com.docker.swarm.storage=ssd
01946d9d34d8
c1d3b0166030 com.docker.swarm.node=debian,com.docker.swarm.cpu=6
41d50ecd2f57 com.docker.swarm.node=fedora,com.docker.swarm.cpu=3,com.docker.swarm.storage=ssd
Display containers with their node label in a table
# docker ps --format 'table {{.ID}}\t{{(.Label "com.docker.swarm.node")}}'
CONTAINER ID NODE
a87ecb4f327c ubuntu
01946d9d34d8
c1d3b0166030 debian
41d50ecd2f57 fedora
Using Perl:
sudo docker ps | \
tail -n 1 | \
perl -lae '$,="\n";foreach(@F){/tcp,?$/&&push(@x,$_)};print(@x)'
-l
: enables automatic line-ending processing. It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on. If octnum is omitted, sets $\ to the current value of $/.-a
: turns on autosplit mode when used with a -n or -p. An implicit split command to the @F array is done as the first thing inside the implicit while loop produced by the -n or -p.-e
: may be used to enter one line of program.$,="\n"
: sets the output field separator to\n
;foreach(@F){/tcp,?$/&&push(@x,$_)}
: for each element of@F
, if the element ends withtcp
followed by an optional,
adds the element at the end of@x
;print(@x)
: prints each element of@x
followed by the output field separator;
% cat in
29ba3137f3e2 java8/local:latest "/bin/bash" 3 hours ago Up 3 hours foo/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp DEMO-20151118124751
29ba3137f3e2 java8/local:latest "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp DEMO-20151118124751
29ba3137f3e2 java8/local:latest "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp DEMO-20151118124751
% tail -n 1 in | perl -lae '$,="\n";foreach(@F){/tcp,?$/&&push(@x,$_)};print(@x)'
0.0.0.0:8080->8080/tcp,
0.0.0.0:8443->8443/tcp,
0.0.0.0:32783->5432/tcp,
0.0.0.0:32782->10523/tcp
Using awk
with the field delimiter {2,}
. Why {2,}
? the output of ps
uses more than one space as separator between the columns. Means, we can use this as separator for the awk
command.
awk -F" {2,}" '{print $6}'
or for your ps
command
sudo docker ps | tail -n1 | awk -F" {2,}" '{print $6}'
or without tail
sudo docker ps | awk -F" {2,}" 'END {print $6}'
Sample output
% <<<'29ba3137f3e2 java8/local:latest "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp DEMO-20151118124751' \
awk -F" {2,}" '{print $6}'
0.0.0.0:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp
or
% <<<'29ba3137f3e2 java8/local:latest "/bin/bash" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp, 0.0.0.0:8443->8443/tcp, 0.0.0.0:32783->5432/tcp, 0.0.0.0:32782->10523/tcp DEMO-20151118124751' \
awk -F" {2,}" '{print $6}' |\
tr ' ' '\n'
0.0.0.0:8080->8080/tcp,
0.0.0.0:8443->8443/tcp,
0.0.0.0:32783->5432/tcp,
0.0.0.0:32782->10523/tcp