What is the command line equivalent of "Clean / Purge Data" in docker? - docker

When I want to remove everything (running or not), I can just go to Troubleshoot and hit the Clean / Purge data button. This would remove all docker data, without resetting settings to factory defaults. Is there a single line command to achieve same thing?
P.S.: I know about docker system prune, but it is not exactly the same. I want to reset everything, not just the unused.

you can use the combination of docker rm to delete running containers and docker system prune to delete everything:
docker rm -f $(docker ps -a -q);docker system prune --volumes -a -f

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

How to Skip Confirmation question in terminal for command - docker system prune -a

I am giving the command via shell script for pruning all docker images and containers. So I gave like this way docker system prune -a -y So that it will bypass the confirmation question. But I am getting unknown flag. So could you help me how can I pass the Y value for this command
docker system prune -af
# verbose way
docker system prune --all --force
Relevant docs for docker system prune. I agree the -y would've been more intuitive to make this command work.

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

How to reset docker-for-mac v2.1.0.0 kubernetes from the command line?

I am constantly creating new clusters for development and I dislike going into "Troubleshooting" every time to reset the cluster.
Is there a quicker way to do this?
Quickest way I can think of is to blat the kubernetes etcd data.
You will need to have "Show system containers" turned on in Docker.
Please note, this is super destructive and not thoroughly tested (other than to see if it runs). There's probably horrible side effects from using it. Docker for Desktop maintains a lot more state on disk than just etcd. There's also an rm -rf in there which you should never trust someone on the internet telling you to run! But here we go...
This will:
Find the k8s_etcd_etcd-* container and remove all etcd data from it.
Force delete all containers with a name of k8s_*.
Docker for Desktop will reinitialise the containers.
Kubernetes will reinitialise the etcd database (hopefully)
One liner:
docker exec $(docker ps -q --filter 'name=k8s_etcd_etcd-*' -l) rm -rf /var/lib/etcd/member \
&& docker rm -f $(docker ps -q --filter 'name=k8s_*')
or as a script with a bit more output:
#!/bin/bash -uex
etcd_container=$(docker ps -q --filter 'name=k8s_etcd_etcd-*' -l)
docker exec "$etcd_container" rm -rf /var/lib/etcd/member;
all_k8s_containers=$(docker ps -q --filter 'name=k8s_*')
docker rm -f $all_k8s_containers

How to setup docker-machine to be as default?

I want to rebuild everything from my docker VM named default, I used docker-compose down but it only removed the containers, all the requirements are still installed and I would like it to be as it was from the beginning so I can 're'-setup everything. Is it possible ?
This will remove all containers, images and volumes
docker rm -f $(docker ps -aq)
docker image rm $(docker image ls -q)
docker volume rm $(docker volume ls -q)
There are other things like networks and secrets that will not be removed, but they should not cause any problems.
If you are using a newer version of docker try the docker system prune -a command instead.
But maybe the --no-cache argumant ist the real solution for your problem. With it, docker will not use the cache and will do a full rebuild of the image.
The simple solution that will ignore previous builds:
docker-compose build --no-cache
But if you want something more destructive:
docker-machine rm default
docker-machine create default
eval $(docker-machine env default)

Resources