Skip to content

Commit

Permalink
Merge pull request #315 from basecamp/prune-unused-images
Browse files Browse the repository at this point in the history
Prune unused images correctly
  • Loading branch information
dhh authored May 29, 2023
2 parents f9fbeba + 1e300f3 commit de2de19
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
15 changes: 13 additions & 2 deletions lib/mrsk/commands/prune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

class Mrsk::Commands::Prune < Mrsk::Commands::Base
def images
docker :image, :prune, "--force", "--filter", "label=service=#{config.service}", "--filter", "dangling=true"
pipe \
docker(:image, :ls, *service_filter, "--format", "'{{.Repository}}:{{.Tag}}'"),
"grep -v -w \"#{active_image_list}\"",
"while read tag; do docker rmi $tag; done"
end

def containers(keep_last: 5)
pipe \
docker(:ps, "-q", "-a", "--filter", "label=service=#{config.service}", *stopped_containers_filters),
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
Expand All @@ -17,4 +20,12 @@ def containers(keep_last: 5)
def stopped_containers_filters
[ "created", "exited", "dead" ].flat_map { |status| ["--filter", "status=#{status}"] }
end

def active_image_list
"$(docker container ls -a --format '{{.Image}}\\|' --filter label=service=#{config.service} | tr -d '\\n')#{config.latest_image}"
end

def service_filter
[ "--filter", "label=service=#{config.service}" ]
end
end
2 changes: 1 addition & 1 deletion test/cli/prune_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CliPruneTest < CliTestCase

test "images" do
run_command("images").tap do |output|
assert_match /docker image prune --force --filter label=service=app --filter dangling=true on 1.1.1.\d/, output
assert_match "docker image ls --filter label=service=app --format '{{.Repository}}:{{.Tag}}' | grep -v -w \"$(docker container ls -a --format '{{.Image}}\\|' --filter label=service=app | tr -d '\\n')dhh/app:latest\" | while read tag; do docker rmi $tag; done on 1.1.1.", output
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/commands/prune_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CommandsPruneTest < ActiveSupport::TestCase

test "images" do
assert_equal \
"docker image prune --force --filter label=service=app --filter dangling=true",
"docker image ls --filter label=service=app --format '{{.Repository}}:{{.Tag}}' | grep -v -w \"$(docker container ls -a --format '{{.Image}}\\|' --filter label=service=app | tr -d '\\n')dhh/app:latest\" | while read tag; do docker rmi $tag; done",
new_command.images.join(" ")
end

Expand Down
8 changes: 4 additions & 4 deletions test/integration/docker/load_balancer/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ server {

location / {
proxy_pass http://loadbalancer;
proxy_connect_timeout 5;
proxy_send_timeout 5;
proxy_read_timeout 5;
send_timeout 5;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
send_timeout 10;
}
}
2 changes: 1 addition & 1 deletion test/integration/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def assert_app_not_found
assert_equal "404", response.code
end

def wait_for_app_to_be_up(timeout: 10, up_count: 3)
def wait_for_app_to_be_up(timeout: 20, up_count: 3)
timeout_at = Time.now + timeout
up_times = 0
response = app_response
Expand Down

0 comments on commit de2de19

Please sign in to comment.