Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune unused images correctly #315

Merged
merged 3 commits into from
May 29, 2023
Merged

Prune unused images correctly #315

merged 3 commits into from
May 29, 2023

Conversation

djmb
Copy link
Collaborator

@djmb djmb commented May 25, 2023

dangling=true doesn't prune any images, as we are not creating dangling images.

Using --all should remove unused images, but it considers the Git SHA tag on the latest image to be unused (presumably because there are two tags, the SHA and latest and the running container is only considered to be using "latest"). As a result it deletes the tag, which means that we can't rollback to that SHA later.

Its a bit more complicated to only remove images that are not referenced by any containers.

First we find the tags we want to keep from the containers (running and stopped).

Then we append the latest tag to that list.

Then we get a full list of image tags and remove those tags from that list (using grep -v -w).

Finally we pass the tags to docker rmi. That either deletes the tag if there are other references to the image or both the tag and the image if it is the only one.

djmb added 3 commits May 25, 2023 17:16
dangling=true doesn't prune any images, as we are not creating dangling
images.

Using --all should remove unused images, but it considers the Git SHA
tag on the latest image to be unused (presumably because there are two
tags, the SHA and latest and the running container is only considered to
be using "latest"). As a result it deletes the tag, which means that we
can't rollback to that SHA later.

Its a bit more complicated to only remove images that are not referenced
by any containers.

First we find the tags we want to keep from the containers (running and
stopped).

Then we append the latest tag to that list.

Then we get a full list of image tags and remove those tags from that
list (using `grep -v -w`).

Finally we pass the tags to `docker rmi`. That either deletes the tag if
there are other references to the image or both the tag and the image if
it is the only one.
@dhh dhh merged commit de2de19 into basecamp:main May 29, 2023
@dhh dhh deleted the prune-unused-images branch May 29, 2023 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants