From 4e735ec5d19eb4b123283dcb7a5cbc8e3148b0db Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Mon, 2 Sep 2024 14:57:02 +0100 Subject: [PATCH 1/9] POD-823: Add repo cache --- cmd/build.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 6f31fda..41b1bcd 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -23,8 +23,9 @@ type BuildCmd struct { Target string BuildArgs []string - IgnorePaths []string - Insecure bool + IgnorePaths []string + Insecure bool + RegistryCache string } // NewBuildCmd returns a new build command @@ -46,6 +47,7 @@ func NewBuildCmd() *cobra.Command { cobraCmd.Flags().StringArrayVar(&cmd.BuildArgs, "build-arg", []string{}, "Docker build args.") cobraCmd.Flags().StringArrayVar(&cmd.IgnorePaths, "ignore-path", []string{}, "Extra paths to exclude from deletion.") cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, "If true will not check for certificates") + cobraCmd.Flags().StringVar(&cmd.RegistryCache, "registry-cache", "gcr.io/pascal-project-387807/my-dev-env", "Registry to use as remote cache.") return cobraCmd } @@ -129,8 +131,7 @@ func (cmd *BuildCmd) build() (v1.Image, error) { return nil, fmt.Errorf("change dir: %w", err) } - // let's build! - image, err := executor.DoBuild(&config.KanikoOptions{ + opts := &config.KanikoOptions{ Destinations: []string{"local"}, Unpack: true, BuildArgs: cmd.BuildArgs, @@ -147,6 +148,7 @@ func (cmd *BuildCmd) build() (v1.Image, error) { RunV2: true, NoPush: true, KanikoDir: "/.dockerless", + Cache: true, CacheRunLayers: true, CacheCopyLayers: true, CompressedCaching: true, @@ -154,10 +156,17 @@ func (cmd *BuildCmd) build() (v1.Image, error) { Compression: config.ZStd, CompressionLevel: 3, CacheOptions: config.CacheOptions{ - CacheDir: "/.dockerless/cache", CacheTTL: time.Hour * 24 * 7, }, - }) + } + if cmd.RegistryCache != "" { + opts.CacheRepo = cmd.RegistryCache + } else { + opts.CacheOptions.CacheDir = "/.dockerless/cache" + } + + // let's build! + image, err := executor.DoBuild(opts) if err != nil { // add a passwd as other we won't be able to exec into this container if addPwdErr := addPasswd(); addPwdErr != nil { From 3846554c7928033689d97346c1ac16ed5775030b Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Mon, 2 Sep 2024 15:05:11 +0100 Subject: [PATCH 2/9] POD-823: Update docs --- Readme.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c8e1841..ed9fd9f 100644 --- a/Readme.md +++ b/Readme.md @@ -9,7 +9,9 @@ Dockerless is a tool that allows you to build and run Docker containers without ## Installation -For example, to build an image: +WARN: running dockerless on your host can delete key files and destroy your working environment. + +For example, to build an image, run the following in the gcr.io/kaniko-project/executor image: ``` bash dockerless build --dockerfile Dockerfile --context . @@ -20,3 +22,13 @@ And to run a container: ``` bash dockerless start ``` + +## Development + +Build dockerless and new image + +```bash +just build +cp dist/dockerless_{arch}/dockerless . +docker build -t {repo}/dockerless:{tag} -f Dockerfile --build-arg TARGETARCH=$(uname -m) --build-arg TARGETOS=linux . +``` \ No newline at end of file From e6e8d98aa38727d65132d72cb4a4a29f731d87c4 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Mon, 2 Sep 2024 15:47:40 +0100 Subject: [PATCH 3/9] POD-823: Fix go vet --- cmd/build.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 41b1bcd..9152cc4 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -23,9 +23,9 @@ type BuildCmd struct { Target string BuildArgs []string - IgnorePaths []string - Insecure bool - RegistryCache string + IgnorePaths []string + Insecure bool + Registry string } // NewBuildCmd returns a new build command @@ -47,7 +47,7 @@ func NewBuildCmd() *cobra.Command { cobraCmd.Flags().StringArrayVar(&cmd.BuildArgs, "build-arg", []string{}, "Docker build args.") cobraCmd.Flags().StringArrayVar(&cmd.IgnorePaths, "ignore-path", []string{}, "Extra paths to exclude from deletion.") cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, "If true will not check for certificates") - cobraCmd.Flags().StringVar(&cmd.RegistryCache, "registry-cache", "gcr.io/pascal-project-387807/my-dev-env", "Registry to use as remote cache.") + cobraCmd.Flags().StringVar(&cmd.Registry, "registry-cache", "gcr.io/pascal-project-387807/my-dev-env", "Registry to use as remote cache.") return cobraCmd } @@ -159,8 +159,8 @@ func (cmd *BuildCmd) build() (v1.Image, error) { CacheTTL: time.Hour * 24 * 7, }, } - if cmd.RegistryCache != "" { - opts.CacheRepo = cmd.RegistryCache + if cmd.Registry != "" { + opts.CacheRepo = cmd.Registry } else { opts.CacheOptions.CacheDir = "/.dockerless/cache" } From bc9531159d11702c3ba00f9c356ba3663853bdc0 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Mon, 2 Sep 2024 15:54:24 +0100 Subject: [PATCH 4/9] POD-823: Fix go vet --- cmd/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/build.go b/cmd/build.go index 9152cc4..42a16d4 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -24,8 +24,8 @@ type BuildCmd struct { BuildArgs []string IgnorePaths []string - Insecure bool Registry string + Insecure bool } // NewBuildCmd returns a new build command From 1142bc1735c6d509519c1411d99a045dbf1326aa Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Mon, 2 Sep 2024 16:01:37 +0100 Subject: [PATCH 5/9] POD-823: Fix go vet --- cmd/build.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 42a16d4..e56d351 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -17,14 +17,12 @@ import ( var ImageConfigOutput = "/.dockerless/image.json" type BuildCmd struct { - Dockerfile string - Context string - - Target string - BuildArgs []string - - IgnorePaths []string + Dockerfile string + Context string + Target string Registry string + BuildArgs []string + IgnorePaths []string Insecure bool } From 1d6d53abf7682572aaae411c278b244d9d4a0e60 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Wed, 4 Sep 2024 13:09:54 +0100 Subject: [PATCH 6/9] POD-823: Remove test default --- cmd/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/build.go b/cmd/build.go index e56d351..2e5e2cc 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -45,7 +45,7 @@ func NewBuildCmd() *cobra.Command { cobraCmd.Flags().StringArrayVar(&cmd.BuildArgs, "build-arg", []string{}, "Docker build args.") cobraCmd.Flags().StringArrayVar(&cmd.IgnorePaths, "ignore-path", []string{}, "Extra paths to exclude from deletion.") cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, "If true will not check for certificates") - cobraCmd.Flags().StringVar(&cmd.Registry, "registry-cache", "gcr.io/pascal-project-387807/my-dev-env", "Registry to use as remote cache.") + cobraCmd.Flags().StringVar(&cmd.Registry, "registry-cache", "", "Registry to use as remote cache.") return cobraCmd } From 4fd43deb3a9887043fc017e782b2989b316917d0 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Thu, 5 Sep 2024 12:21:40 +0100 Subject: [PATCH 7/9] POD-823: Optimize snapshotting --- cmd/build.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 2e5e2cc..3e64f06 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -24,6 +24,7 @@ type BuildCmd struct { BuildArgs []string IgnorePaths []string Insecure bool + ExportCache bool } // NewBuildCmd returns a new build command @@ -46,6 +47,7 @@ func NewBuildCmd() *cobra.Command { cobraCmd.Flags().StringArrayVar(&cmd.IgnorePaths, "ignore-path", []string{}, "Extra paths to exclude from deletion.") cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, "If true will not check for certificates") cobraCmd.Flags().StringVar(&cmd.Registry, "registry-cache", "", "Registry to use as remote cache.") + cobraCmd.Flags().BoolVar(&cmd.ExportCache, "export-cache", false, "If true kanoiko build push cache to registry.") return cobraCmd } @@ -139,20 +141,22 @@ func (cmd *BuildCmd) build() (v1.Image, error) { InsecurePull: cmd.Insecure, SkipTLSVerify: cmd.Insecure, }, - SrcContext: cmd.Context, - Target: cmd.Target, - CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), - SnapshotMode: "time", - RunV2: true, - NoPush: true, - KanikoDir: "/.dockerless", - Cache: true, - CacheRunLayers: true, - CacheCopyLayers: true, - CompressedCaching: true, - SkipUnusedStages: true, - Compression: config.ZStd, - CompressionLevel: 3, + SrcContext: cmd.Context, + Target: cmd.Target, + CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), + SnapshotMode: "redo", + RunV2: true, + NoPush: true, + KanikoDir: "/.dockerless", + Cache: true, + CacheRunLayers: true, + CacheCopyLayers: true, + CompressedCaching: true, + SkipUnusedStages: true, + ImageFSExtractRetry: 3, + NoPushCache: !cmd.ExportCache, + Compression: config.ZStd, + CompressionLevel: 3, CacheOptions: config.CacheOptions{ CacheTTL: time.Hour * 24 * 7, }, @@ -162,6 +166,9 @@ func (cmd *BuildCmd) build() (v1.Image, error) { } else { opts.CacheOptions.CacheDir = "/.dockerless/cache" } + if !cmd.ExportCache { + opts.SingleSnapshot = true + } // let's build! image, err := executor.DoBuild(opts) From 6463a45e212ee092f7ebbcf1da7c745566baeb83 Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Thu, 5 Sep 2024 13:44:18 +0100 Subject: [PATCH 8/9] POD-823: PR comments --- cmd/build.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index 3e64f06..cd24087 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -14,17 +14,19 @@ import ( "github.com/spf13/cobra" ) +const DEFAULT_CACHE_DIR = "/.dockerless/cache" + var ImageConfigOutput = "/.dockerless/image.json" type BuildCmd struct { - Dockerfile string - Context string - Target string - Registry string - BuildArgs []string - IgnorePaths []string - Insecure bool - ExportCache bool + Dockerfile string + Context string + Target string + RegistryCache string + BuildArgs []string + IgnorePaths []string + Insecure bool + ExportCache bool } // NewBuildCmd returns a new build command @@ -46,7 +48,7 @@ func NewBuildCmd() *cobra.Command { cobraCmd.Flags().StringArrayVar(&cmd.BuildArgs, "build-arg", []string{}, "Docker build args.") cobraCmd.Flags().StringArrayVar(&cmd.IgnorePaths, "ignore-path", []string{}, "Extra paths to exclude from deletion.") cobraCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, "If true will not check for certificates") - cobraCmd.Flags().StringVar(&cmd.Registry, "registry-cache", "", "Registry to use as remote cache.") + cobraCmd.Flags().StringVar(&cmd.RegistryCache, "registry-cache", "", "Registry to use as remote cache.") cobraCmd.Flags().BoolVar(&cmd.ExportCache, "export-cache", false, "If true kanoiko build push cache to registry.") return cobraCmd } @@ -161,10 +163,10 @@ func (cmd *BuildCmd) build() (v1.Image, error) { CacheTTL: time.Hour * 24 * 7, }, } - if cmd.Registry != "" { - opts.CacheRepo = cmd.Registry + if cmd.RegistryCache != "" { + opts.CacheRepo = cmd.RegistryCache } else { - opts.CacheOptions.CacheDir = "/.dockerless/cache" + opts.CacheOptions.CacheDir = DEFAULT_CACHE_DIR } if !cmd.ExportCache { opts.SingleSnapshot = true From 635988840ac83f582066f59196f1573607c73cff Mon Sep 17 00:00:00 2001 From: Bryan Kneis Date: Thu, 5 Sep 2024 14:10:37 +0100 Subject: [PATCH 9/9] POD-823: PR feedback --- cmd/build.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/build.go b/cmd/build.go index cd24087..21d94be 100644 --- a/cmd/build.go +++ b/cmd/build.go @@ -14,7 +14,7 @@ import ( "github.com/spf13/cobra" ) -const DEFAULT_CACHE_DIR = "/.dockerless/cache" +const defaultCacheDir = "/.dockerless/cache" var ImageConfigOutput = "/.dockerless/image.json" @@ -166,7 +166,7 @@ func (cmd *BuildCmd) build() (v1.Image, error) { if cmd.RegistryCache != "" { opts.CacheRepo = cmd.RegistryCache } else { - opts.CacheOptions.CacheDir = DEFAULT_CACHE_DIR + opts.CacheOptions.CacheDir = defaultCacheDir } if !cmd.ExportCache { opts.SingleSnapshot = true