Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort or order results docker ps --format?

Tags:

I haven't found any way to order my results when using docker ps

In my case I want to order by .Ports

docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"

How do I order the result?

like image 300
Theo Avatar asked Sep 12 '17 09:09

Theo


People also ask

What is docker ps command used for?

The 'docker ps' is a Docker command to list the running containers by default; however, we can use different flags to get the list of other containers that are in stopped or exited status. We can also manipulate the output as per our requirement using flags.

What is the default format of docker inspect output?

By default, docker inspect will render results in a JSON array.

Which port is my docker container running on?

The left-hand side of the port number mapping is the Docker host port to map to and the right-hand side is the Docker container port number. When you open the browser and navigate to the Docker host on port 8080, you will see Jenkins up and running.

What docker is my image running?

You can check with this command systemctl status docker it will show the status of the docker. If you want to start you can use systemctl start docker instead of systemctl you can try also with service , service docker status and service docker start respectively.


2 Answers

If it's enough to simply sort by output column, you can use the following:

 docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3 )

I also added a code for skipping the table headers and sorting only ps output data.

like image 121
Igor Maximov Avatar answered Oct 04 '22 09:10

Igor Maximov


Format and Order docker ps

List containers

docker ps

Synopsis

docker ps [--format="TEMPLATE"]

--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.
     .Names - Container names.
     .Labels - All labels assigned to the container.
     .Label - Value of a specific label for this container. For example {{.Label "com.docker.swarm.cpu"}}
     .Mounts - Names of the volumes mounted in this container.

Display containers with their commands

docker ps --format "{{.ID}}: {{.Command}}"

Display containers with their labels in a table

docker ps --format "table {{.ID}}\t{{.Labels}}"

Display containers with their node label in a table

docker ps --format 'table {{.ID}}\t{{(.Label "com.docker.swarm.node")}}'
like image 45
Jinna Balu Avatar answered Oct 04 '22 09:10

Jinna Balu