linuxbashdockercron

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


Solution

  • 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