--A memo that displays only a part of the result of docker ps
--Summary: I can't write it, so read Official Documentation. Especially [Filtering](https://docs.docker.com/engine/reference/commandline/ps/ # filtering).
Help display
$ docker ps -h
Flag shorthand -h has been deprecated, please use --help
Usage:  docker ps [OPTIONS]
List containers
Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes
hello-Create 5 world containers
$ for i in `seq 1 5`; do  docker run hello-world; done
$ docker ps -a -n 5
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             cool_hertz
e6d3616b5a67   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             clever_chatelet
bd7678b95d36   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             zealous_maxwell
e68109080f0b   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             agitated_fermi
d59d057989ce   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             vigilant_kilby
Not displayed because all are exited
$ docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS                      PORTS     NAMES
Show what id is 236858d287f8
$ docker ps -a -f id=bcca4b50f397
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             cool_hertz
NAME is cool_Show what is hertz
$ docker ps -a -f name=cool_hertz
CONTAINER ID   IMAGE         COMMAND    CREATED              STATUS                          PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   About a minute ago   Exited (0) About a minute ago             cool_hertz
Show NAMEs starting with c
$ docker ps -a -f name=^c.*
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             cool_hertz
e6d3616b5a67   hello-world   "/hello"   2 minutes ago   Exited (0) 2 minutes ago             clever_chatelet
CONTAINER although NAME starts with c_Output ID
$ docker ps -a -f name=^c.* -q
bcca4b50f397
e6d3616b5a67
--See the end of the article about PlaceHolder in format.
Output IMAGE even though NAME starts with c
$ docker ps -a -f name=^c.* --format '{{.Image}}'
hello-world
hello-world
--See the end of the article about PlaceHolder in format.
CONTAINER_Show only ID, NAMES and PORTS columns
$ docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
CONTAINER ID   NAMES                  PORTS
bcca4b50f397   cool_hertz             
e6d3616b5a67   clever_chatelet        
bd7678b95d36   zealous_maxwell        
e68109080f0b   agitated_fermi         
d59d057989ce   vigilant_kilby  
NAME=zealous_Before maxwell
$ docker ps -a -f before=zealous_maxwell
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
e68109080f0b   hello-world   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             agitated_fermi
d59d057989ce   hello-world   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             vigilant_kilby
NAME=zealous_After maxwell
$ docker ps -a -f since=zealous_maxwell 
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             cool_hertz
e6d3616b5a67   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             clever_chatelet
Last made container
docker ps -l
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             cool_hertz
From the last to the third
$ docker ps -a -n 3
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
bcca4b50f397   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             cool_hertz
e6d3616b5a67   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             clever_chatelet
bd7678b95d36   hello-world   "/hello"   5 minutes ago   Exited (0) 5 minutes ago             zealous_maxwell
--Reference: Query Label
bash:com.example.is-Everything that has a beta label
$ docker ps --filter "label=com.example.is-beta"
color label value is blue
$ docker ps --filter "label=color=blue"
| Placeholder | Column name (generally corresponding) | Description | 
|---|---|---|
| .ID | CONTAINER ID | Container ID | 
| .Image | IMAGE | Image ID | 
| .Command | COMMAND | command | 
| .CreatedAt | CREATED | Date and time of container creation | 
| .RunningFor | Calculated from CREATED | Time from the start of the container | 
| .Ports | PORTS | Port to open | 
| .State | STATUS | Container status(For example; “created”, “running”, “exited”). | 
| .Status | STATUS | Container status (with details) | 
| .Size | SIZE | Container size | 
| .Names | NAMES | Container name | 
| .Labels | - | All labels on the container | 
| .Label | - | A specific label on the container. For example'{{.Label "com.docker.swarm.cpu"}}' | 
| .Mounts | - | Mounted volume | 
| .Networks | - | Attached network | 
Delete the last 3 containers
#For the time being, you can erase it by passing all CONTAINER IDs to docker rm with xargs
$ docker ps -a -n 3 -q | xargs docker rm
Failure example
#I thought it would be better to have the size column on the far left and then let the sort command handle it.
#The unit is in the way, size/It fails because the virtual size cannot be distinguished because it is in the way
$ docker ps -a -n 3 --format "table {{.Size}}\t{{.ID}}\t{{.Names}}\t{{.Ports}}" | (read -r; printf "%s\n"; sort -k 1)
        Recommended Posts