Docker ps -a command to publish only container names?

Hello All,

Please find the output from my docker ps -a command.


jim@jim-beam-VirtualBox:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8eb508a5590 d0e7f81ca65c "/bin/bash -c 'while " 14 hours ago Exited (137) 14 hours ago thirsty_roentgen
74b6a39b12c3 d0e7f81ca65c "/bin/bash -c 'while " 14 hours ago Exited (137) 14 hours ago tiny_shirley
8622857d142f d0e7f81ca65c “/bin/bash -c /bin/ba” 14 hours ago Exited (0) 14 hours ago dreamy_ptolemy
e0577d713618 d0e7f81ca65c “/bin/bash -c /bin/ba” 15 hours ago Exited (0) 15 hours ago reverent_goldwasser
f2e8e74bfb5e d0e7f81ca65c “/bin/bash -c /bin/ba” 16 hours ago Exited (0) 16 hours ago suspicious_mahavira
2667f01d3b12 d0e7f81ca65c “/bin/bash -c /bin/ba” 16 hours ago Exited (0) 16 hours ago insane_newton
a935ad7ee86b d0e7f81ca65c “/bin/bash -c /bin/ba” 16 hours ago Exited (0) 16 hours ago ecstatic_fermi
cc69dd90ea7c d0e7f81ca65c “/bin/bash -c /bin/ba” 16 hours ago Exited (0) 16 hours ago elegant_allen
59196ec9e552 d0e7f81ca65c "/bin/bash -c 'while " 16 hours ago Exited (137) 16 hours ago dreamy_bartik
75dd66ba4ad7 d0e7f81ca65c “/bin/bash” 16 hours ago Exited (0) 16 hours ago admiring_mcnulty
cdda24a71231 adejonge/helloworld “/helloworld” 11 days ago Exited (2) 11 days ago boring_hawking
a06da7ec609b adejonge/helloworld “/helloworld” 11 days ago Exited (2) 11 days ago goofy_shockley
jim@jim-beam-VirtualBox:~$


Is there any way just to get names?

I tried with docker ps -a --format but it says flag needs an argument. Where is the source documentation for all arguments/switches?

docker ps -q will print only the hex IDs, which is actually just as good for scripting purposes. I type extremely regularly

docker ps -a -q | xargs docker rm -v

which deletes all of the (stopped) containers. (And also its sibling,

docker images -f dangling=true -q | xargs docker rmi

which deletes images with no label and no running container.)

Thanks for that. This would be extremely useful for me in my learning.

You can also use the --format option for docker ps to set a custom template. All of this depends on what you want to do. If you want to clean up images, I suggest working with docker images directly.

$ docker ps --format '{{.Image}}'
sameersbn/redis:latest
nginx
nginx
nginx
nginx
nginx

Awesome !
This is what exactly I wanted. Hoping I am rigjt track.
jim@jim-beam-VirtualBox:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f529b3f6e18b centos "/bin/bash -c 'while " 9 seconds ago Up 9 seconds jolly_mirzakhani
34d543c5bd8c centos "/bin/bash -c 'while " 10 seconds ago Up 10 seconds reverent_hypatia
85d2859bd99d centos "/bin/bash -c 'while " 11 seconds ago Up 11 seconds tender_hypatia
9b2d867b0b57 centos "/bin/bash -c 'while " 13 seconds ago Up 12 seconds gloomy_bhaskara
c1380986824c centos "/bin/bash -c 'while " 15 seconds ago Up 15 seconds tender_mccarthy
3ba112a480cc centos "/bin/bash -c 'while " 11 minutes ago Up About a minute drunk_cori
jim@jim-beam-VirtualBox:~$ docker ps --format ‘{{.Image}}’
centos
centos
centos
centos
centos
centos
jim@jim-beam-VirtualBox:~$ docker ps --format ‘{{.Names}}’
jolly_mirzakhani
reverent_hypatia
tender_hypatia
gloomy_bhaskara
tender_mccarthy
drunk_cori
jim@jim-beam-VirtualBox:~$

Therefore I can write a shell script to manage these applications. Either stop/start or even delete them.

If I want to delete the image I would do with
docker rmi correct?

Can you provide me an example of custom template? I couldn’t get that. Please bear with me as I am still learning. Apologies in advance for silly questions

Thank you.

I am thrilled to see that two commands do the same job !

docker ps -a -q | xargs docker rm -v
and
docker ps --format ‘{{.Names}}’|xargs rm -vf

May I know which one to use when ?

Yes, docker rmi removes images.

If you just need to clean all containers, the docker ps -aq | xargs docker rm -v notation is a bit easier in my opinion (I usually use a subshell, i.e. docker rm $(docker ps -aq), but they’re practically the same). The format option comes in handy if you want to do something more flexible. The argument passed to the flag is a Golang text/template.

Yep, got it. Thank you again !

docker ps --format ‘{{.Names}}’

docker rm `docker ps -a -q` removes all the stopped containers. You will see error message for running containers

➜ webcv git:(master) docker ps --format “table {{.ID}}\t{{.Mounts}}”
CONTAINER ID MOUNTS
5c05e9202d44
a361eadb8c92 8646781e7e331d…,be3ee5f3f95de1…

you can find it here