I am parsing output from the docker container ls --format json command and trying to process and format the content to keep only what I need.
/usr/bin/docker container ls --format json | jq ". | select(.Labels | split(\",\") | index(\"com.docker.compose.project=$1\")) | { id: .ID, ports: .Ports | split(\",\"), labels: .Labels | split(\",\") }" | jq -csThis transforms the source returned by docker container ls (abridged):
[ {"Command": "\"/docker-entrypoint.…\"","CreatedAt": "2025-09-05 23:04:34 +0200 CEST","ID": "4d3f1dbc7ddc","Labels": "com.docker.compose.service=arc,org.opencontainers.image.version=22.04,com.docker.compose.container-number=1,com.docker.compose.project=dcm4chee-arc","Ports": "2575/tcp, 8443/tcp, 9990/tcp, 9993/tcp, 0.0.0.0:11112->11112/tcp, :::11112->11112/tcp, 0.0.0.0:8500->8080/tcp, [::]:8500->8080/tcp","RunningFor": "5 months ago" }, {"Command": "\"/docker-entrypoint.…\"","CreatedAt": "2025-05-23 01:39:16 +0200 CEST","ID": "b92b7425ad5b","Labels": "com.docker.compose.container-number=1,com.docker.compose.project.working_dir=/opt/dcm4chee-arc,com.docker.compose.project.config_files=/opt/dcm4chee-arc/docker-compose.yml,com.docker.compose.project=dcm4chee-arc","Ports": "0.0.0.0:18081->18080/tcp, [::]:18081->18080/tcp","RunningFor": "8 months ago" }, {"Command": "\"docker-entrypoint.s…\"","CreatedAt": "2025-05-23 01:39:16 +0200 CEST","ID": "2a26e350e3c0","Labels": "com.docker.compose.image=sha256:bfb24fcb8571d58fca08d4bcb5e85c5f708646c2d5b04621941f4e2a78ff8d9d,com.docker.compose.project=dcm4chee-arc,com.docker.compose.service=db,com.docker.compose.version=2.32.4,com.docker.compose.config-hash=e2be5ab506c6ae08c6f6799898988e307babf2732a5e226fa30233179fdcd49a","Ports": "0.0.0.0:5432->5432/tcp, :::5432->5432/tcp","RunningFor": "8 months ago" }, {"Command": "\"/docker-entrypoint.…\"","CreatedAt": "2025-05-23 01:39:16 +0200 CEST","ID": "2d821174c376","Labels": "com.docker.compose.version=2.32.4,com.docker.compose.container-number=1,com.docker.compose.oneoff=False,com.docker.compose.project=dcm4chee-arc","Ports": "0.0.0.0:389->389/tcp, :::389->389/tcp","RunningFor": "8 months ago" }]to:
[ {"id": "4d3f1dbc7ddc","ports": ["2575/tcp"," 8443/tcp"," 9990/tcp"," 9993/tcp"," 0.0.0.0:11112->11112/tcp"," :::11112->11112/tcp"," 0.0.0.0:8500->8080/tcp"," [::]:8500->8080/tcp" ],"labels": ["com.docker.compose.container-number=1","com.docker.compose.service=arc","org.opencontainers.image.version=22.04" ] }, {"id": "b92b7425ad5b","ports": ["0.0.0.0:18081->18080/tcp"," [::]:18081->18080/tcp" ],"labels": ["com.docker.compose.project.working_dir=/opt/dcm4chee-arc","com.docker.compose.container-number=1","com.docker.compose.project.config_files=/opt/dcm4chee-arc/docker-compose.yml","com.docker.compose.project=dcm4chee-arc" ] }, {"id": "2a26e350e3c0","ports": ["0.0.0.0:5432->5432/tcp"," :::5432->5432/tcp" ],"labels": ["com.docker.compose.project=dcm4chee-arc","com.docker.compose.version=2.32.4","com.docker.compose.image=sha256:bfb24fcb8571d58fca08d4bcb5e85c5f708646c2d5b04621941f4e2a78ff8d9d","com.docker.compose.config-hash=e2be5ab506c6ae08c6f6799898988e307babf2732a5e226fa30233179fdcd49a","com.docker.compose.project=dcm4chee-arc" ] }, {"id": "2d821174c376","ports": ["0.0.0.0:389->389/tcp"," :::389->389/tcp" ],"labels": ["com.docker.compose.oneoff=False","com.docker.compose.project=dcm4chee-arc","com.docker.compose.version=2.32.4","com.docker.compose.container-number=1" ] }]Now, I cannot figure out how to perform operations:
on each element of the ports array individually, like triming spaces, selecting some elements and spliting the string to generate a map of ports like
{ "8080": "8500", "11112": "11112" }(only keeping mapped ports)on each element of the labels array to generate a map like
{ "com.docker.compose.oneoff": "False","com.docker.compose.project": "dcm4chee-arc","com.docker.compose.version": "2.32.4","com.docker.compose.container-number": "1"}
Is this achievable? If so, how?