$ docker container run -d nginx:latest
02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730Let's run the ubuntu image:
$ docker container run ubuntu:latest
318bb50490ab5ebf49f633f36f14ca3e65c343bad8c87fb189e968ab6e038bea
$ docker container ls [OPTIONS]Note that the following commands are equivalent: docker container ls, docker container list, docker container ps, docker ps
-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_sutherlandNote 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.
$ 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
$ 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)
$ 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
$ 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
$ docker container ls -a --format "table {{.ID}}\t{{.Image}}" -f "id=02404a9bc24b"
CONTAINER ID IMAGE 02404a9bc24b nginx:latest
$ docker container ls -q
02404a9bc24bThe option '-q' can be useful for example in case you want to write scripts to do some actions on the containers.
$ docker container stop `docker container ls -q --no-trunc`
02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730To remove all containers:
$ docker container rm `docker container ls -q -a --no-trunc`
02404a9bc24bc3f7222035a9b7eaa698557bb53f660222858c7f590556762730 318bb50490ab5ebf49f633f36f14ca3e65c343bad8c87fb189e968ab6e038bea