【Docker】Commands used occasionally (bulk delete command, backup data volume)

Batch delete command

Bulk deletion of stopped containers, untagged images, unused volumes, and unused networks

docker system prune

Bulk deletion of stopped containers

docker container prune

Delete all containers at once

docker rm -f `docker ps -a -q`

Batch deletion of unused images

docker image prune

Batch delete untagged images

docker rmi `docker images -f "dangling=true" -q`

Bulk deletion of unused volumes

docker volume prune

Backup/restore Docker data volumes

Backup

Create a container and retrieve a backup (named backup.tar) of “/var/lib/mysql" where MySQL data is stored in the current directory ($PWD) on the host side.

The container is immediately destroyed by the -rm option.

docker run --rm --volumes-from mysql_con -v $PWD:/backup busybox tar cvf /backup/backup.tar /var/lib/mysql

If successful, a backup.tar file will be generated in the host’s current directory.

Restore

Assume that you want to create a new MySQL server container named mysql_con2 and restore backup files to this container. The following command will simultaneously create the data volume mysql_db2.

docker run --name mysql_con2 -v mysql_db2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pass -d mysql

Create a container just for restoring and extract the backup.tar file in the host’s current directory ($PWD).

docker run --rm --volumes-from mysql_con2 -v $PWD:/backup busybox tar xvf /backup/backup.tar

Reference

Docker

Posted by Next-k