Docker command that purges everything - docker

Is there a single docker command that can be used to purge everything? Stop all containers if running, delete all images, delete all volumes... ect.

I don't think there is a single command to do that. You first need to stop all containers using
$ docker stop `docker ps -qa` > /dev/null 2>&1; ## Stop all running containers
$ docker system prune --volumes --all; ## Remove all unused docker components
Usage on prune can be found in docker official documentation.
You can still run the above 2 commands in a single line.
$ docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;
Or an alias should help
alias dockerRemoveAll="docker stop `docker ps -qa` > /dev/null 2>&1; docker system prune --volumes --all;"
Edit-1:
Based on #Zeitounator's comment, I've added > /dev/null 2>&1 to redirect whatever the output is to null and stderr. This still continues to remove all the docker contents even when the exit code is 1 for the stop command when there are no containers.
Also #Zeitounator's wipedocker function is even more elegant.

Related

Docker system prune: only current directory

I'm working on 2 projects that both use Docker, in separate directories.
In the 2nd project, for a new local build, the first command given (of a series of commands) is the following:
docker container stop $(docker container ls -a -q) && docker system prune -a -f --volumes
However, as a side effect, this kills the containers in the 1st project, also destroying the databases associated with it as well.
This is annoying because I have to constantly rebuild and re-seed the database in the 1st project.
How can I edit the first command such that it only effects the project in the current directory?
Note that this project is also using docker-compose, which I know is good at noting the current directory, so maybe we could make use of docker-compose instead.
The full list of commands given for a new local build are:
docker container stop $(docker container ls -a -q) && docker system prune -a -f --volumes
docker stack rm up
docker-compose -f docker-compose.local.yml build
docker stack deploy up --compose-file docker-compose.local.yml
Thank you very much in advance for any help.
-Michael

Docker not killing containers

I'm trying to kill my docker containers with the command:
$ docker container kill $(docker ps -q)
however, the containers aren't responding.
I'm able to log into them with:
$ docker exec -it container_id bash
but any commands within the terminal hangs.
Whats interesting is somehow the process also doesn't seem to exist. I get the list of running containers with the process ids as so:
$ for i in $(docker container ls --format "{{.ID}}"); do docker inspect -f '{{.State.Pid}} {{.Name}}' $i; done
12821 /brave_carson
12661 /trusting_hoover0
12617 /peaceful_franklin
12534 /frosty_volhard
12702 /zealous_sammet
12678 /flamboyant_jang
12690 /dreamy_driscoll
When I try to kill it with kill -9 pid I get the error:
$ kill -9 12821
-bash: kill: (12821) - No such process
This is very unusual. How do I resolve this? I'd prefer not to restart docker unless it is the last and only option.
If you kill the container, you cannot exec into it. The container must be running for exec to work (you should get an error message from this).
When the container is not running, there should be no process. However the container definition in docker, including logs, and changes to the container filesystem, will remain until you remove it with docker container rm (same as docker rm), e.g.:
docker container rm brave_carson
As a side note you can use docker run ... --rm ... to automatically remove containers after stop

What is the difference between "docker container prune" vs "docker rm $(docker container ls -aq)"

I'm reading through the Docker documentation and I don't understand the difference between:
docker container prune
and
docker rm $(docker container ls -aq)
Note that in the link, the second command I've listed is docker rm $(docker ps -a -q), but there is no difference between that and what I've written. container ls is just the newer version of the ps command.
It seems that both of these commands remove all stopped containers. Is there more to it than that, or are these just synonyms?
I don't think there is substantial difference. This -a though means list all containers and as a result docker rm ... will also try to remove running containers. This gives the error that you see below:
Error response from daemon: You cannot remove a running container [...] Stop the container before attempting removal or force remove
example:
$ docker container run --rm -itd alpine:latest
0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec4d7459d35 alpine:latest "/bin/sh" 4 seconds ago Up 1 second jovial_ritchie
$ docker rm $(docker container ls -aq)
Error response from daemon: You cannot remove a running container 0ec4d7459d35749ecc24cc5c6fd748f4254b0782f73f1ede76cf49b1fc53b2d4. Stop the container before attempting removal or force remove
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
But... difference when --force, -f is used:
In this case, the commands do 2 different things:
docker rm -f ... Forces the removal of a running container (uses SIGKILL) which means that it will remove running containers.
$ docker rm -f $(docker container ls -aq)
0ec4d7459d35
docker container prune -f will remove all stopped containers without asking for confirmation (no [y/N] prompt will be printed).
$ docker container prune -f
Total reclaimed space: 0B
The effects of the two commands are indeed similar, but there are some nuances to consider:
docker container prune can be used with the --filter option.
docker container prune has a synchronous protection that blocks concurrent prune executions on the daemon.
docker container prune attempts to remove only the containers that are not running, instead of trying to delete all containers and relying on the daemon to throw an exception for those that are not stopped, therefore is quicker and does not generate unnecessary error logs in case someone is tracking the daemon logs.
docker container prune builds a report at the end of its execution, providing the reclaimed space. The report is added in daemon.EventsService and implicitly displayed on the screen.
docker container prune is shorter
In the end of this answer I have a question: Why would someone type 15 additional characters to get the same result or worse?
docker system prune -f : to remove all the stopped containers (docker do not touch the running containers)
docker system prune -a : to remove all the stopped containers (docker do not touch the running containers) + unused images
docker rm <container_id> : remove a specific container, it should be stopped before (docker stop <container_id>)

reset a docker container to its initial state every 24 hours

I need to reset a moodle docker to its initial state every 24 hours. This docker will be a running a demo site where users can login and carry out various setting changes and the site needs to reset itself every day. Does docker provide any such feature?
I searched for a docker reset command but it doesn't seem to be there yet.
Will such a process of removing and reinitiating docker container work?
docker rm -f $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker-compose up -d
I should be able to do this programatically ofcourse, preferably using a shell script.
Yes you do not need to reset just recreate the container is enough but if you bind volumes with the host it will not work if there is anything that pick from persistent storage of the host in docker-compose up.
Write a bash script that will run every 1:00 AM or whatever time you want to create fresh container.
0 0 * * * create_container.sh
create_container.sh
#!/bin/bash
docker-compose rm -f
docker-compose up -d
or you can use your own script as well but if there is bind volumes the clear that files before creating the container.
rm -rf /path/to_host_shared_volume
docker rm -f $(docker ps -a -q)
.
.
.
As the behavour of -v is different it will create directory if not exist.
Or if you want to remove everything then you can use system-prune
#!/bin/bash
docker system prune -f -a --volumes
docker-compose up -d
Remove all unused containers, networks, images (both dangling and unreferenced), and volumes.
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache

single command to stop and remove docker container

Is there any command which can combine the docker stop and docker rm command together ? Each time I want to delete a running container, I need to execute 2 commands sequentially, I wonder if there is a combined command can simplify this process.
docker stop CONTAINER_ID
docker rm CONTATINER_ID
You can use :
docker rm -f CONTAINER_ID
It will remove the container even if it is still running.
https://docs.docker.com/engine/reference/commandline/rm/
You can also run your containers with --rm option (e.g. docker run --rm -it alpine), it will be automatically removed when stopped.
https://docs.docker.com/engine/reference/run/#clean-up---rm
Edit: The rm -f might be dangerous for your data and is best suited for test or development containers. #Bernard's comment on this subject is worth reading.
docker stop CONTAINER_ID | xargs docker rm
You can stop and remove the container with a single command the $_ gives you the last echo
docker stop CONTAINER && docker rm $_
In my case, to remove the running containers I used
docker rm -f $(docker ps -a -q)
In case you also need to remove the images, then run
docker rmi $(docker images -q) afterwards.
Only run docker rmi $(docker images -q) if you want to remove the images.
https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/
You can use kill, and also by using rm and the force flag it will also use kill.
Remove all containers: docker ps -aq | xargs docker rm -f
This will stop and remove all images including running containers as we are using -f
docker rmi -f $(docker images -a -q)
Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove
docker ps -a
To remove: $ docker rm ID_or_Name ID_or_Name
Remove a container upon exit:
If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits.
Run and Remove : docker run --rm image_name
Remove all exited containers:
You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.
List:
docker ps -a -f status=exited
docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter:
Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:
docker ps -a -f status=exited -f status=created
Stop and Remove all the containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
For removing a single container
docker rm -f CONTAINER_ID
For removing all containers
docker rm -f `docker container ps -qa`
To remove all stopped containers docker system prune
To stop live container, docker stop CONTAINER_ID waits 10 sec and it calls docker kill CONTAINER_ID
Or with docker kill CONTAINER_ID, you can immediately stop the container
remove all container with xargs
docker ps -a -q | xargs docker rm
for stop all
sudo docker ps -a -q |sudo xargs docker stop
remove single container
docker rm -f <container_id>

Resources