Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused images #3490

Closed
miguelramos opened this issue Jan 2, 2020 · 10 comments
Closed

Remove unused images #3490

miguelramos opened this issue Jan 2, 2020 · 10 comments
Labels
kind/question Questions on the platform

Comments

@miguelramos
Copy link

miguelramos commented Jan 2, 2020

Question:
How can I remove unused images with a cron job?

Current i have this kind of script running:

docker container rm $(docker ps -qa --no-trunc --filter "status=exited") || true >> /var/log/docker/containers.log 2>&1
docker rmi $(docker image ls -aq --filter "dangling=true") || true >> /var/log/docker/images_dangling.log 2>&1

image

But when i check images list in portainer browser, the unused images are there. So how can i remove periodically unused images on portainer to free space?

@tronyx
Copy link

tronyx commented Jan 9, 2020

Portainer cannot do that. Put your above commands into a script and create a cronjob for the script.

@miguelramos
Copy link
Author

Portainer cannot do that. Put your above commands into a script and create a cronjob for the script.

I don't know if you understood but my script example above is a cron job, and unused images are not clean when it runs, I can only remove images if I go to portainer and remove it manually.

@tronyx
Copy link

tronyx commented Jan 11, 2020

You can use docker system prune as well, even in a script/cronjob, to cleanup unused images.

@ghost
Copy link

ghost commented Jan 14, 2020

@christronyxyocum Portainer does in fact have cron job functionality.

  1. Enable host management in settings and browse to the host jobs view
    image

  2. Click on add a schedule
    image

  3. Configure and save the schedule & the job will run as desired.
    image

I just tested this example on Portainer 1.23 to remove un-used images and it worked as intended.
Note that docker image prune -af will remove ALL un-used images

@ghost
Copy link

ghost commented Jan 14, 2020

Here is a command that you can perhaps modify to your liking, this will delete images older than 24 hours
docker image prune --all --filter "until=24h" --force official docker documentation: https://docs.docker.com/config/pruning/#prune-images

Note that --force is required for execution without the confirmation prompt

@ghost ghost added the kind/question Questions on the platform label Jan 14, 2020
@ghost
Copy link

ghost commented Jan 14, 2020

I'll close this as I believe i have answered the question. If needed I can re-open to discuss further

@ghost ghost closed this as completed Jan 14, 2020
@gianlucapisati
Copy link

gianlucapisati commented Apr 29, 2020

I'm having problems with this configuration, I've added the host jobs as suggested, using ubuntu:latest as base image and adding
docker image prune --all --force but the images seems not to be removed.

If I manually launch the container I receive this error:
Failure OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"sh\": executable file not found in $PATH": unknown

I've noticed that the CMD of the container is sh /tmp/script.sh. It seems strange to me!

By the way, it's not clear to me if this command is executed just on the host node or everywhere

@kungl
Copy link

kungl commented May 10, 2020

Never use latest tag, I have the same problem with the ubuntu:latest image, but if you use ubuntu:18.04 it'll work. You can set endpoints in "Target endpoints" section (above the Create sections button) .

@iambryancs
Copy link

@kungl you saved me! Thank you. I think when someone creates a new Schedule under Host Jobs, the Image's value under Job configuration should not use latest as the default tag.

chiptus pushed a commit to chiptus/portainer that referenced this issue Aug 16, 2023
@cinderblock
Copy link

If you're using Watchtower to automatically update containers, and that is generating a lot of unused images, you might want to enable WATCHTOWER_CLEANUP.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question Questions on the platform
Projects
None yet
Development

No branches or pull requests

6 participants