-
Notifications
You must be signed in to change notification settings - Fork 0
Add clear-caches directive for reducing Docker image sizes #5
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
48afbca
3c48fe1
4f8e7f8
b93bd3e
13cf2e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,7 @@ func processAptfile(path string, dryRun bool) { | |
| } | ||
|
|
||
| pkgs := make([]aptfile.PackageDirective, 0) | ||
| clearCachesDirectives := make([]aptfile.ClearCachesDirective, 0) | ||
|
|
||
| // First pass, skip package installation (except for .deb files, | ||
| // which can be necessary for setting up repos or keyrings, etc.) | ||
|
|
@@ -117,6 +118,9 @@ func processAptfile(path string, dryRun bool) { | |
| if err := addHold(dir, dryRun); err != nil { | ||
| log.Fatalf("Failed to add hold: %v", err) | ||
| } | ||
| case aptfile.ClearCachesDirective: | ||
| // Defer clear-caches to run after packages are installed | ||
| clearCachesDirectives = append(clearCachesDirectives, dir) | ||
| default: | ||
| log.Fatalf("Unknown directive: %v", d) | ||
| } | ||
|
|
@@ -126,6 +130,13 @@ func processAptfile(path string, dryRun bool) { | |
| if err != nil { | ||
| log.Fatalf("Failed to install packages: %v", err) | ||
| } | ||
|
|
||
| // Execute clear-caches once if any directives exist | ||
| if len(clearCachesDirectives) > 0 { | ||
| if err := clearCaches(dryRun); err != nil { | ||
| log.Fatalf("Failed to clear caches: %v", err) | ||
| } | ||
| } | ||
|
Comment on lines
+134
to
+139
|
||
| } | ||
|
|
||
| func installPackages(pkgs []aptfile.PackageDirective, dryRun bool) error { | ||
|
|
@@ -401,3 +412,35 @@ func addHold(hold aptfile.HoldDirective, dryRun bool) error { | |
| return fixCmd.Run() | ||
| } | ||
| } | ||
|
|
||
| func clearCaches(dryRun bool) error { | ||
| if dryRun { | ||
| fmt.Println("[dry-run] Would run `apt-get clean`") | ||
| fmt.Println("[dry-run] Would remove /var/lib/apt/lists/*") | ||
| return nil | ||
| } | ||
|
|
||
| fmt.Println("Clearing apt caches...") | ||
|
|
||
| // Run apt-get clean to clear package cache | ||
| cleanCmd := exec.Command("apt-get", "clean") | ||
| cleanCmd.Env = append(os.Environ(), "DEBIAN_FRONTEND=noninteractive") | ||
| cleanCmd.Stdout = os.Stdout | ||
| cleanCmd.Stderr = os.Stderr | ||
| if err := cleanCmd.Run(); err != nil { | ||
| return fmt.Errorf("error running apt-get clean: %w", err) | ||
| } | ||
|
|
||
| // Remove apt lists to reduce size further | ||
| // Use sh -c to ensure glob expansion works | ||
| fmt.Println("Removing apt package lists...") | ||
| rmCmd := exec.Command("sh", "-c", "rm -rf /var/lib/apt/lists/*") | ||
| rmCmd.Stdout = os.Stdout | ||
| rmCmd.Stderr = os.Stderr | ||
| if err := rmCmd.Run(); err != nil { | ||
| return fmt.Errorf("error removing apt lists: %w", err) | ||
| } | ||
|
|
||
| fmt.Println("Cache clearing completed") | ||
| return nil | ||
| } | ||
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.
clearCachesDirectivesis only used as a presence check (len(...) > 0) and the directive struct is empty, so storing every occurrence is unnecessary. Consider replacing this slice with a simple boolean (e.g.,clearCachesRequested) to better express intent and avoid redundant allocations/append calls.