如何按名称`docker service ls`排序

时间:2017-02-22 18:30:52

标签: bash docker

我使用Docker Swarm 1.13.1,按名称(A-Z)列出服务非常实用。

我不认为Docker原生支持这一壮举所以我期待一些awk,sort和其他小黑客。

到目前为止,这是我能得到的最接近的:

root@N01-par1-dev-e:~/deploy-setup# docker service ls | sort -k2,2 -k1
yru8n6zxmp08  consul            global      4/4       consul:latest
1lb98n83fttr  mysql             replicated  1/1       devmtl/percona-fire:5.7.16-10-1
ID            NAME              MODE        REPLICAS  IMAGE
i75t4zef8x8c  papertrail        global      4/4       gliderlabs/logspout:latest
4s14t7f13fp8  portainer         replicated  1/1       portainer/portainer:1.11.3
8wojufyq3dwn  resilio           global      4/4       devmtl/resilio-fire:2.4.4-alpine-0
vn8fhhfvb6pw  sematext          global      4/4       sematext/sematext-agent-docker:1.31.11
mwxtlff6jyl5  thm-s0001-iamfoo  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
o8p4q85axkwy  thm-s0002-iambar  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
wedrfqzkg3zp  thm-s0003-caply   replicated  1/1       devmtl/nginx-fire:1.10.3-alpine-0
ql9yapc0brnk  thm-s0003-valgo   replicated  1/1       devmtl/caddy:0.9.5-alpine-0
vu3jhyo71oos  traefik           replicated  1/1       traefik:v1.1.2-alpine

或......

root@N01-par1-dev-e:~/deploy-setup# docker service ls | tail -n +2 |  sort -k2,2 -k1
yru8n6zxmp08  consul            global      4/4       consul:latest
1lb98n83fttr  mysql             replicated  1/1       devmtl/percona-fire:5.7.16-10-1
i75t4zef8x8c  papertrail        global      4/4       gliderlabs/logspout:latest
4s14t7f13fp8  portainer         replicated  1/1       portainer/portainer:1.11.3
8wojufyq3dwn  resilio           global      4/4       devmtl/resilio-fire:2.4.4-alpine-0
vn8fhhfvb6pw  sematext          global      4/4       sematext/sematext-agent-docker:1.31.11
mwxtlff6jyl5  thm-s0001-iamfoo  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
o8p4q85axkwy  thm-s0002-iambar  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
wedrfqzkg3zp  thm-s0003-caply   replicated  1/1       devmtl/nginx-fire:1.10.3-alpine-0
ql9yapc0brnk  thm-s0003-valgo   replicated  1/1       devmtl/caddy:0.9.5-alpine-0
vu3jhyo71oos  traefik           replicated  1/1       traefik:v1.1.2-alpine

或使用2个命令:

root@N01-par1-dev-e:~/deploy-setup# echo; \
> docker service ls | head -n 1; \
> docker service ls | tail -n +2 |  sort -k2,2 -k1; echo;

ID            NAME              MODE        REPLICAS  IMAGE
yru8n6zxmp08  consul            global      4/4       consul:latest
1lb98n83fttr  mysql             replicated  1/1       devmtl/percona-fire:5.7.16-10-1
i75t4zef8x8c  papertrail        global      4/4       gliderlabs/logspout:latest
4s14t7f13fp8  portainer         replicated  1/1       portainer/portainer:1.11.3
8wojufyq3dwn  resilio           global      4/4       devmtl/resilio-fire:2.4.4-alpine-0
vn8fhhfvb6pw  sematext          global      4/4       sematext/sematext-agent-docker:1.31.11
mwxtlff6jyl5  thm-s0001-iamfoo  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
o8p4q85axkwy  thm-s0002-iambar  replicated  1/1       devmtl/iam-this-ctn:1.0.1-alpine-0
wedrfqzkg3zp  thm-s0003-caply   replicated  1/1       devmtl/nginx-fire:1.10.3-alpine-0
ql9yapc0brnk  thm-s0003-valgo   replicated  1/1       devmtl/caddy:0.9.5-alpine-0
vu3jhyo71oos  traefik           replicated  1/1       traefik:v1.1.2-alpine

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以使用awkNAME列中提取名称,并根据此顺序对生成的名称数组进行排序以打印行:

对于docker service lsnamesmode的位置会存储到名称的子字符串中。然后使用asort处理键组(包含名称)的排序:

docker service ls | awk ' 
{
    if (NR == 1) {
        print $0
        names=index($0,"NAME")
        mode=index($0,"MODE")
    }
    else{
        key[NR-2]=substr($0,names,mode-1-names) "\t" NR
        value[NR-2]=$0
    }
}
END {
    asort(key)
    for (i=1; i<=(NR-1); i++) {
        split(key[i],a,"\t")
        print value[a[2]-2]
    }
}'

根据排序key数组的顺序打印结果,该数组包含情侣[name]\t[index]。索引用于获取要打印的行。

如果是docker ps -a,则NAMES是最后一列,因此我们在此处为100的长度采用固定值:

docker ps -a | awk ' 
{
    if (NR == 1) {
        print $0
        names=index($0,"NAME")
    }
    else{
        key[NR-2]=substr($0, names, 100) "\t" NR
        value[NR-2]=$0
    }
}
END {
    asort(key)
    for (i=1; i<=(NR-1); i++) {
        split(key[i],a,"\t")
        print value[a[2]-2]
    }
}'

检查this pos t以获取asort的另一个例子