Docker ps -a sort by date - docker

docker ps sorts by time, but the most recent docker instance is at the very top. This means if you started very many instances you have to scroll all the way to the top to see them. How do we output "docker ps -a" in reverse order, so that the most recent instance is printed at the bottom?

You can pipe the output to tac[1] like:
docker ps -a | tac
[1] From man tac: tac - concatenate and print files in reverse

Latest created container:
docker ps -a -l
Latest 5 created containers:
docker ps -a -n 5
As far as I know ordering is not possible but maybe you don't really need it...

It's enough to get what you want.
$ docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.CreatedAt}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3 -r )
See also
How to sort or order results docker ps --format?

Related

What is difference between 'docker ps -all' and 'docker ps -a'?

The command 'docker ps -all' gives me all the created containers but 'docker ps -a' also supposed to give me same results but somehow it returns me only 1 or sometimes 2 latest entries of containers.
Can someone please explain me? I am new to docker. Any help is appreciated.
There is a difference between docker ps -a (which is identical to docker ps --all) and docker ps -all.
We can see the difference by looking at the documentation of docker ps:
Options
Name, shorthand Default Description
--all , -a Show all containers (default shows just running)
--filter , -f Filter output based on conditions provided
--format Pretty-print containers using a Go template
--last , -n -1 Show n last created containers (includes all states)
--latest , -l Show the latest created container (includes all states)
--no-trunc Don't truncate output
--quiet , -q Only display container IDs
--size , -s Display total file sizes
As we can see: docker ps -a and docker ps --all are identical.
Meanwhile, docker ps -all uses the shorthand form and is identical to docker ps -al. This command will only show the most recently created container.
As you can see in the documentation -a is just short for --all, they serve the same purpose.
docker ps -a and docker ps --all are same commands but if you use docker ps -all command it will return just last created container. docker ps -all is same as docker ps -al.

Docker: Filter containers by image, ignoring the tag, then the images

I need to remove a series of open containers which are derived from an image which I have tagged many times so I have plenty of versions around. I also have plenty of containers running (which remained hanging from various tests I did with the various versions of the container).
What are the commands to quickly erase all of them? (I.E based on image name or whatever)
I found that you can do
docker container stop $(docker container ls -aq --filter "ancestor=imagename")
But this doesn't work with the various tagged versions (like imagename:1.0, imagename:2.0)
so to get all of them I had to use
docker container stop $(docker container ls -aq --limit 30)
Which gets the container created up to 1 month ago.
Prune just cleaned a few of them.
I won't list every and all the commands I tried.
Please tell me what is the quickest and cleanest way to clean an image and all the successive tagged versions, and the relative containers which are running or exited related to them.
Thanks
Can't you just use some linux magic? For example
docker ps | grep <image-name> | awk '{ print $1 }' | xargs docker stop
Explanation:
docker ps show running containers
grep <image-name> search for image-name in the previous command output
awk '{ print $1 }' take the first 'column' (in this case the container id)
xargs docker stop feed the ids to docker stop
Similarly, you could just use other bash commands to delete the containers and remove the images:
docker ps -a | grep <image-name> | awk '{ print $1 }' | xargs docker container rm
docker image ls | grep <image-name> | awk '{ print $3 }' | xargs docker rmi
A little warning: grep is not specific to docker and will filter just based on the contents of the string so if your image name appears in the container names or anywhere else, then this method will stop/remove those containers as well!

Filter docker containers by name and stop them

I'm trying to stop all the containers starting with the name app_
I though this would work: docker stop $(docker ps -f name="app_*"), but it shows:
unknown shorthand flag: 'f' in -f
See 'docker stop --help'.
Is there a way to do this?
You must put the complete filter expression into parantheses:
docker ps -f "name=app_*"
The search is fuzzy by default, so e.g. name=app will also return my-app.
You can use a regex to indicate that the match should be at the start:
docker ps -f "name=^app_"
You should further add the quiet flag q so that the command only returns ids to make it work with docker stop:
docker stop $(docker ps -qf "name=^app_")
Your command would work, if you had the docker ps show only the ID of containers.
I started some sleeping containers like this:
for i in $(seq 1 10); do
docker run --rm -d --name sleep_$i bash sleep 100000;
done
And since they were all named sleep_* I could stop them like this:
docker ps -f Name=sleep_* --format '{{.ID}}' | xargs docker stop
I usually use xargs rather than $() when possible for this kind of thing - for one reason, I can easily parallelize it with -n 3 -P 10 (10 concurrent executions of 3 each):
$ docker ps -f Name=sleep_* --format '{{.ID}}' | xargs -n 3 -P 10 docker stop
c58a1fc538cf
98a9358734e4
e34828d3a7d7
ffbd2ec18775
1ba1e8a304e7
5e78aecb4ed6
cf86c4ea46aa
624a7d0342c2
7cb17ece1f0a
01665a084d02
Quite a lot faster for many containers.

Stop docker containers with name matching a pattern

I'm using puckel/docker-airflow with CeleryExecutor. It launches a total of 5 containers named like this
docker-airflow_flower_1_de2035f778e6
docker-airflow_redis_1_49d2e710e82b
..
While development, I often have to stop all above containers. However, I can't do a docker stop $(docker ps -aq) since I have other containers running on my machine too.
Is there a way to stop all containers who's names match a given pattern (for instance all containers who's names start with docker-airflow in above)?
From this article by #james-coyle, following command works for me
docker ps --filter name=docker-airflow* --filter status=running -aq | xargs docker stop
I believe docker CLI natively does not provide such a functionality, so we have to rely on filtering and good-old bash PIPE and xargs
UPDATE-1
Note that depending on your environment, you might have to do these
run docker commands with sudo (just prefix both docker .. commands above with sudo)
enclose name pattern in double-quotes --filter name="docker-airflow*" (particularly on zsh)
Better late than never ;). From this article. The following works for me:
Stop containers with names matching a given pattern:
$ docker container stop $(docker container ls -q --filter name=<pattern>)
On the other hand, if we want to start containers with names matching a given pattern:
$ docker container start $(docker container ls --all -q --filter name=<pattern>)
NOTE: For different environments related tips, #y2k-shubham's update is a good starting point.
Another approach using grep and docker ps:
To stop docker container matching the given pattern/list of pattern":
docker ps | grep -E "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
To stop docker container excluding the given pattern/list of pattern:
docker ps | grep -Ev "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
Reference: Grep

Shell command to Get container id from "docker ps"

I am basically looking to achieve this 2 steps:
1. Run the docker image:
docker run -p 80:80 some-image-name:25
2. Now "docker ps" returns whole data about the container but I was looking for just the container ID to
3. run some test on it.. ex.
docker exec -it /usr/bin/npm run test
So my question is how I can get just the container id from step 2.
Note: I need this flow for my pipeline script in Jenkins.
docker ps -a -q
This will give you only container's id
You could use awk to get the container ID's as follows:
docker ps | awk 'NR > 1 {print $1}'
This one-liner outputs all the container ID's printed by docker ps. To get only the first one you would use:
docker ps | awk 'NR > 1 {print $1; exit}'
Even though that answers your question I recommend that you use container names instead of relying on container ID's.
P.S.: This solution is on average 1 millisecond slower than docker ps -q, but it is significantly more flexible.
docker ps --format {{.ID}}
Will return only the ids of running containers.
you can use docker functionality to get this done:
docker ps --filter volume=remote-volume --format "table {{.ID}}\t{{.Mounts}}"
with --format "{{.ID}}" you'd get the ids only. You can also filter. Read the documentation of docker ps for more details
All the below command give you container id's
docker ps -aqf "name=containername"
docker ps --no-trunc -aqf name=containername
docker container ls | grep 'container-name' | awk '{print $1}'```
You can get container ID using following command:
docker ps -q

Resources