16

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,
3

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 with tcp 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
  • I have neither known Perl anymore but I really appreciate to you for this. How to try this code with in "docker ps". – R.Chonpisit Nov 18 '15 at 10:16
  • @R.Chonpisit The first line I wrote is meant to be copy-pasted-run on the terminal, the remainder is just an explanation of what it does: sudo docker ps | perl -lane '$next=<>&&redo;$,="\n";foreach(@F){/tcp,?$/&&push(@list, $_)};print(@list);last' – kos Nov 18 '15 at 10:26
  • @R.Chonpisit The previous version was wrong, see the update. This should work fine: sudo docker ps | tail -n 1 | perl -lae '$,="\n";foreach(@F){/tcp,?$/&&push(@x,$_)};print(@x)' – kos Nov 18 '15 at 11:12
21

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
  • hellingly yes​​ – Etki Nov 18 '15 at 16:52
2

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
  • I try your code on this way. echo "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}' but it produced nothing. – R.Chonpisit Nov 18 '15 at 9:58
  • Sure not, ps has another output. – A.B. Nov 18 '15 at 10:16
0

docker ps -q | xargs -n1 docker port

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.