I need to run more than 70 docker containers at once. Later, these containers need to be stopped.
At the moment I can docker stop all of them with the shell command docker stop $(docker ps -f since=<last docker before>). It works OK, but if there are any containers started after mine, I have a problem as the above code will stop them too.
Is there any way I can close all of running containers with some kind of specific search?
I know there is an docker ps -f label=<some label>, but I just haven't figured out on how to use it yet.
If you're launching many containers at the same time, launch them all with
docker run --label=anyname other-docker-args-of-yours image:tag
And when you want to delete all your containers just do
docker stop $(docker ps -f label=anyname | awk 'NR>1 {print$1}')
where anyname is the label name you provide during the docker run command and
awk 'NR>1 {print$1}' ignores the column header CONTAINER_ID and just prints the values alone.
Edit-1:
I later realized that you can achieve the list of Container_ID without awk as well. I'd consider using the below line.
docker stop `docker ps -qaf label=anyname`
If you want to remove all stoppped containers also, then include a within the options, like instead of -qf use -qaf.
-q to print container IDs alone.
-a for all containers including stopped.
Related
I have to stop all container's with specific string in name.
For example there is 4 container's with name's :
apache1 apache2 apache3 jboos
I try to stop only this with "apache" in the name.
Is there any native way in PODMAN to stop all containers with a specific name?
I can list the containers with the specified name:
podman ps -a -f name=apache
But is it possible to stop only container's from above output ?
Or maybe there is another way to achieve this result?
You can get a list of the container ids using
docker ps -q -f name=apache
Then you can use that somewhere else. I'm not familiar with podman, but you can kill the containers using docker like this
docker kill $(docker ps -q -f name=apache)
Please be aware that this will kill any container with a name that contains 'apache'. A container called 'im-nginx-and-not-apache' will match and will be killed.
When I launch docker, it launch by default a few containers that I have build in the past.(I've use docker-compose at the time, but deleted the repo since)
I kill them, but each time i restart docker, they are back.
What can I do ?
I know there is something like "docker system prune",
but I would like to delete the less possible .
You can try running docker ps -a to get a list of all containers including the ones which are not running but stopped.
You can then docker rm each container you do not wish to start on each docker restart.
Use docker ps to see what containers are running.
Use this command to kill/stop all running containers.
docker rm $(docker ps -a -q)
Use docker images to get list of all images.
Use docker rmi <image_id> to delete desired image.
I have a docker-compose.yml file with a few services declared within.
When I type docker-compose up they all start.
Now, I wish to fix just one of them. I usually then docker-compose down which stops all of them. Instead, I'm hoping to just pull down the one service I need to fix, fix it (build/compile/etc) and then start up that single Docker image.
So is there a way I can do this instead of doing:
docker-compose down (all services stop)
build/compile code in the service I wish to update
docker-compose up --build (all services compile/build/ ... )
You re-build the images and do a docker-compose up, which will automatically detect the the image is updated and recreate the service. Down side of building an images while the container from that image is running is that it will leave a none image, since docker won't delete image. You could overcome by removing the container from that image before hand by either one of these commands.
docker rm -vf <container-name>
docker rm -vr <container-id>
If that is inconvenient for you could delete the none images afterwards by,
docker rmi <image-id>
Also, if you have multiple none images hanging, you could remove all by,
docker images -q --filter "dangling=true" | xargs docker rmi -f
We normally go with:
docker-compose stop servicename
docker-compose rm servicename
and then:
docker-compose up -d servicename
I currently have a lot of docker containers running that are no longer used.
So many actually that it would take a while to exit and quit them all manually.
I was wondering if there was a way to exit and delete all of them except for the latest one ?
A quick approach to do this is the following:
First, stop all containers with this command:
docker stop $(docker ps -aq)
Then, start the container that you want to keep using:
docker start container_name
And finally, use docker container prune, which removes all stopped containers.
docker container prune
docker stop lastContainerName
It works fine. I want to stop it using docker ps -l command
docker stop | docker ps -l
I tried this. Unfortunately docker stop --help getting executed.
Am i missing something? How to achieve this? Any Suggestions.
It looks like what you are trying to do is to pipe the output of docker ps -l as an argument of the docker stop command. One way to do this in Unix is with back-quotes:
docker stop `docker ps -lq`
(I also added a -q option so you get just the ID with no column-names, etc.)