github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/docs/reference/commandline/ps.md (about)

     1  # ps
     2  
     3  <!---MARKER_GEN_START-->
     4  List containers
     5  
     6  ### Aliases
     7  
     8  `docker container ls`, `docker container list`, `docker container ps`, `docker ps`
     9  
    10  ### Options
    11  
    12  | Name                                   | Type     | Default | Description                                                                                                                                                                                                                                                                                                                                                                                                                          |
    13  |:---------------------------------------|:---------|:--------|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    14  | [`-a`](#all), [`--all`](#all)          |          |         | Show all containers (default shows just running)                                                                                                                                                                                                                                                                                                                                                                                     |
    15  | [`-f`](#filter), [`--filter`](#filter) | `filter` |         | Filter output based on conditions provided                                                                                                                                                                                                                                                                                                                                                                                           |
    16  | [`--format`](#format)                  | `string` |         | Format output using a custom template:<br>'table':            Print output in table format with column headers (default)<br>'table TEMPLATE':   Print output in table format using the given Go template<br>'json':             Print in JSON format<br>'TEMPLATE':         Print output using the given Go template.<br>Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates |
    17  | `-n`, `--last`                         | `int`    | `-1`    | Show n last created containers (includes all states)                                                                                                                                                                                                                                                                                                                                                                                 |
    18  | `-l`, `--latest`                       |          |         | Show the latest created container (includes all states)                                                                                                                                                                                                                                                                                                                                                                              |
    19  | [`--no-trunc`](#no-trunc)              |          |         | Don't truncate output                                                                                                                                                                                                                                                                                                                                                                                                                |
    20  | `-q`, `--quiet`                        |          |         | Only display container IDs                                                                                                                                                                                                                                                                                                                                                                                                           |
    21  | [`-s`](#size), [`--size`](#size)       |          |         | Display total file sizes                                                                                                                                                                                                                                                                                                                                                                                                             |
    22  
    23  
    24  <!---MARKER_GEN_END-->
    25  
    26  ## Examples
    27  
    28  ### <a name="no-trunc"></a> Do not truncate output (--no-trunc)
    29  
    30  Running `docker ps --no-trunc` showing 2 linked containers.
    31  
    32  ```console
    33  $ docker ps --no-trunc
    34  
    35  CONTAINER ID        IMAGE                        COMMAND                CREATED              STATUS              PORTS               NAMES
    36  4c01db0b339c        ubuntu:22.04                 bash                   17 seconds ago       Up 16 seconds       3300-3310/tcp       webapp
    37  d7886598dbe2        crosbymichael/redis:latest   /redis-server --dir    33 minutes ago       Up 33 minutes       6379/tcp            redis,webapp/db
    38  ```
    39  
    40  ### <a name="all"></a> Show both running and stopped containers (-a, --all)
    41  
    42  The `docker ps` command only shows running containers by default. To see all
    43  containers, use the `--all` (or `-a`) flag:
    44  
    45  ```console
    46  $ docker ps -a
    47  ```
    48  
    49  `docker ps` groups exposed ports into a single range if possible. E.g., a
    50  container that exposes TCP ports `100, 101, 102` displays `100-102/tcp` in
    51  the `PORTS` column.
    52  
    53  ### <a name="size"></a> Show disk usage by container (--size)
    54  
    55  The `docker ps --size` (or `-s`) command displays two different on-disk-sizes for each container:
    56  
    57  ```console
    58  $ docker ps --size
    59  
    60  CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS       PORTS   NAMES        SIZE
    61  e90b8831a4b8   nginx          "/bin/bash -c 'mkdir "   11 weeks ago   Up 4 hours           my_nginx     35.58 kB (virtual 109.2 MB)
    62  00c6131c5e30   telegraf:1.5   "/entrypoint.sh"         11 weeks ago   Up 11 weeks          my_telegraf  0 B (virtual 209.5 MB)
    63  ```
    64    * The "size" information shows the amount of data (on disk) that is used for the _writable_ layer of each container
    65    * The "virtual size" is the total amount of disk-space used for the read-only _image_ data used by the container and the writable layer.
    66  
    67  For more information, refer to the [container size on disk](https://docs.docker.com/storage/storagedriver/#container-size-on-disk) section.
    68  
    69  
    70  ### <a name="filter"></a> Filtering (--filter)
    71  
    72  The `--filter` (or `-f`) flag format is a `key=value` pair. If there is more
    73  than one filter, then pass multiple flags (e.g. `--filter "foo=bar" --filter "bif=baz"`)
    74  
    75  The currently supported filters are:
    76  
    77  | Filter                | Description                                                                                                                          |
    78  |:----------------------|:-------------------------------------------------------------------------------------------------------------------------------------|
    79  | `id`                  | Container's ID                                                                                                                       |
    80  | `name`                | Container's name                                                                                                                     |
    81  | `label`               | An arbitrary string representing either a key or a key-value pair. Expressed as `<key>` or `<key>=<value>`                           |
    82  | `exited`              | An integer representing the container's exit code. Only useful with `--all`.                                                         |
    83  | `status`              | One of `created`, `restarting`, `running`, `removing`, `paused`, `exited`, or `dead`                                                 |
    84  | `ancestor`            | Filters containers which share a given image as an ancestor. Expressed as `<image-name>[:<tag>]`,  `<image id>`, or `<image@digest>` |
    85  | `before` or `since`   | Filters containers created before or after a given container ID or name                                                              |
    86  | `volume`              | Filters running containers which have mounted a given volume or bind mount.                                                          |
    87  | `network`             | Filters running containers connected to a given network.                                                                             |
    88  | `publish` or `expose` | Filters containers which publish or expose a given port. Expressed as `<port>[/<proto>]` or `<startport-endport>/[<proto>]`          |
    89  | `health`              | Filters containers based on their healthcheck status. One of `starting`, `healthy`, `unhealthy` or `none`.                           |
    90  | `isolation`           | Windows daemon only. One of `default`, `process`, or `hyperv`.                                                                       |
    91  | `is-task`             | Filters containers that are a "task" for a service. Boolean option (`true` or `false`)                                               |
    92  
    93  
    94  #### label
    95  
    96  The `label` filter matches containers based on the presence of a `label` alone or a `label` and a
    97  value.
    98  
    99  The following filter matches containers with the `color` label regardless of its value.
   100  
   101  ```console
   102  $ docker ps --filter "label=color"
   103  
   104  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
   105  673394ef1d4c        busybox             "top"               47 seconds ago      Up 45 seconds                           nostalgic_shockley
   106  d85756f57265        busybox             "top"               52 seconds ago      Up 51 seconds                           high_albattani
   107  ```
   108  
   109  The following filter matches containers with the `color` label with the `blue` value.
   110  
   111  ```console
   112  $ docker ps --filter "label=color=blue"
   113  
   114  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
   115  d85756f57265        busybox             "top"               About a minute ago   Up About a minute                       high_albattani
   116  ```
   117  
   118  #### name
   119  
   120  The `name` filter matches on all or part of a container's name.
   121  
   122  The following filter matches all containers with a name containing the `nostalgic_stallman` string.
   123  
   124  ```console
   125  $ docker ps --filter "name=nostalgic_stallman"
   126  
   127  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
   128  9b6247364a03        busybox             "top"               2 minutes ago       Up 2 minutes                            nostalgic_stallman
   129  ```
   130  
   131  You can also filter for a substring in a name as this shows:
   132  
   133  ```console
   134  $ docker ps --filter "name=nostalgic"
   135  
   136  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
   137  715ebfcee040        busybox             "top"               3 seconds ago       Up 1 second                             i_am_nostalgic
   138  9b6247364a03        busybox             "top"               7 minutes ago       Up 7 minutes                            nostalgic_stallman
   139  673394ef1d4c        busybox             "top"               38 minutes ago      Up 38 minutes                           nostalgic_shockley
   140  ```
   141  
   142  #### exited
   143  
   144  The `exited` filter matches containers by exist status code. For example, to
   145  filter for containers that have exited successfully:
   146  
   147  ```console
   148  $ docker ps -a --filter 'exited=0'
   149  
   150  CONTAINER ID        IMAGE             COMMAND                CREATED             STATUS                   PORTS                      NAMES
   151  ea09c3c82f6e        registry:latest   /srv/run.sh            2 weeks ago         Exited (0) 2 weeks ago   127.0.0.1:5000->5000/tcp   desperate_leakey
   152  106ea823fe4e        fedora:latest     /bin/sh -c 'bash -l'   2 weeks ago         Exited (0) 2 weeks ago                              determined_albattani
   153  48ee228c9464        fedora:20         bash                   2 weeks ago         Exited (0) 2 weeks ago                              tender_torvalds
   154  ```
   155  
   156  #### Filter by exit signal
   157  
   158  You can use a filter to locate containers that exited with status of `137`
   159  meaning a `SIGKILL(9)` killed them.
   160  
   161  ```console
   162  $ docker ps -a --filter 'exited=137'
   163  
   164  CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                       PORTS               NAMES
   165  b3e1c0ed5bfe        ubuntu:latest       "sleep 1000"           12 seconds ago      Exited (137) 5 seconds ago                       grave_kowalevski
   166  a2eb5558d669        redis:latest        "/entrypoint.sh redi   2 hours ago         Exited (137) 2 hours ago                         sharp_lalande
   167  ```
   168  
   169  Any of these events result in a `137` status:
   170  
   171  * the `init` process of the container is killed manually
   172  * `docker kill` kills the container
   173  * Docker daemon restarts which kills all running containers
   174  
   175  #### status
   176  
   177  The `status` filter matches containers by status. You can filter using
   178  `created`, `restarting`, `running`, `removing`, `paused`, `exited` and `dead`. For example,
   179  to filter for `running` containers:
   180  
   181  ```console
   182  $ docker ps --filter status=running
   183  
   184  CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
   185  715ebfcee040        busybox                "top"               16 minutes ago      Up 16 minutes                           i_am_nostalgic
   186  d5c976d3c462        busybox                "top"               23 minutes ago      Up 23 minutes                           top
   187  9b6247364a03        busybox                "top"               24 minutes ago      Up 24 minutes                           nostalgic_stallman
   188  ```
   189  
   190  To filter for `paused` containers:
   191  
   192  ```console
   193  $ docker ps --filter status=paused
   194  
   195  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
   196  673394ef1d4c        busybox             "top"               About an hour ago   Up About an hour (Paused)                       nostalgic_shockley
   197  ```
   198  
   199  #### ancestor
   200  
   201  The `ancestor` filter matches containers based on its image or a descendant of
   202  it. The filter supports the following image representation:
   203  
   204  - `image`
   205  - `image:tag`
   206  - `image:tag@digest`
   207  - `short-id`
   208  - `full-id`
   209  
   210  If you don't specify a `tag`, the `latest` tag is used. For example, to filter
   211  for containers that use the latest `ubuntu` image:
   212  
   213  ```console
   214  $ docker ps --filter ancestor=ubuntu
   215  
   216  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
   217  919e1179bdb8        ubuntu-c1           "top"               About a minute ago   Up About a minute                       admiring_lovelace
   218  5d1e4a540723        ubuntu-c2           "top"               About a minute ago   Up About a minute                       admiring_sammet
   219  82a598284012        ubuntu              "top"               3 minutes ago        Up 3 minutes                            sleepy_bose
   220  bab2a34ba363        ubuntu              "top"               3 minutes ago        Up 3 minutes                            focused_yonath
   221  ```
   222  
   223  Match containers based on the `ubuntu-c1` image which, in this case, is a child
   224  of `ubuntu`:
   225  
   226  ```console
   227  $ docker ps --filter ancestor=ubuntu-c1
   228  
   229  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
   230  919e1179bdb8        ubuntu-c1           "top"               About a minute ago   Up About a minute                       admiring_lovelace
   231  ```
   232  
   233  Match containers based on the `ubuntu` version `22.04` image:
   234  
   235  ```console
   236  $ docker ps --filter ancestor=ubuntu:22.04
   237  
   238  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
   239  82a598284012        ubuntu:22.04        "top"               3 minutes ago        Up 3 minutes                            sleepy_bose
   240  ```
   241  
   242  The following matches containers based on the layer `d0e008c6cf02` or an image
   243  that have this layer in its layer stack.
   244  
   245  ```console
   246  $ docker ps --filter ancestor=d0e008c6cf02
   247  
   248  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
   249  82a598284012        ubuntu:22.04        "top"               3 minutes ago        Up 3 minutes                            sleepy_bose
   250  ```
   251  
   252  #### Create time
   253  
   254  ##### before
   255  
   256  The `before` filter shows only containers created before the container with
   257  given id or name. For example, having these containers created:
   258  
   259  ```console
   260  $ docker ps
   261  
   262  CONTAINER ID        IMAGE       COMMAND       CREATED              STATUS              PORTS              NAMES
   263  9c3527ed70ce        busybox     "top"         14 seconds ago       Up 15 seconds                          desperate_dubinsky
   264  4aace5031105        busybox     "top"         48 seconds ago       Up 49 seconds                          focused_hamilton
   265  6e63f6ff38b0        busybox     "top"         About a minute ago   Up About a minute                      distracted_fermat
   266  ```
   267  
   268  Filtering with `before` would give:
   269  
   270  ```console
   271  $ docker ps -f before=9c3527ed70ce
   272  
   273  CONTAINER ID        IMAGE       COMMAND       CREATED              STATUS              PORTS              NAMES
   274  4aace5031105        busybox     "top"         About a minute ago   Up About a minute                      focused_hamilton
   275  6e63f6ff38b0        busybox     "top"         About a minute ago   Up About a minute                      distracted_fermat
   276  ```
   277  
   278  ##### since
   279  
   280  The `since` filter shows only containers created since the container with given
   281  id or name. For example, with the same containers as in `before` filter:
   282  
   283  ```console
   284  $ docker ps -f since=6e63f6ff38b0
   285  
   286  CONTAINER ID        IMAGE       COMMAND       CREATED             STATUS              PORTS               NAMES
   287  9c3527ed70ce        busybox     "top"         10 minutes ago      Up 10 minutes                           desperate_dubinsky
   288  4aace5031105        busybox     "top"         10 minutes ago      Up 10 minutes                           focused_hamilton
   289  ```
   290  
   291  #### volume
   292  
   293  The `volume` filter shows only containers that mount a specific volume or have
   294  a volume mounted in a specific path:
   295  
   296  ```console
   297  $ docker ps --filter volume=remote-volume --format "table {{.ID}}\t{{.Mounts}}"
   298  
   299  CONTAINER ID        MOUNTS
   300  9c3527ed70ce        remote-volume
   301  
   302  $ docker ps --filter volume=/data --format "table {{.ID}}\t{{.Mounts}}"
   303  
   304  CONTAINER ID        MOUNTS
   305  9c3527ed70ce        remote-volume
   306  ```
   307  
   308  #### network
   309  
   310  The `network` filter shows only containers that are connected to a network with
   311  a given name or id.
   312  
   313  The following filter matches all containers that are connected to a network
   314  with a name containing `net1`.
   315  
   316  ```console
   317  $ docker run -d --net=net1 --name=test1 ubuntu top
   318  $ docker run -d --net=net2 --name=test2 ubuntu top
   319  
   320  $ docker ps --filter network=net1
   321  
   322  CONTAINER ID        IMAGE       COMMAND       CREATED             STATUS              PORTS               NAMES
   323  9d4893ed80fe        ubuntu      "top"         10 minutes ago      Up 10 minutes                           test1
   324  ```
   325  
   326  The network filter matches on both the network's name and id. The following
   327  example shows all containers that are attached to the `net1` network, using
   328  the network id as a filter;
   329  
   330  ```console
   331  $ docker network inspect --format "{{.ID}}" net1
   332  
   333  8c0b4110ae930dbe26b258de9bc34a03f98056ed6f27f991d32919bfe401d7c5
   334  
   335  $ docker ps --filter network=8c0b4110ae930dbe26b258de9bc34a03f98056ed6f27f991d32919bfe401d7c5
   336  
   337  CONTAINER ID        IMAGE       COMMAND       CREATED             STATUS              PORTS               NAMES
   338  9d4893ed80fe        ubuntu      "top"         10 minutes ago      Up 10 minutes                           test1
   339  ```
   340  
   341  #### publish and expose
   342  
   343  The `publish` and `expose` filters show only containers that have published or exposed port with a given port
   344  number, port range, and/or protocol. The default protocol is `tcp` when not specified.
   345  
   346  The following filter matches all containers that have published port of 80:
   347  
   348  ```console
   349  $ docker run -d --publish=80 busybox top
   350  $ docker run -d --expose=8080 busybox top
   351  
   352  $ docker ps -a
   353  
   354  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
   355  9833437217a5        busybox             "top"               5 seconds ago       Up 4 seconds        8080/tcp                dreamy_mccarthy
   356  fc7e477723b7        busybox             "top"               50 seconds ago      Up 50 seconds       0.0.0.0:32768->80/tcp   admiring_roentgen
   357  
   358  $ docker ps --filter publish=80
   359  
   360  CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                   NAMES
   361  fc7e477723b7        busybox             "top"               About a minute ago   Up About a minute   0.0.0.0:32768->80/tcp   admiring_roentgen
   362  ```
   363  
   364  The following filter matches all containers that have exposed TCP port in the range of `8000-8080`:
   365  
   366  ```console
   367  $ docker ps --filter expose=8000-8080/tcp
   368  
   369  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
   370  9833437217a5        busybox             "top"               21 seconds ago      Up 19 seconds       8080/tcp            dreamy_mccarthy
   371  ```
   372  
   373  The following filter matches all containers that have exposed UDP port `80`:
   374  
   375  ```console
   376  $ docker ps --filter publish=80/udp
   377  
   378  CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
   379  ```
   380  
   381  ### <a name="format"></a> Format the output (--format)
   382  
   383  The formatting option (`--format`) pretty-prints container output using a Go
   384  template.
   385  
   386  Valid placeholders for the Go template are listed below:
   387  
   388  | Placeholder   | Description                                                                                     |
   389  |:--------------|:------------------------------------------------------------------------------------------------|
   390  | `.ID`         | Container ID                                                                                    |
   391  | `.Image`      | Image ID                                                                                        |
   392  | `.Command`    | Quoted command                                                                                  |
   393  | `.CreatedAt`  | Time when the container was created.                                                            |
   394  | `.RunningFor` | Elapsed time since the container was started.                                                   |
   395  | `.Ports`      | Exposed ports.                                                                                  |
   396  | `.State`      | Container status (for example; "created", "running", "exited").                                 |
   397  | `.Status`     | Container status with details about duration and health-status.                                 |
   398  | `.Size`       | Container disk size.                                                                            |
   399  | `.Names`      | Container names.                                                                                |
   400  | `.Labels`     | All labels assigned to the container.                                                           |
   401  | `.Label`      | Value of a specific label for this container. For example `'{{.Label "com.docker.swarm.cpu"}}'` |
   402  | `.Mounts`     | Names of the volumes mounted in this container.                                                 |
   403  | `.Networks`   | Names of the networks attached to this container.                                               |
   404  
   405  When using the `--format` option, the `ps` command will either output the data
   406  exactly as the template declares or, when using the `table` directive, includes
   407  column headers as well.
   408  
   409  The following example uses a template without headers and outputs the `ID` and
   410  `Command` entries separated by a colon (`:`) for all running containers:
   411  
   412  ```console
   413  $ docker ps --format "{{.ID}}: {{.Command}}"
   414  
   415  a87ecb4f327c: /bin/sh -c #(nop) MA
   416  01946d9d34d8: /bin/sh -c #(nop) MA
   417  c1d3b0166030: /bin/sh -c yum -y up
   418  41d50ecd2f57: /bin/sh -c #(nop) MA
   419  ```
   420  
   421  To list all running containers with their labels in a table format you can use:
   422  
   423  ```console
   424  $ docker ps --format "table {{.ID}}\t{{.Labels}}"
   425  
   426  CONTAINER ID        LABELS
   427  a87ecb4f327c        com.docker.swarm.node=ubuntu,com.docker.swarm.storage=ssd
   428  01946d9d34d8
   429  c1d3b0166030        com.docker.swarm.node=debian,com.docker.swarm.cpu=6
   430  41d50ecd2f57        com.docker.swarm.node=fedora,com.docker.swarm.cpu=3,com.docker.swarm.storage=ssd
   431  ```
   432  
   433  To list all running containers in JSON format, use the `json` directive:
   434  
   435  ```console
   436  $ docker ps --format json
   437  {"Command":"\"/docker-entrypoint.…\"","CreatedAt":"2021-03-10 00:15:05 +0100 CET","ID":"a762a2b37a1d","Image":"nginx","Labels":"maintainer=NGINX Docker Maintainers \u003cdocker-maint@nginx.com\u003e","LocalVolumes":"0","Mounts":"","Names":"boring_keldysh","Networks":"bridge","Ports":"80/tcp","RunningFor":"4 seconds ago","Size":"0B","State":"running","Status":"Up 3 seconds"}
   438  ```