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.
39 lines
1.4 KiB
Ruby
39 lines
1.4 KiB
Ruby
require "active_support/duration"
|
|
require "active_support/core_ext/numeric/time"
|
|
|
|
class Mrsk::Commands::Prune < Mrsk::Commands::Base
|
|
def dangling_images
|
|
docker :image, :prune, "--force", "--filter", "label=service=#{config.service}", "--filter", "dangling=true"
|
|
end
|
|
|
|
def tagged_images
|
|
pipe \
|
|
docker(:image, :ls, *service_filter, "--format", "'{{.ID}} {{.Repository}}:{{.Tag}}'"),
|
|
"grep -v -w \"#{active_image_list}\"",
|
|
"while read image tag; do docker rmi $tag; done"
|
|
end
|
|
|
|
def containers(keep_last: 5)
|
|
pipe \
|
|
docker(:ps, "-q", "-a", *service_filter, *stopped_containers_filters),
|
|
"tail -n +#{keep_last + 1}",
|
|
"while read container_id; do docker rm $container_id; done"
|
|
end
|
|
|
|
private
|
|
def stopped_containers_filters
|
|
[ "created", "exited", "dead" ].flat_map { |status| ["--filter", "status=#{status}"] }
|
|
end
|
|
|
|
def active_image_list
|
|
# Pull the images that are used by any containers
|
|
# Append repo:latest - to avoid deleting the latest tag
|
|
# Append repo:<none> - to avoid deleting dangling images that are in use. Unused dangling images are deleted separately
|
|
"$(docker container ls -a --format '{{.Image}}\\|' --filter label=service=#{config.service} | tr -d '\\n')#{config.latest_image}\\|#{config.repository}:<none>"
|
|
end
|
|
|
|
def service_filter
|
|
[ "--filter", "label=service=#{config.service}" ]
|
|
end
|
|
end
|