How to extract mapped ports from `docker ps`'s output
I'm trying to get all the ports used by a Docker container with this command:
sudo docker ps | tail -n1
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
When I ran it in terminal I got I want.
$ sudo docker ps | tail -n1 | awk '{print $12}'
0.0.0.0:32783->5432/tcp,
But I need all the mapped ports. Is it possible to make a shell script like this:
#!/bin/bash
paramnum=$(sudo docker ps | grep $lasttimestamp | wc -w);
text=$(sudo docker ps | tail -n1);
begin=($paramnum-4);
end=($paramnum-1);
for (( i=$end; i>=$begin; i--))
do
t="awk '{print $"$i"}'";
eval "echo $text | $t";
done
I've been hanging around for few hours. Please help, or suggest how to get an output like below.
0.0.0.0:32782->10523/tcp
0.0.0.0:32783->5432/tcp,
0.0.0.0:8443->8443/tcp,
0.0.0.0:8080->8080/tcp,
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