如何从`docker ps`的输出中提取映射的端口

R.C*_*sit 17 command-line bash docker

我正在尝试使用以下命令获取 Docker 容器使用的所有端口:

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
Run Code Online (Sandbox Code Playgroud)

当我在终端中运行它时,我得到了我想要的。

$ sudo docker ps | tail -n1 | awk '{print $12}'
0.0.0.0:32783->5432/tcp,
Run Code Online (Sandbox Code Playgroud)

但我需要所有映射的端口。是否可以制作这样的shell脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)

我已经闲逛了几个小时。请帮助,或建议如何获得如下输出。

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,
Run Code Online (Sandbox Code Playgroud)

c0r*_*0rp 24

根据 docker 手册页,您可以尝试以下操作:

sudo docker ps --format "{{.Ports}}"
Run Code Online (Sandbox Code Playgroud)

或者如果您还需要身份证:

sudo docker ps --format "{{.ID}}: {{.Ports}}"
Run Code Online (Sandbox Code Playgroud)

文档中没有提到它,但是要格式化输出,您必须使用{{}}.

引自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"
Run Code Online (Sandbox Code Playgroud)

码头工人 1.10.3

现在,有一些关于{{}}大括号的有用说明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
Run Code Online (Sandbox Code Playgroud)


kos*_*kos 3

使用 Perl:

sudo docker ps | \
tail -n 1 | \
perl -lae '$,="\n";foreach(@F){/tcp,?$/&&push(@x,$_)};print(@x)'
Run Code Online (Sandbox Code Playgroud)
  • -l:启用自动换行处理。它有两种不同的效果。首先,当与 -n 或 -p 一起使用时,它会自动删除 $/(输入记录分隔符)。其次,它指定 $\ (输出记录分隔符)具有 octnum 值,以便任何打印语句都将重新添加该分隔符。如果省略 octnum,则将 $\ 设置为 $/ 的当前值。
  • -a:与 -n 或 -p 一起使用时打开自动分割模式。对 @F 数组的隐式 split 命令是由 -n 或 -p 生成的隐式 while 循环内的第一件事。
  • -e:可用于输入一行程序。
  • $,="\n":将输出字段分隔符设置为\n
  • foreach(@F){/tcp,?$/&&push(@x,$_)}:对于 的每个元素@F,如果该元素以 结尾,tcp后跟一个可选的,,则将该元素添加到 的末尾@x
  • print(@x):打印每个元素,@x后跟输出字段分隔符;
% 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
Run Code Online (Sandbox Code Playgroud)