-
Notifications
You must be signed in to change notification settings - Fork 119
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
Add garbage collection for docker images #2435
base: main
Are you sure you want to change the base?
Conversation
💔 Build Failed
Failed CI StepsHistorycc @jsoriano |
) | ||
|
||
// ByteSize represents the size of a file. | ||
type ByteSize uint64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type mostly copied from https://github.com/elastic/package-spec/blob/964c4a69e024cc464c4808720ba0db9f001a82a7/code/go/internal/spectypes/filesize.go, adding support for GB and floats, to support sizes reported by docker system df
.
@@ -50,6 +51,34 @@ func (eb *envBuilder) build() []string { | |||
return eb.vars | |||
} | |||
|
|||
func runDockerImagesGC(ctx context.Context, project *compose.Project, opts compose.CommandOptions, appConfig *install.ApplicationConfiguration) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added by now only to stack operations, it could be added too to agent and service deployers. It may be specially interesting in the service deployer.
@@ -60,7 +61,23 @@ type Config struct { | |||
Services map[string]service | |||
} | |||
|
|||
// Images lists the images found in the configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only lists images found directly in the docker compose configuration, it won't find images found in Dockerfiles. Maybe we can think in other approaches to find the images to track. Ideas welcome.
Related to #856.
If one is using elastic-package during enough time, Docker storage can end up full of docker images. Once the 90% threshold is reached, behavior can be unpredictable as described in #856.
This change introduces garbage collection for docker images that were downloaded by elastic-package. Before pulling new images, it checks first if there are old unused images or if the images are taking too much space, and remove images accordingly. It only removes images known to be downloaded by elastic-package.
Implementation is based on two methods:
Track
is used to start tracking images, by recording their tags and last used times. It has to be called before pulling the images, it won't track images that are already pulled, to avoid GCing images not managed by elastic-package.Run
runs the garbage collection, based on two parameters:max_unused
can be used to remove images older than some time andmax_total_size
to trigger removals when the total size of images (fromdocker system df
) goes beyond some threshold. Less recently used images are removed first.Tracking of known images is persisted under
~/.elastic-package/cache/docker-images-gc.json
.Garbage collection is disabled by default.
This can be configured from
~/.elastic-package/config.yml
, for example: