Cleanup

2 snippets across 2 stacks — Docker, Python

DKDocker

Dangling Images & Orphan Volumes

DK · Common Mistakes
syntax
# Check dangling images:
docker images -f dangling=true
# Check orphan volumes:
docker volume ls -f dangling=true
example
# Frequent rebuilds leave untagged images:
docker images -f dangling=true
# REPOSITORY  TAG     SIZE
# <none>      <none>  450MB
# <none>      <none>  450MB

# Clean them:
docker image prune
docker volume prune

Note Every time you rebuild with the same tag, the old image loses its tag and becomes <none>:<none>. These pile up fast. Volumes from removed containers also linger. Schedule regular prune commands or add them to your CI cleanup steps.

PYPython

else & finally

PY · Error Handling
syntax
try:
    ...
except:
    ...
else:
    ...
finally:
    ...
example
def safe_divide(a: float, b: float) -> float | None:
    try:
        result = a / b
    except ZeroDivisionError:
        print("Cannot divide by zero")
        return None
    else:
        print(f"Result: {result}")
        return result
    finally:
        print("Division attempted")

safe_divide(10, 3)
safe_divide(10, 0)
output
Result: 3.3333333333333335
Division attempted
Cannot divide by zero
Division attempted

Note else runs only when no exception occurred. finally always runs, even after return statements. Use else to keep the try block minimal.