Last active
September 1, 2022 15:39
-
-
Save moylop260/0c53af9fe8267fbdaceb3f086cf04adb to your computer and use it in GitHub Desktop.
script to remove exited containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://docker-py.readthedocs.io/en/stable/containers.html | |
import docker | |
cli = docker.from_env() | |
all_containers = cli.containers.list(True) | |
exited_containers = [i for i in all_containers if i.status == 'exited'] | |
[i.remove(v=True,force=True) for i in exited_containers] | |
images_exclude = { | |
'vauxoo/docker-postgresql:11-ci', 'vauxoo/odoo-120-image:latest', 'vauxoo/odoo-80-image-shippable-auto:latest', | |
'quay.io/vauxoo/islamicrelief:islamicrelief-14.0-5290f4973a', | |
} | |
all_images = cli.images.list() | |
for image in all_images: | |
if not set(image.tags) & images_exclude: | |
try: | |
cli.images.remove(image.id, force=True) | |
except docker.errors.APIError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment