Skip to content

Instantly share code, notes, and snippets.

@moylop260
Last active September 1, 2022 15:39
Show Gist options
  • Save moylop260/0c53af9fe8267fbdaceb3f086cf04adb to your computer and use it in GitHub Desktop.
Save moylop260/0c53af9fe8267fbdaceb3f086cf04adb to your computer and use it in GitHub Desktop.
script to remove exited containers
# 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