How to write shell script for docker system prune as schedule
Asked Answered
G

2

8

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

Gloucester answered 16/6, 2021 at 13:1 Comment(1)
docker system prune -fMinutes
H
14

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
Haematoma answered 16/6, 2021 at 13:9 Comment(1)
@Sansli, nice to hear! Please have a look here: What should I do when someone answers my question?Lenee
B
1

This one will also work.

echo y | docker system prune -a
Barreto answered 29/8 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.