• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Docker | List Containers
  1. Notes
  2. docker container ls
  3. docker container ls -a ('--all')
  4. docker container ls -s ('--size')
  5. docker container ls --no-trunc
  6. docker container ls --format
  7. docker container ls -f ('--filter')
  8. docker container ls -q ('--quiet')

  1. Notes
    See this page for more information:
    https://docs.docker.com/reference/cli/docker/container/ls/

    For the following sections we will use the nginx and ubuntu images as examples to show how to use docker commands to list containers.

    Let's run the nginx image:
    $ docker container run -d nginx:latest
    02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730
    Let's run the ubuntu image:
    $ docker container run ubuntu:latest
    318bb50490ab5ebf49f633f36f14ca3e65c343bad8c87fb189e968ab6e038bea
  2. docker container ls
    The 'docker container ls' command has the following syntax:
    $ docker container ls [OPTIONS]
    Note that the following commands are equivalent: docker container ls, docker container list, docker container ps, docker ps

    The 'docker container ls' command has the following options:
    -a, --all             Show all containers (default shows just running)
    
    -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
    
    -f, --filter filter   Filter output based on conditions provided
    
        --format string   Format output using a custom template:
                          'table':            Print output in table format with column headers (default)
                          'table TEMPLATE':   Print output in table format using the given Go template
                          'json':             Print in JSON format
                          'TEMPLATE':         Print output using the given Go template.
                          Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
    Let's verify that nginx is up and running:
    $ docker container ls
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS    NAMES
    02404a9bc24b   nginx:latest   "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   80/tcp   competent_sutherland
    Note that the Ubuntu container was started and executed the command /bin/bash, but then exited because no process was running in the foreground or background.

    The Container ID shows the short ID (truncated) but you can use the --no-trunc to see the full ID.
  3. docker container ls -a ('--all')
    The 'docker container ls' command lists only running containers. To list all containers including stopped (or paused, failed, ...), use the option '-a' ('--all'):
    $ docker container ls -a
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS    NAMES
    02404a9bc24b   nginx:latest   "/docker-entrypoint.…"   9 seconds ago   Up 9 seconds               80/tcp   competent_sutherland
    318bb50490ab   ubuntu:latest  "/bin/bash"              5 seconds ago   Exited (0) 5 seconds ago            sleepy_brahmagupta
  4. docker container ls -s ('--size')
    To print the size of the container, use the option '-s' ('--size'):
    $ docker container ls -s
    CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS    NAMES                   SIZE
    02404a9bc24b   nginx:latest   "/docker-entrypoint.…"   5 minutes ago   Up 5 minutes   80/tcp   competent_sutherland    1.12kB (virtual 132MB)
  5. docker container ls --no-trunc
    To print the full container ID and the whole command, use the option '--no-trunc':
    $ docker container ls --no-trunc
    CONTAINER ID                                                       IMAGE          COMMAND                                          CREATED         STATUS         PORTS    NAMES
    02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730   nginx:latest   "/docker-entrypoint.sh nginx -g 'daemon off;'"   37 minutes ago  Up 37 minutes  80/tcp   competent_sutherland
  6. docker container ls --format
    To format the output of the command, use the option '--format'.
    You can use either json or GO template to format the output.

    First let's find out the available data that we can format (the trick is to print the output of the 'docker container ls' command as json):
    $ docker container ls --format json | jq
    $ docker container ls --format "{{json .}}" | jq
    {
      "Command": "\"/docker-entrypoint.…\"",
      "CreatedAt": "12:11:11 -0400 EDT",
      "ID": "02404a9bc24b",
      "Image": "nginx:latest",
      "Labels": "maintainer=NGINX Docker Maintainers <docker-maint@nginx.com>",
      "LocalVolumes": "0",
      "Mounts": "",
      "Names": "heuristic_black",
      "Networks": "bridge",
      "Ports": "80/tcp",
      "RunningFor": "10 minutes ago",
      "Size": "0B",
      "Status": "Up 10 minutes"
    }
    To print only the container ID and the image name:
    $ docker container ls -a --format "{{.ID}}\t{{.Image}}"
    "02404a9bc24b"    "nginx:latest"
    "318bb50490ab"    "ubuntu:latest"
    We can use the Go template to format the output:
    $ docker container ls -a --format "table {{.ID}}\t{{.Image}}\t{{.State}}\t{{.Status}}"
    CONTAINER ID   IMAGE           STATE     STATUS
    02404a9bc24b   nginx:latest    running   Up 9 seconds
    318bb50490ab   ubuntu:latest   exited    Exited (0) 5 seconds ago
  7. docker container ls -f ('--filter')
    To filter the output based on some conditions, use the option '-f' ('--filter'):
    $ docker container ls -a --format "table {{.ID}}\t{{.Image}}" -f "id=02404a9bc24b"
    CONTAINER ID        IMAGE
    02404a9bc24b        nginx:latest
  8. docker container ls -q ('--quiet')
    To print only the container ID, use the option '-q' ('--quiet'):
    $ docker container ls -q
    02404a9bc24b
    The option '-q' can be useful for example in case you want to write scripts to do some actions on the containers.

    For example, if you want to stop all running container:
    $ docker container stop `docker container ls -q --no-trunc`
    02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730
    To remove all containers:
    $ docker container rm `docker container ls -q -a --no-trunc`
    02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730
    318bb50490ab5ebf49f633f36f14ca3e65c343bad8c87fb189e968ab6e038bea
© 2025  mtitek