Skip to content

Commit

Permalink
build: support --cache-from and --cache-to
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Sep 30, 2021
1 parent 77d602e commit ff8f036
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,10 @@ Flags:
- :whale: `--secret`: Secret file to expose to the build: id=mysecret,src=/local/secret
- :whale: `--ssh`: SSH agent socket or keys to expose to the build (format: `default|<id>[=<socket>|<key>[,<key>]]`)
- :whale: `-q, --quiet`: Suppress the build output and print image ID on success
- :whale: `--cache-from=CACHE`: External cache sources (eg. user/app:cache, type=local,src=path/to/dir) (compatible with `docker buildx build`)
- :whale: `--cache-to=CACHE`: Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir) (compatible with `docker buildx build`)

Unimplemented `docker build` flags: `--add-host`, `--cache-from`, `--iidfile`, `--label`, `--network`, `--platform`, `--squash`
Unimplemented `docker build` flags: `--add-host`, `--iidfile`, `--label`, `--network`, `--platform`, `--squash`

### :whale: nerdctl commit
Create a new image from a container's changes
Expand Down
37 changes: 37 additions & 0 deletions cmd/nerdctl/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"os"
"os/exec"
"strconv"
"strings"

"path/filepath"
Expand Down Expand Up @@ -89,6 +90,14 @@ var buildCommand = &cli.Command{
Aliases: []string{"q"},
Usage: "Suppress the build output and print image ID on success",
},
&cli.StringSliceFlag{
Name: "cache-from",
Usage: "External cache sources (eg. user/app:cache, type=local,src=path/to/dir)",
},
&cli.StringSliceFlag{
Name: "cache-to",
Usage: "Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)",
},
},
}

Expand Down Expand Up @@ -192,6 +201,20 @@ func generateBuildctlArgs(clicontext *cli.Context) (string, []string, bool, erro

for _, ba := range strutil.DedupeStrSlice(clicontext.StringSlice("build-arg")) {
buildctlArgs = append(buildctlArgs, "--opt=build-arg:"+ba)

// Support `--build-arg BUILDKIT_INLINE_CACHE=1` for compatibility with `docker buildx build`
// https://github.com/docker/buildx/blob/v0.6.3/docs/reference/buildx_build.md#-export-build-cache-to-an-external-cache-destination---cache-to
if strings.HasPrefix(ba, "BUILDKIT_INLINE_CACHE=") {
bic := strings.TrimPrefix(ba, "BUILDKIT_INLINE_CACHE=")
bicParsed, err := strconv.ParseBool(bic)
if err == nil {
if bicParsed {
buildctlArgs = append(buildctlArgs, "--export-cache=type=inline")
}
} else {
logrus.WithError(err).Warnf("invalid BUILDKIT_INLINE_CACHE: %q", bic)
}
}
}

if clicontext.Bool("no-cache") {
Expand All @@ -206,5 +229,19 @@ func generateBuildctlArgs(clicontext *cli.Context) (string, []string, bool, erro
buildctlArgs = append(buildctlArgs, "--ssh="+s)
}

for _, s := range strutil.DedupeStrSlice(clicontext.StringSlice("cache-from")) {
if !strings.Contains(s, "type=") {
s = "type=registry,ref=" + s
}
buildctlArgs = append(buildctlArgs, "--import-cache="+s)
}

for _, s := range strutil.DedupeStrSlice(clicontext.StringSlice("cache-to")) {
if !strings.Contains(s, "type=") {
s = "type=registry,ref=" + s
}
buildctlArgs = append(buildctlArgs, "--export-cache="+s)
}

return buildctlBinary, buildctlArgs, needsLoading, nil
}

0 comments on commit ff8f036

Please sign in to comment.