@@ -5,10 +5,13 @@ import (
55 "errors"
66 "fmt"
77 "io"
8+ "os"
89 "path/filepath"
910 "time"
1011
1112 "github.com/cenkalti/backoff/v4"
13+ "github.com/docker/docker/builder/remotecontext/git"
14+ "github.com/docker/docker/builder/remotecontext/urlutil"
1215 "github.com/moby/go-archive"
1316 "github.com/moby/go-archive/compression"
1417 "github.com/moby/term"
@@ -23,13 +26,25 @@ import (
2326// This function is useful for creating a build context to build an image.
2427// The dockerfile path needs to be relative to the build context.
2528func ArchiveBuildContext (dir string , dockerfile string ) (r io.ReadCloser , err error ) {
26- // always pass context as absolute path
27- abs , err := filepath .Abs (dir )
28- if err != nil {
29- return nil , fmt .Errorf ("absolute path: %w" , err )
29+ switch {
30+ case isLocalDir (dir ):
31+ // always pass context as absolute path
32+ dir , err = filepath .Abs (dir )
33+ if err != nil {
34+ return nil , fmt .Errorf ("absolute path: %w" , err )
35+ }
36+ case urlutil .IsGitURL (dir ):
37+ clone , err := git .Clone (dir )
38+ if err != nil {
39+ return nil , fmt .Errorf ("unable to clone git build context: %w" , err )
40+ }
41+ dir = clone
42+ // TODO case urlutil.IsURL(dir):
43+ default :
44+ return nil , fmt .Errorf ("unable to prepare context: path %q not found" , dir )
3045 }
3146
32- dockerIgnoreExists , excluded , err := ParseDockerIgnore (abs )
47+ dockerIgnoreExists , excluded , err := ParseDockerIgnore (dir )
3348 if err != nil {
3449 return nil , fmt .Errorf ("parse docker ignore: %w" , err )
3550 }
@@ -41,7 +56,7 @@ func ArchiveBuildContext(dir string, dockerfile string) (r io.ReadCloser, err er
4156 }
4257
4358 buildContext , err := archive .TarWithOptions (
44- abs ,
59+ dir ,
4560 & archive.TarOptions {
4661 ExcludePatterns : excluded ,
4762 IncludeFiles : includes ,
@@ -55,6 +70,11 @@ func ArchiveBuildContext(dir string, dockerfile string) (r io.ReadCloser, err er
5570 return buildContext , nil
5671}
5772
73+ func isLocalDir (c string ) bool {
74+ _ , err := os .Stat (c )
75+ return err == nil
76+ }
77+
5878// ImageBuildClient is a client that can build images.
5979type ImageBuildClient interface {
6080 ImageClient
0 commit comments