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

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.

Related

Remove docker containers according to days filter

I have containers with a specific tag. I want to delete all containers which are running more than 3 days.
docker ps -a | grep -i "ngnix" | xargs docker container prune --force --filter "until=3days"
But its not working. Can anyone please help me?
If it's not clear:
I have list of containers with same tag name but different container_name and now I want to first grep all the containers with same tag name and stop all containers running since last 3 days
Your requirement is not clear but here is something I found related to it.
docker container prune provides a filter to remove containers until a specific date:
docker container prune -a --filter "until=$(date +'%Y-%m-%dT%H:%M:%S' --date='-3 days')"
OR
Using the command below, it will stop all containers running with a specific image, since the time of container id you provide (which is basically for time reference).
There are the following filters used :
ancestor - image_name
status - One of created, restarting, running, removing, paused, exited, or dead
since - The since filter shows only containers created since the container with given id or name.
( In your case give conaitner id which is just older than 3 days.)
docker container ls -a --filter ancestor=image_name --filter status=running --filter since=container_id | xargs docker stop
Using the above command will stop all containers running. Now in order to remove them, you just have to change filter values and change command to docker rm.
This command worked for me
Cleanup:
stage: cleanup
script:
- docker ps -a --filter "name=mapping" | grep 'days ago' | awk '{print $1}' | xargs docker stop
- docker image prune -f --filter "until=48h"
- docker system prune -f

How to docker remove all containers based on image name

My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"

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

What is the difference between docker ps -all and docker ps --all

The docker commands reference for ps mentions that
docker ps -a
docker ps --all
commands show the list of all containers.
While working on dockers, I mistakenly was using the command
docker ps -all
and it gave me the list of last container I ran, even though its status was Exited only.
Wasted much time due to this confusion. Have been looking for its official reference and couldn't find any. I was wondering if its a bug or am I missing something.
By convention on linux, -xyz is shorthand for -x -y -z, so it looks like docker's taking -all to be -a -l -l, or just -a -l.
From the reference quoted below, you can see that -a is shorthand for -all, and -l is shorthand for --latest, so you get shown the latest container run.
--all , -a Show all containers (default shows just running)
...
--latest , -l Show the latest created container (includes all states)
From the descriptions in the reference, it looks like -l effectively overrides -a, so your output for docker ps -all should be the same as for docker ps -l.

How do I get the container ID of a non-running container?

I was updating a docker image from a container shell via
docker run -it <image_name> /bin/bash -l
I've exited the container and now want to commit the changes with
docker commit -m 'changes' -a 'me' <container ID> <image name>
But I don't have access to the container ID anymore? docker ps only lists running containers. How can I get the ID of the stopped container?
Getting the container ID is really simple:
docker ps --all
The -a or --all flag to docker ps lists all containers, not just the running one.
If you want only the ID of the most-recently exited container, you can use
docker ps --all -q | head -1
This can be handy to use in the docker commit command so you can easily commit the latest changes by just pressing up a few times and changing the commit message:
docker commit -m 'changes' -a 'me' "$(docker ps --all -q | head -1)" <image name>
docker ps -l (or --latest) will return the container that was last run (also if that container is no longer running).
To get just the ID, add the -q flag, so docker ps -l -q (or docker ps -lq will give you the ID of the last run container

Resources