2019-03-24 12:30:29 +08:00
package commands
import (
2021-12-19 14:08:43 +08:00
"bytes"
2019-03-24 12:30:29 +08:00
"context"
2022-02-11 15:15:47 +08:00
"encoding/base64"
2022-05-24 19:00:55 +08:00
"encoding/csv"
2021-06-30 13:41:21 +08:00
"encoding/json"
2021-08-20 22:09:56 +08:00
"fmt"
2021-12-19 14:08:43 +08:00
"io"
2019-03-24 12:30:29 +08:00
"os"
2019-10-21 14:02:37 +08:00
"path/filepath"
2022-05-24 19:00:55 +08:00
"strconv"
2019-03-24 12:30:29 +08:00
"strings"
2022-05-24 19:00:55 +08:00
"sync"
2019-03-24 12:30:29 +08:00
2022-05-24 19:00:55 +08:00
"github.com/containerd/console"
2019-04-25 10:29:56 +08:00
"github.com/docker/buildx/build"
2022-12-06 02:57:35 +08:00
"github.com/docker/buildx/builder"
2022-05-24 19:00:55 +08:00
"github.com/docker/buildx/monitor"
2021-04-09 14:20:26 +08:00
"github.com/docker/buildx/util/buildflags"
2021-10-30 12:15:04 +08:00
"github.com/docker/buildx/util/confutil"
2022-11-28 22:47:40 +08:00
"github.com/docker/buildx/util/dockerutil"
2019-04-25 10:29:56 +08:00
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
2021-07-03 12:39:57 +08:00
"github.com/docker/buildx/util/tracing"
2021-11-22 17:51:54 +08:00
"github.com/docker/cli-docs-tool/annotation"
2019-03-24 12:30:29 +08:00
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
2022-07-06 16:47:29 +08:00
"github.com/docker/cli/cli/config"
2021-10-26 18:53:35 +08:00
dockeropts "github.com/docker/cli/opts"
2021-12-16 13:02:31 +08:00
"github.com/docker/distribution/reference"
2021-06-30 13:41:21 +08:00
"github.com/docker/docker/pkg/ioutils"
2021-10-15 22:03:44 +08:00
"github.com/docker/go-units"
2019-04-17 14:15:58 +08:00
"github.com/moby/buildkit/client"
2019-03-24 12:30:29 +08:00
"github.com/moby/buildkit/session/auth/authprovider"
2021-12-19 14:08:43 +08:00
"github.com/moby/buildkit/solver/errdefs"
2019-03-24 12:30:29 +08:00
"github.com/moby/buildkit/util/appcontext"
2022-01-05 14:34:30 +08:00
"github.com/moby/buildkit/util/grpcerrors"
2021-12-19 14:08:43 +08:00
"github.com/morikuni/aec"
2019-04-17 01:55:13 +08:00
"github.com/pkg/errors"
2021-10-27 03:36:37 +08:00
"github.com/sirupsen/logrus"
2019-03-24 12:30:29 +08:00
"github.com/spf13/cobra"
2019-04-05 15:04:19 +08:00
"github.com/spf13/pflag"
2022-01-05 14:34:30 +08:00
"google.golang.org/grpc/codes"
2019-03-24 12:30:29 +08:00
)
2021-06-30 13:41:21 +08:00
const defaultTargetName = "default"
2019-03-24 12:30:29 +08:00
type buildOptions struct {
contextPath string
dockerfileName string
2022-05-05 09:48:50 +08:00
printFunc string
2019-04-17 01:55:13 +08:00
2021-11-23 12:21:35 +08:00
allow [ ] string
2022-12-08 02:44:11 +08:00
attests [ ] string
2021-11-23 12:21:35 +08:00
buildArgs [ ] string
cacheFrom [ ] string
cacheTo [ ] string
cgroupParent string
contexts [ ] string
extraHosts [ ] string
imageIDFile string
2022-11-16 22:22:33 +08:00
invoke string
2021-11-23 12:21:35 +08:00
labels [ ] string
networkMode string
noCacheFilter [ ] string
outputs [ ] string
platforms [ ] string
quiet bool
secrets [ ] string
shmSize dockeropts . MemBytes
ssh [ ] string
tags [ ] string
target string
ulimits * dockeropts . UlimitOpt
2021-10-26 18:53:35 +08:00
commonOptions
2019-03-24 12:30:29 +08:00
}
2019-04-05 15:04:19 +08:00
type commonOptions struct {
2021-06-30 13:41:21 +08:00
builder string
2021-10-26 18:53:35 +08:00
metadataFile string
2021-06-30 13:41:21 +08:00
noCache * bool
progress string
pull * bool
2021-10-26 18:53:35 +08:00
2019-10-17 06:04:54 +08:00
exportPush bool
exportLoad bool
2022-12-08 02:44:11 +08:00
sbom string
provenance string
2019-04-05 15:04:19 +08:00
}
2021-07-03 12:39:57 +08:00
func runBuild ( dockerCli command . Cli , in buildOptions ) ( err error ) {
2019-03-24 12:30:29 +08:00
ctx := appcontext . Context ( )
2021-07-03 12:39:57 +08:00
ctx , end , err := tracing . TraceCurrentCommand ( ctx , "build" )
if err != nil {
return err
}
defer func ( ) {
end ( err )
} ( )
2019-10-17 06:10:17 +08:00
noCache := false
if in . noCache != nil {
noCache = * in . noCache
}
pull := false
if in . pull != nil {
pull = * in . pull
}
2022-01-21 01:50:33 +08:00
if noCache && len ( in . noCacheFilter ) > 0 {
return errors . Errorf ( "--no-cache and --no-cache-filter cannot currently be used together" )
}
2022-10-25 17:55:36 +08:00
if in . quiet && in . progress != progress . PrinterModeAuto && in . progress != progress . PrinterModeQuiet {
2021-08-19 11:05:15 +08:00
return errors . Errorf ( "progress=%s and quiet cannot be used together" , in . progress )
} else if in . quiet {
in . progress = "quiet"
}
2021-12-16 13:02:31 +08:00
contexts , err := parseContextNames ( in . contexts )
if err != nil {
return err
}
2022-08-05 15:25:39 +08:00
printFunc , err := parsePrintFunc ( in . printFunc )
if err != nil {
return err
}
2019-03-24 12:30:29 +08:00
opts := build . Options {
Inputs : build . Inputs {
ContextPath : in . contextPath ,
DockerfilePath : in . dockerfileName ,
InStream : os . Stdin ,
2021-12-16 13:02:31 +08:00
NamedContexts : contexts ,
2019-03-24 12:30:29 +08:00
} ,
2021-11-23 12:21:35 +08:00
BuildArgs : listToMap ( in . buildArgs , true ) ,
ExtraHosts : in . extraHosts ,
ImageIDFile : in . imageIDFile ,
Labels : listToMap ( in . labels , false ) ,
NetworkMode : in . networkMode ,
NoCache : noCache ,
NoCacheFilter : in . noCacheFilter ,
Pull : pull ,
ShmSize : in . shmSize ,
Tags : in . tags ,
Target : in . target ,
Ulimits : in . ulimits ,
2022-08-05 15:25:39 +08:00
PrintFunc : printFunc ,
2019-03-24 12:30:29 +08:00
}
2019-04-18 07:48:52 +08:00
platforms , err := platformutil . Parse ( in . platforms )
2019-03-24 12:30:29 +08:00
if err != nil {
return err
}
opts . Platforms = platforms
2022-07-06 16:47:29 +08:00
dockerConfig := config . LoadDefaultConfigFile ( os . Stderr )
opts . Session = append ( opts . Session , authprovider . NewDockerAuthProvider ( dockerConfig ) )
2019-03-24 12:30:29 +08:00
2021-04-09 14:20:26 +08:00
secrets , err := buildflags . ParseSecretSpecs ( in . secrets )
2019-03-24 12:30:29 +08:00
if err != nil {
return err
}
opts . Session = append ( opts . Session , secrets )
2021-04-02 02:08:56 +08:00
sshSpecs := in . ssh
2021-04-09 14:20:26 +08:00
if len ( sshSpecs ) == 0 && buildflags . IsGitSSH ( in . contextPath ) {
2021-04-02 02:08:56 +08:00
sshSpecs = [ ] string { "default" }
}
2021-04-09 14:20:26 +08:00
ssh , err := buildflags . ParseSSHSpecs ( sshSpecs )
2019-03-24 12:30:29 +08:00
if err != nil {
return err
}
opts . Session = append ( opts . Session , ssh )
2021-04-09 14:20:26 +08:00
outputs , err := buildflags . ParseOutputs ( in . outputs )
2019-03-24 12:30:29 +08:00
if err != nil {
return err
}
2019-04-17 14:15:58 +08:00
if in . exportPush {
if in . exportLoad {
return errors . Errorf ( "push and load may not be set together at the moment" )
}
if len ( outputs ) == 0 {
outputs = [ ] client . ExportEntry { {
Type : "image" ,
Attrs : map [ string ] string {
"push" : "true" ,
} ,
} }
} else {
switch outputs [ 0 ] . Type {
case "image" :
outputs [ 0 ] . Attrs [ "push" ] = "true"
default :
return errors . Errorf ( "push and %q output can't be used together" , outputs [ 0 ] . Type )
}
}
}
if in . exportLoad {
if len ( outputs ) == 0 {
outputs = [ ] client . ExportEntry { {
Type : "docker" ,
Attrs : map [ string ] string { } ,
} }
} else {
switch outputs [ 0 ] . Type {
case "docker" :
default :
return errors . Errorf ( "load and %q output can't be used together" , outputs [ 0 ] . Type )
}
}
}
2019-03-24 12:30:29 +08:00
opts . Exports = outputs
2022-12-08 02:44:11 +08:00
inAttests := append ( [ ] string { } , in . attests ... )
if in . provenance != "" {
inAttests = append ( inAttests , buildflags . CanonicalizeAttest ( "provenance" , in . provenance ) )
}
if in . sbom != "" {
inAttests = append ( inAttests , buildflags . CanonicalizeAttest ( "sbom" , in . sbom ) )
}
opts . Attests , err = buildflags . ParseAttests ( inAttests )
if err != nil {
return err
}
2021-04-09 14:20:26 +08:00
cacheImports , err := buildflags . ParseCacheEntry ( in . cacheFrom )
2019-04-18 14:07:01 +08:00
if err != nil {
return err
}
opts . CacheFrom = cacheImports
2021-04-09 14:20:26 +08:00
cacheExports , err := buildflags . ParseCacheEntry ( in . cacheTo )
2019-04-18 14:07:01 +08:00
if err != nil {
return err
}
opts . CacheTo = cacheExports
2021-04-09 14:20:26 +08:00
allow , err := buildflags . ParseEntitlements ( in . allow )
2019-07-09 06:58:38 +08:00
if err != nil {
return err
}
opts . Allow = allow
2019-10-21 14:02:37 +08:00
// key string used for kubernetes "sticky" mode
contextPathHash , err := filepath . Abs ( in . contextPath )
if err != nil {
contextPathHash = in . contextPath
}
2022-12-06 02:57:35 +08:00
b , err := builder . New ( dockerCli ,
builder . WithName ( in . builder ) ,
builder . WithContextPathHash ( contextPathHash ) ,
)
if err != nil {
return err
}
nodes , err := b . LoadNodes ( ctx , false )
if err != nil {
return err
}
imageID , res , err := buildTargets ( ctx , dockerCli , nodes , map [ string ] build . Options { defaultTargetName : opts } , in . progress , in . metadataFile , in . invoke != "" )
2022-02-23 13:55:44 +08:00
err = wrapBuildError ( err , false )
2021-08-20 22:09:56 +08:00
if err != nil {
return err
}
2022-05-24 19:00:55 +08:00
if in . invoke != "" {
cfg , err := parseInvokeConfig ( in . invoke )
if err != nil {
return err
}
cfg . ResultCtx = res
con := console . Current ( )
if err := con . SetRaw ( ) ; err != nil {
return errors . Errorf ( "failed to configure terminal: %v" , err )
}
err = monitor . RunMonitor ( ctx , cfg , func ( ctx context . Context ) ( * build . ResultContext , error ) {
2022-12-06 02:57:35 +08:00
_ , rr , err := buildTargets ( ctx , dockerCli , nodes , map [ string ] build . Options { defaultTargetName : opts } , in . progress , in . metadataFile , true )
2022-05-24 19:00:55 +08:00
return rr , err
} , io . NopCloser ( os . Stdin ) , nopCloser { os . Stdout } , nopCloser { os . Stderr } )
if err != nil {
logrus . Warnf ( "failed to run monitor: %v" , err )
}
con . Reset ( )
}
2021-08-20 22:09:56 +08:00
if in . quiet {
fmt . Println ( imageID )
}
return nil
2019-04-05 15:04:19 +08:00
}
2022-05-24 19:00:55 +08:00
type nopCloser struct {
io . WriteCloser
}
func ( c nopCloser ) Close ( ) error { return nil }
2022-12-06 02:57:35 +08:00
func buildTargets ( ctx context . Context , dockerCli command . Cli , nodes [ ] builder . Node , opts map [ string ] build . Options , progressMode string , metadataFile string , allowNoOutput bool ) ( imageID string , res * build . ResultContext , err error ) {
2019-03-24 12:30:29 +08:00
ctx2 , cancel := context . WithCancel ( context . TODO ( ) )
defer cancel ( )
2021-08-19 11:05:15 +08:00
2022-10-25 17:55:36 +08:00
printer , err := progress . NewPrinter ( ctx2 , os . Stderr , os . Stderr , progressMode )
if err != nil {
return "" , nil , err
}
2020-09-21 10:46:39 +08:00
2022-05-24 19:00:55 +08:00
var mu sync . Mutex
var idx int
2022-12-06 02:57:35 +08:00
resp , err := build . BuildWithResultHandler ( ctx , nodes , opts , dockerutil . NewClient ( dockerCli ) , confutil . ConfigDir ( dockerCli ) , printer , func ( driverIndex int , gotRes * build . ResultContext ) {
2022-05-24 19:00:55 +08:00
mu . Lock ( )
defer mu . Unlock ( )
if res == nil || driverIndex < idx {
idx , res = driverIndex , gotRes
}
2022-08-08 16:07:09 +08:00
} , allowNoOutput )
2020-09-21 10:46:39 +08:00
err1 := printer . Wait ( )
if err == nil {
err = err1
}
2021-06-30 13:41:21 +08:00
if err != nil {
2022-05-24 19:00:55 +08:00
return "" , nil , err
2021-06-30 13:41:21 +08:00
}
if len ( metadataFile ) > 0 && resp != nil {
2022-02-11 15:15:47 +08:00
if err := writeMetadataFile ( metadataFile , decodeExporterResponse ( resp [ defaultTargetName ] . ExporterResponse ) ) ; err != nil {
2022-05-24 19:00:55 +08:00
return "" , nil , err
2021-06-30 13:41:21 +08:00
}
}
2019-03-24 12:30:29 +08:00
2021-12-19 14:08:43 +08:00
printWarnings ( os . Stderr , printer . Warnings ( ) , progressMode )
2022-05-05 09:48:50 +08:00
for k := range resp {
2022-08-05 15:25:39 +08:00
if opts [ k ] . PrintFunc != nil {
2022-05-05 09:48:50 +08:00
if err := printResult ( opts [ k ] . PrintFunc , resp [ k ] . ExporterResponse ) ; err != nil {
return "" , nil , err
}
}
}
2022-05-24 19:00:55 +08:00
return resp [ defaultTargetName ] . ExporterResponse [ "containerimage.digest" ] , res , err
}
func parseInvokeConfig ( invoke string ) ( cfg build . ContainerConfig , err error ) {
2022-09-20 23:23:18 +08:00
cfg . Tty = true
if invoke == "default" {
return cfg , nil
}
2022-05-24 19:00:55 +08:00
csvReader := csv . NewReader ( strings . NewReader ( invoke ) )
fields , err := csvReader . Read ( )
if err != nil {
return cfg , err
}
if len ( fields ) == 1 && ! strings . Contains ( fields [ 0 ] , "=" ) {
2022-09-20 23:23:18 +08:00
cfg . Cmd = [ ] string { fields [ 0 ] }
2022-05-24 19:00:55 +08:00
return cfg , nil
}
for _ , field := range fields {
parts := strings . SplitN ( field , "=" , 2 )
if len ( parts ) != 2 {
return cfg , errors . Errorf ( "invalid value %s" , field )
}
key := strings . ToLower ( parts [ 0 ] )
value := parts [ 1 ]
switch key {
case "args" :
2022-09-20 23:23:18 +08:00
cfg . Cmd = append ( cfg . Cmd , value ) // TODO: support JSON
2022-05-24 19:00:55 +08:00
case "entrypoint" :
2022-09-20 23:23:18 +08:00
cfg . Entrypoint = append ( cfg . Entrypoint , value ) // TODO: support JSON
2022-05-24 19:00:55 +08:00
case "env" :
cfg . Env = append ( cfg . Env , value )
case "user" :
2022-09-20 23:23:18 +08:00
cfg . User = & value
2022-05-24 19:00:55 +08:00
case "cwd" :
2022-09-20 23:23:18 +08:00
cfg . Cwd = & value
2022-05-24 19:00:55 +08:00
case "tty" :
cfg . Tty , err = strconv . ParseBool ( value )
if err != nil {
return cfg , errors . Errorf ( "failed to parse tty: %v" , err )
}
default :
return cfg , errors . Errorf ( "unknown key %q" , key )
}
}
return cfg , nil
2019-03-24 12:30:29 +08:00
}
2021-12-19 14:08:43 +08:00
func printWarnings ( w io . Writer , warnings [ ] client . VertexWarning , mode string ) {
if len ( warnings ) == 0 || mode == progress . PrinterModeQuiet {
return
}
fmt . Fprintf ( w , "\n " )
sb := & bytes . Buffer { }
if len ( warnings ) == 1 {
fmt . Fprintf ( sb , "1 warning found" )
} else {
fmt . Fprintf ( sb , "%d warnings found" , len ( warnings ) )
}
if logrus . GetLevel ( ) < logrus . DebugLevel {
fmt . Fprintf ( sb , " (use --debug to expand)" )
}
fmt . Fprintf ( sb , ":\n" )
fmt . Fprint ( w , aec . Apply ( sb . String ( ) , aec . YellowF ) )
for _ , warn := range warnings {
fmt . Fprintf ( w , " - %s\n" , warn . Short )
if logrus . GetLevel ( ) < logrus . DebugLevel {
continue
}
for _ , d := range warn . Detail {
fmt . Fprintf ( w , "%s\n" , d )
}
if warn . URL != "" {
fmt . Fprintf ( w , "More info: %s\n" , warn . URL )
}
if warn . SourceInfo != nil && warn . Range != nil {
src := errdefs . Source {
Info : warn . SourceInfo ,
Ranges : warn . Range ,
}
src . Print ( w )
}
fmt . Fprintf ( w , "\n" )
}
}
2021-10-15 22:03:44 +08:00
func newBuildOptions ( ) buildOptions {
ulimits := make ( map [ string ] * units . Ulimit )
return buildOptions {
2021-10-26 18:53:35 +08:00
ulimits : dockeropts . NewUlimitOpt ( & ulimits ) ,
2021-10-15 22:03:44 +08:00
}
}
2020-04-28 05:37:17 +08:00
func buildCmd ( dockerCli command . Cli , rootOpts * rootOptions ) * cobra . Command {
2021-10-15 22:03:44 +08:00
options := newBuildOptions ( )
2019-03-24 12:30:29 +08:00
cmd := & cobra . Command {
Use : "build [OPTIONS] PATH | URL | -" ,
Aliases : [ ] string { "b" } ,
Short : "Start a build" ,
Args : cli . ExactArgs ( 1 ) ,
2020-05-01 04:01:45 +08:00
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
options . contextPath = args [ 0 ]
options . builder = rootOpts . builder
2021-10-27 03:36:37 +08:00
cmd . Flags ( ) . VisitAll ( checkWarnedFlags )
2020-05-01 04:01:45 +08:00
return runBuild ( dockerCli , options )
} ,
2019-03-24 12:30:29 +08:00
}
2021-10-26 18:53:35 +08:00
var platformsDefault [ ] string
if v := os . Getenv ( "DOCKER_DEFAULT_PLATFORM" ) ; v != "" {
platformsDefault = [ ] string { v }
}
2019-03-24 12:30:29 +08:00
flags := cmd . Flags ( )
2021-11-22 17:51:54 +08:00
flags . StringSliceVar ( & options . extraHosts , "add-host" , [ ] string { } , ` Add a custom host-to-IP mapping (format: "host:ip") ` )
flags . SetAnnotation ( "add-host" , annotation . ExternalURL , [ ] string { "https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host" } )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringSliceVar ( & options . allow , "allow" , [ ] string { } , ` Allow extra privileged entitlement (e.g., "network.host", "security.insecure") ` )
2019-04-17 14:15:58 +08:00
2019-03-24 12:30:29 +08:00
flags . StringArrayVar ( & options . buildArgs , "build-arg" , [ ] string { } , "Set build-time variables" )
2021-03-24 02:07:57 +08:00
2021-11-22 17:51:54 +08:00
flags . StringArrayVar ( & options . cacheFrom , "cache-from" , [ ] string { } , ` External cache sources (e.g., "user/app:cache", "type=local,src=path/to/dir") ` )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringArrayVar ( & options . cacheTo , "cache-to" , [ ] string { } , ` Cache export destinations (e.g., "user/app:cache", "type=local,dest=path/to/dir") ` )
2021-10-26 18:53:35 +08:00
2021-10-29 01:34:33 +08:00
flags . StringVar ( & options . cgroupParent , "cgroup-parent" , "" , "Optional parent cgroup for the container" )
2021-11-22 17:51:54 +08:00
flags . SetAnnotation ( "cgroup-parent" , annotation . ExternalURL , [ ] string { "https://docs.docker.com/engine/reference/commandline/build/#use-a-custom-parent-cgroup---cgroup-parent" } )
2021-10-29 01:34:33 +08:00
2022-01-05 14:01:08 +08:00
flags . StringArrayVar ( & options . contexts , "build-context" , [ ] string { } , "Additional build contexts (e.g., name=path)" )
2021-12-16 13:02:31 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVarP ( & options . dockerfileName , "file" , "f" , "" , ` Name of the Dockerfile (default: "PATH/Dockerfile") ` )
flags . SetAnnotation ( "file" , annotation . ExternalURL , [ ] string { "https://docs.docker.com/engine/reference/commandline/build/#specify-a-dockerfile--f" } )
2019-04-17 01:55:13 +08:00
2021-10-26 18:53:35 +08:00
flags . StringVar ( & options . imageIDFile , "iidfile" , "" , "Write the image ID to the file" )
2019-03-24 12:30:29 +08:00
flags . StringArrayVar ( & options . labels , "label" , [ ] string { } , "Set metadata for an image" )
2019-04-17 01:55:13 +08:00
2021-11-22 17:51:54 +08:00
flags . BoolVar ( & options . exportLoad , "load" , false , ` Shorthand for "--output=type=docker" ` )
2019-03-24 12:30:29 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVar ( & options . networkMode , "network" , "default" , ` Set the networking mode for the "RUN" instructions during build ` )
2019-04-17 01:55:13 +08:00
2021-11-23 12:21:35 +08:00
flags . StringArrayVar ( & options . noCacheFilter , "no-cache-filter" , [ ] string { } , "Do not cache specified stages" )
2021-11-22 17:51:54 +08:00
flags . StringArrayVarP ( & options . outputs , "output" , "o" , [ ] string { } , ` Output destination (format: "type=local,dest=path") ` )
2021-10-26 18:53:35 +08:00
flags . StringArrayVar ( & options . platforms , "platform" , platformsDefault , "Set target platform for build" )
2022-05-05 09:48:50 +08:00
if isExperimental ( ) {
2022-08-16 18:07:36 +08:00
flags . StringVar ( & options . printFunc , "print" , "" , "Print result of information request (e.g., outline, targets) [experimental]" )
2022-05-05 09:48:50 +08:00
}
2021-11-22 17:51:54 +08:00
flags . BoolVar ( & options . exportPush , "push" , false , ` Shorthand for "--output=type=registry" ` )
2019-07-09 06:58:38 +08:00
2019-04-17 01:55:13 +08:00
flags . BoolVarP ( & options . quiet , "quiet" , "q" , false , "Suppress the build output and print image ID on success" )
2021-10-26 18:53:35 +08:00
2022-02-10 22:37:06 +08:00
flags . StringArrayVar ( & options . secrets , "secret" , [ ] string { } , ` Secret to expose to the build (format: "id=mysecret[,src=/local/secret]") ` )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . Var ( & options . shmSize , "shm-size" , ` Size of "/dev/shm" ` )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringArrayVar ( & options . ssh , "ssh" , [ ] string { } , ` SSH agent socket or keys to expose to the build (format: "default|<id>[=<socket>|<key>[,<key>]]") ` )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringArrayVarP ( & options . tags , "tag" , "t" , [ ] string { } , ` Name and optionally a tag (format: "name:tag") ` )
flags . SetAnnotation ( "tag" , annotation . ExternalURL , [ ] string { "https://docs.docker.com/engine/reference/commandline/build/#tag-an-image--t" } )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVar ( & options . target , "target" , "" , "Set the target build stage to build" )
flags . SetAnnotation ( "target" , annotation . ExternalURL , [ ] string { "https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target" } )
2021-10-26 18:53:35 +08:00
2021-10-15 22:03:44 +08:00
flags . Var ( options . ulimits , "ulimit" , "Ulimit options" )
2021-08-19 11:05:15 +08:00
2022-12-08 02:44:11 +08:00
flags . StringArrayVar ( & options . attests , "attest" , [ ] string { } , ` Attestation parameters (format: "type=sbom,generator=image") ` )
flags . StringVar ( & options . sbom , "sbom" , "" , ` Shorthand for "--attest=type=sbom" ` )
flags . StringVar ( & options . provenance , "provenance" , "" , ` Shortand for "--attest=type=provenance" ` )
2022-05-05 09:48:50 +08:00
if isExperimental ( ) {
2022-08-16 18:07:36 +08:00
flags . StringVar ( & options . invoke , "invoke" , "" , "Invoke a command after the build [experimental]" )
2022-05-24 19:00:55 +08:00
}
2019-04-17 01:55:13 +08:00
// hidden flags
var ignore string
var ignoreSlice [ ] string
var ignoreBool bool
var ignoreInt int64
2021-10-26 18:53:35 +08:00
2019-04-17 01:55:13 +08:00
flags . BoolVar ( & ignoreBool , "compress" , false , "Compress the build context using gzip" )
flags . MarkHidden ( "compress" )
2021-10-26 18:53:35 +08:00
flags . StringVar ( & ignore , "isolation" , "" , "Container isolation technology" )
flags . MarkHidden ( "isolation" )
2021-10-27 03:36:37 +08:00
flags . SetAnnotation ( "isolation" , "flag-warn" , [ ] string { "isolation flag is deprecated with BuildKit." } )
2021-10-26 18:53:35 +08:00
flags . StringSliceVar ( & ignoreSlice , "security-opt" , [ ] string { } , "Security options" )
flags . MarkHidden ( "security-opt" )
2021-10-27 03:36:37 +08:00
flags . SetAnnotation ( "security-opt" , "flag-warn" , [ ] string { ` security-opt flag is deprecated. "RUN --security=insecure" should be used with BuildKit. ` } )
flags . BoolVar ( & ignoreBool , "squash" , false , "Squash newly built layers into a single new layer" )
flags . MarkHidden ( "squash" )
flags . SetAnnotation ( "squash" , "flag-warn" , [ ] string { "experimental flag squash is removed with BuildKit. You should squash inside build using a multi-stage Dockerfile for efficiency." } )
2021-10-26 18:53:35 +08:00
2019-04-17 01:55:13 +08:00
flags . StringVarP ( & ignore , "memory" , "m" , "" , "Memory limit" )
flags . MarkHidden ( "memory" )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVar ( & ignore , "memory-swap" , "" , ` Swap limit equal to memory plus swap: "-1" to enable unlimited swap ` )
2019-04-17 01:55:13 +08:00
flags . MarkHidden ( "memory-swap" )
2021-10-26 18:53:35 +08:00
2019-04-17 01:55:13 +08:00
flags . Int64VarP ( & ignoreInt , "cpu-shares" , "c" , 0 , "CPU shares (relative weight)" )
flags . MarkHidden ( "cpu-shares" )
2021-10-26 18:53:35 +08:00
2019-04-17 01:55:13 +08:00
flags . Int64Var ( & ignoreInt , "cpu-period" , 0 , "Limit the CPU CFS (Completely Fair Scheduler) period" )
flags . MarkHidden ( "cpu-period" )
2021-10-26 18:53:35 +08:00
2019-04-17 01:55:13 +08:00
flags . Int64Var ( & ignoreInt , "cpu-quota" , 0 , "Limit the CPU CFS (Completely Fair Scheduler) quota" )
flags . MarkHidden ( "cpu-quota" )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVar ( & ignore , "cpuset-cpus" , "" , ` CPUs in which to allow execution ("0-3", "0,1") ` )
2019-04-17 01:55:13 +08:00
flags . MarkHidden ( "cpuset-cpus" )
2021-10-26 18:53:35 +08:00
2021-11-22 17:51:54 +08:00
flags . StringVar ( & ignore , "cpuset-mems" , "" , ` MEMs in which to allow execution ("0-3", "0,1") ` )
2019-04-17 01:55:13 +08:00
flags . MarkHidden ( "cpuset-mems" )
2021-10-26 18:53:35 +08:00
2019-05-15 09:06:23 +08:00
flags . BoolVar ( & ignoreBool , "rm" , true , "Remove intermediate containers after a successful build" )
flags . MarkHidden ( "rm" )
2021-10-26 18:53:35 +08:00
2019-05-15 09:06:23 +08:00
flags . BoolVar ( & ignoreBool , "force-rm" , false , "Always remove intermediate containers" )
flags . MarkHidden ( "force-rm" )
2019-03-24 12:30:29 +08:00
2020-03-26 07:32:46 +08:00
commonBuildFlags ( & options . commonOptions , flags )
2019-03-24 12:30:29 +08:00
return cmd
}
2020-03-26 07:32:46 +08:00
func commonBuildFlags ( options * commonOptions , flags * pflag . FlagSet ) {
2020-05-01 03:25:30 +08:00
options . noCache = flags . Bool ( "no-cache" , false , "Do not use cache when building the image" )
2021-11-22 17:51:54 +08:00
flags . StringVar ( & options . progress , "progress" , "auto" , ` Set type of progress output ("auto", "plain", "tty"). Use plain to show container output ` )
2022-02-26 12:39:42 +08:00
options . pull = flags . Bool ( "pull" , false , "Always attempt to pull all referenced images" )
2021-06-30 13:41:21 +08:00
flags . StringVar ( & options . metadataFile , "metadata-file" , "" , "Write build result metadata to the file" )
2019-04-05 15:04:19 +08:00
}
2021-10-27 03:36:37 +08:00
func checkWarnedFlags ( f * pflag . Flag ) {
if ! f . Changed {
return
}
for t , m := range f . Annotations {
switch t {
case "flag-warn" :
logrus . Warn ( m [ 0 ] )
}
}
}
2019-07-31 07:32:36 +08:00
func listToMap ( values [ ] string , defaultEnv bool ) map [ string ] string {
2019-03-24 12:30:29 +08:00
result := make ( map [ string ] string , len ( values ) )
for _ , value := range values {
kv := strings . SplitN ( value , "=" , 2 )
if len ( kv ) == 1 {
2019-07-31 07:32:36 +08:00
if defaultEnv {
2020-01-07 04:02:06 +08:00
v , ok := os . LookupEnv ( kv [ 0 ] )
if ok {
result [ kv [ 0 ] ] = v
}
2019-07-31 07:32:36 +08:00
} else {
result [ kv [ 0 ] ] = ""
}
2019-03-24 12:30:29 +08:00
} else {
result [ kv [ 0 ] ] = kv [ 1 ]
}
}
return result
}
2021-12-16 13:02:31 +08:00
2022-02-24 15:09:46 +08:00
func parseContextNames ( values [ ] string ) ( map [ string ] build . NamedContext , error ) {
2021-12-16 13:02:31 +08:00
if len ( values ) == 0 {
return nil , nil
}
2022-02-24 15:09:46 +08:00
result := make ( map [ string ] build . NamedContext , len ( values ) )
2021-12-16 13:02:31 +08:00
for _ , value := range values {
kv := strings . SplitN ( value , "=" , 2 )
if len ( kv ) != 2 {
return nil , errors . Errorf ( "invalid context value: %s, expected key=value" , value )
}
named , err := reference . ParseNormalizedNamed ( kv [ 0 ] )
if err != nil {
return nil , errors . Wrapf ( err , "invalid context name %s" , kv [ 0 ] )
}
name := strings . TrimSuffix ( reference . FamiliarString ( named ) , ":latest" )
2022-02-24 15:09:46 +08:00
result [ name ] = build . NamedContext { Path : kv [ 1 ] }
2021-12-16 13:02:31 +08:00
}
return result , nil
}
2022-01-05 14:34:30 +08:00
2022-08-05 15:25:39 +08:00
func parsePrintFunc ( str string ) ( * build . PrintFunc , error ) {
if str == "" {
return nil , nil
}
csvReader := csv . NewReader ( strings . NewReader ( str ) )
fields , err := csvReader . Read ( )
if err != nil {
return nil , err
}
f := & build . PrintFunc { }
for _ , field := range fields {
parts := strings . SplitN ( field , "=" , 2 )
if len ( parts ) == 2 {
if parts [ 0 ] == "format" {
f . Format = parts [ 1 ]
} else {
return nil , errors . Errorf ( "invalid print field: %s" , field )
}
} else {
if f . Name != "" {
return nil , errors . Errorf ( "invalid print value: %s" , str )
}
f . Name = field
}
}
return f , nil
}
2022-02-11 15:15:47 +08:00
func writeMetadataFile ( filename string , dt interface { } ) error {
b , err := json . MarshalIndent ( dt , "" , " " )
if err != nil {
return err
}
return ioutils . AtomicWriteFile ( filename , b , 0644 )
}
func decodeExporterResponse ( exporterResponse map [ string ] string ) map [ string ] interface { } {
out := make ( map [ string ] interface { } )
for k , v := range exporterResponse {
dt , err := base64 . StdEncoding . DecodeString ( v )
if err != nil {
out [ k ] = v
continue
}
var raw map [ string ] interface { }
if err = json . Unmarshal ( dt , & raw ) ; err != nil || len ( raw ) == 0 {
out [ k ] = v
continue
}
out [ k ] = json . RawMessage ( dt )
}
return out
}
2022-02-23 13:55:44 +08:00
func wrapBuildError ( err error , bake bool ) error {
2022-01-05 14:34:30 +08:00
if err == nil {
return nil
}
st , ok := grpcerrors . AsGRPCStatus ( err )
if ok {
if st . Code ( ) == codes . Unimplemented && strings . Contains ( st . Message ( ) , "unsupported frontend capability moby.buildkit.frontend.contexts" ) {
2022-02-23 13:55:44 +08:00
msg := "current frontend does not support --build-context."
if bake {
msg = "current frontend does not support defining additional contexts for targets."
}
msg += " Named contexts are supported since Dockerfile v1.4. Use #syntax directive in Dockerfile or update to latest BuildKit."
return & wrapped { err , msg }
2022-01-05 14:34:30 +08:00
}
}
return err
}
type wrapped struct {
err error
msg string
}
func ( w * wrapped ) Error ( ) string {
return w . msg
}
func ( w * wrapped ) Unwrap ( ) error {
return w . err
}
2022-05-05 09:48:50 +08:00
func isExperimental ( ) bool {
2022-08-12 09:45:08 +08:00
if v , ok := os . LookupEnv ( "BUILDX_EXPERIMENTAL" ) ; ok {
2022-05-05 09:48:50 +08:00
vv , _ := strconv . ParseBool ( v )
return vv
}
return false
}