How to quickly show policies of all docker containers - docker

Is there a way to quickly show policies of all running docker containers?
For instance, I'm trying to find a way to list all currently running containers that do not have a restart policy set or to list all containers that have RestartPolicy set to "always".
I know I can use docker inspect to see the RestartPolicy of individual containers, but doing this one by one is a bit tedious.

You can make the command line run docker inspect on each container for you by combining docker ps -aq and xargs, and some docker inspect template magic lets you see only the names of the containers with --restart=always like so:
docker ps -aq | xargs docker inspect -f \
'{{if eq .HostConfig.RestartPolicy.Name "always"}}{{.Name}}{{end}}'
or, for the containers with no restart policy:
docker ps -aq | xargs docker inspect -f \
'{{if eq .HostConfig.RestartPolicy.Name ""}}{{.Name}}{{end}}'

Related

How Do I identify the Docker Containers That Are Managed by PREvant

I'm using PREvant with the Docker backend to review development versions of my microservice application. However, I want to execute some commands inside of a specific Docker container. How can I identify the containers of a certain application with docker ps?
PREvant adds labels to all containers when they will be created. The labels include, e.g. the service name and the application name. With following command you can identify the container ID and the service name in one line (replace my-application with your application name):
docker ps \
--filter "label=com.aixigo.preview.servant.app-name=my-application" \
--format '{{.ID}} {{.Label "com.aixigo.preview.servant.service-name"}}'
If you have a Kafka container in your application you can open a shell inside of the container with following command:
docker exec -it \
$(docker ps --filter "label=com.aixigo.preview.servant.app-name=my-application" --filter "label=com.aixigo.preview.servant.service-name=kafka" -q) \
sh

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.

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.

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

Docker: clean up all stopped containers except data-only containers

What is the Docker way to clean up all stopped Docker containers but retain data-only containers?
docker rm $(docker ps -qa -f status=exited) removes these too!
How to clean up the according images?
In general there is no definitive way to distinguish data-only from other containers. If you wish them to survive your cleansing, you could probably design a certain name scheme and have more elaborate scripts that wouldn't remote containers with name, say, starting with data-.
Following Mykolas proposal I introduced a naming convention requiring all data-only containers to be suffixed by -data.
To remove all stopped containers, except those named *-data:
docker ps -a -f status=exited | grep -v '\-data *$'| awk '{if(NR>1) print $1}' | xargs -r docker rm
To remove all unused images afterwards:
docker rmi $(docker images -qa -f dangling=true)
(the images used by the data-only containers are retained)
May be you can, in the docker run command of all your data-only container add a -e "type=data-only", and then filter based on this criteria, either with a grep or with a docker inspect example, I start a container with sudo docker run -it -e type=data-only ubuntu bash
root#f7e9ea4efbd9:/# and then sudo docker inspect -f "{{ .Config.Env }}" f7e shows
[type=data-only PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]

Resources