Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to remove all containers at once with awk, but after running this code I got an empty line and no any argument passed to the docker.
for i in {1..3}; do docker container rm -f `docker ps -a | awk -v i=$i++ 'NR==i {print $1}'`; done'
The solution you posted yourself is absurd. Just
docker ps -a -q |
xargs -n 1 docker container rm -f
Less compactly, you could say
docker ps -a -q |
while read -r id; do
docker container rm -f "$id"
done
The -q option to docker ps causes it to only list the container's id, without the chaff you apparently figured out you wanted to remove with Awk.
Expressing shell logic as pipelines is often very succinct, natural, and quick; thinking you somehow need to know how many of something you have before you can loop over them is a rather common beginner antipattern.
If there could be more than three containers and you always want to kill the first three, you can add head -n 3 in the obvious place; though this too seems like a potentially grave beginner mistake - you have no direct control over in which order docker ps will list things. A much better approach then would be to pass in a --filter argument to docker ps to select only exactly the ones which (are yours and which also) meet some filtering criterion.
<shellcheck.net> correct my issue
thanks https://stackoverflow.com/users/1745001/ed-morton
for i in {1..3}; do docker container rm -f $(docker ps -a | awk -v i="$i" 'NR==i {print $1}'); done
Related
I would like to use awk in gitlab-ci.yml to kill a docker container. However, awk does not work as expected.
For example, I want to kill a docker container called ADockerContainer using awk. Therefore I use the following command:
docker kill $(docker ps | grep ADockerContainer | awk '{print $1}')
After the execution of the command, I get:
"docker kill" requires at least 1 argument.
Does anyone know how to fix this?
docker kill (and other commands) will take the container name directly, so you don't need any sort of command substitution here. It's enough to run
docker kill AContainerName
This question already has an answer here:
Dashes in Docker commands
(1 answer)
Closed 1 year ago.
Learning docker so I have some dumb questions
Came across the below command , why is that some arguments have only one hyphen (-) while others have two hypens (--). One of them is not a argument? Like interactive terminal is -it , while remove container is with --rm ?
docker run -it --rm --entrypoint sh debug/ubuntu
What does --rm do in the above command. rm is to remove the container so why are they passing it when running the container?
Answered before here
This is a standard Unix/Linux syntax, not specific to docker. One dash
is used for single letter flags. Two dashes for an option that is more
than one letter. You can merge together multiple single letter options
that don't take arguments, e.g. -i and -t can be merged into -it. You
can run docker --help to see all the options, some of which have both
a long and short format.
The --rm means that the container will be removed automatically after it is stopped. Without --rm, it can be found "dangling" with docker container ls --all
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
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?
Once in a while I bash into my containers to manually run some scripts on my servers and sometimes my session times out and those containers stay up.
I'm trying to run a command to kill all previous running containers of that image. Let's say my image is called "WEB" and this is what docker ps is showing:
ID NAMES
1 project_web_1
2 project_web_2
3 project_web_3
I want to kill all of those with a single command. I have tried this
docker rm -f $(docker ps -aqf "name=web") but this only kills the first one. Is there a way to kill all of them?
It seems my command actually removes all of them
docker rm -f $(docker ps -aqf "name=web")
I just had a typo in my image name. I leave this question/answer in case someone in the future needs it.
docker container rm -f $(docker container ps | awk '/yourname/ {print $1}')
Replace yourname with the name or anything you want to match from docker ps.