If you different images with the same git SHA, on the second deploy the
tag is moved and the first image becomes untagged. It may however still
be attached to an existing container.
To handle this:
1. Initially prune dangling images - this will remove any untagged
images that are not attached to an existing image
2. Then filter out the untagged images when deleting tagged images - any
that remain will be attached to a container.
The second issue is that `docker container ls -a --format '{{.Image}}`
will sometimes return the image id rather than a tag. This means that
the image doesn't get filtered out when we grep to remove the active
images.
To fix that we'll grep against both the image id and repo:tag.
31 lines
724 B
Ruby
31 lines
724 B
Ruby
class Mrsk::Cli::Prune < Mrsk::Cli::Base
|
|
desc "all", "Prune unused images and stopped containers"
|
|
def all
|
|
with_lock do
|
|
containers
|
|
images
|
|
end
|
|
end
|
|
|
|
desc "images", "Prune dangling images"
|
|
def images
|
|
with_lock do
|
|
on(MRSK.hosts) do
|
|
execute *MRSK.auditor.record("Pruned images"), verbosity: :debug
|
|
execute *MRSK.prune.dangling_images
|
|
execute *MRSK.prune.tagged_images
|
|
end
|
|
end
|
|
end
|
|
|
|
desc "containers", "Prune all stopped containers, except the last 5"
|
|
def containers
|
|
with_lock do
|
|
on(MRSK.hosts) do
|
|
execute *MRSK.auditor.record("Pruned containers"), verbosity: :debug
|
|
execute *MRSK.prune.containers
|
|
end
|
|
end
|
|
end
|
|
end
|