Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 49ab8e4

Browse files
committed
Add a new flag to cleanup the filesystem at the end
Currently, kaniko can only build a single image per container run, because the filesystem is full of the content of the first image. When running kaniko in Jenkins, where we need to start the container "doing nothing" first (using the debug kaniko container), and then exec /kaniko/executor, this is a limitation because it means that if we want to build multiple images, we need to start multiple containers - see https://groups.google.com/forum/#!topic/kaniko-users/_7LivHdMdy0 for more details A solution to fix this issue is to add a new flag to cleanup the filesystem at the end - the same way it is done between stages when building a multi-stages image. This way, the same (debug) container can be used to build multiple images.
1 parent 49184c2 commit 49ab8e4

4 files changed

Lines changed: 11 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ If `--destination=gcr.io/kaniko-project/test`, then cached layers will be stored
317317

318318
_This flag must be used in conjunction with the `--cache=true` flag._
319319

320+
#### --cleanup
321+
322+
Set this flag to cleanup the filesystem at the end, leaving a clean kaniko container (if you want to build multiple images in the same container, using the debug kaniko image)
323+
320324
### Debug Image
321325

322326
The kaniko executor image is based off of scratch and doesn't contain a shell.

cmd/executor/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func addKanikoOptionsFlags(cmd *cobra.Command) {
9797
RootCmd.PersistentFlags().BoolVarP(&opts.NoPush, "no-push", "", false, "Do not push the image to the registry")
9898
RootCmd.PersistentFlags().StringVarP(&opts.CacheRepo, "cache-repo", "", "", "Specify a repository to use as a cache, otherwise one will be inferred from the destination provided")
9999
RootCmd.PersistentFlags().BoolVarP(&opts.Cache, "cache", "", false, "Use cache when building image")
100+
RootCmd.PersistentFlags().BoolVarP(&opts.Cleanup, "cleanup", "", false, "Clean the filesystem at the end")
100101
}
101102

102103
// addHiddenFlags marks certain flags as hidden from the executor help text

pkg/config/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ type KanikoOptions struct {
3333
Reproducible bool
3434
NoPush bool
3535
Cache bool
36+
Cleanup bool
3637
}

pkg/executor/build.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
264264
return nil, err
265265
}
266266
}
267+
if opts.Cleanup {
268+
if err = util.DeleteFilesystem(); err != nil {
269+
return nil, err
270+
}
271+
}
267272
return sourceImage, nil
268273
}
269274
if stage.SaveStage {

0 commit comments

Comments
 (0)