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

Add garbage collection for docker images #2435

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jsoriano
Copy link
Member

@jsoriano jsoriano commented Feb 27, 2025

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 and max_total_size to trigger removals when the total size of images (from docker 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:

stack:
  gc:
    enabled: true
    max_unused: 336h # 2 weeks
    max_total_size: 60GB

@jsoriano jsoriano self-assigned this Feb 27, 2025
@elasticmachine
Copy link
Collaborator

elasticmachine commented Feb 27, 2025

💔 Build Failed

Failed CI Steps

History

cc @jsoriano

)

// ByteSize represents the size of a file.
type ByteSize uint64
Copy link
Member Author

@jsoriano jsoriano Feb 27, 2025

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 {
Copy link
Member Author

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.
Copy link
Member Author

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.

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