Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write shell script for docker system prune as schedule

docker system prune wants answer y/n. How to i pass in shell script my shell script

#!/bin/sh
docker stop registry
docker system prune
docker run -d -p 5000:5000 --restart=always --name registry -v /etc/docker/registry/config.yml:/etc/docker/registry/config.yml registry:2

I'm configured cron expression as every minutes
crontab -e

* * * * * /bin/sh /home/sansli/dc.sh

How do i test? I'm checking the created dated as docker ps -a

like image 203
Sansli Avatar asked Apr 16 '26 16:04

Sansli


1 Answers

The solution is docker system prune -f, which will remove all stopped containers, all unused networks, all dangling images and build caches.

You can use crontab to periodic running this command. Look at this example of crontab:

0 3 * * * /usr/bin/docker system prune -f

You can also put this command into your script:

#!/bin/sh
docker stop registry
docker system prune -f
docker run -d -p 5000:5000 --restart=always --name registry -v /etc/docker/registry/config.yml:/etc/docker/registry/config.yml registry:2
like image 88
Mikołaj Głodziak Avatar answered Apr 19 '26 08:04

Mikołaj Głodziak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!