@@ -28,7 +28,6 @@ import (
28
28
buildtypes "github.com/docker/docker/api/types/build"
29
29
"github.com/docker/docker/api/types/container"
30
30
registrytypes "github.com/docker/docker/api/types/registry"
31
- "github.com/docker/docker/builder/remotecontext/urlutil"
32
31
"github.com/docker/docker/pkg/progress"
33
32
"github.com/docker/docker/pkg/streamformatter"
34
33
"github.com/moby/go-archive"
@@ -76,12 +75,6 @@ func (o buildOptions) dockerfileFromStdin() bool {
76
75
return o .dockerfileName == "-"
77
76
}
78
77
79
- // contextFromStdin returns true when the user specified that the build context
80
- // should be read from stdin
81
- func (o buildOptions ) contextFromStdin () bool {
82
- return o .context == "-"
83
- }
84
-
85
78
func newBuildOptions () buildOptions {
86
79
ulimits := make (map [string ]* container.Ulimit )
87
80
return buildOptions {
@@ -189,21 +182,24 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
189
182
buildCtx io.ReadCloser
190
183
dockerfileCtx io.ReadCloser
191
184
contextDir string
192
- tempDir string
193
185
relDockerfile string
194
186
progBuff io.Writer
195
187
buildBuff io.Writer
196
188
remote string
197
189
)
198
190
191
+ contextType , err := build .DetectContextType (options .context )
192
+ if err != nil {
193
+ return err
194
+ }
195
+
199
196
if options .dockerfileFromStdin () {
200
- if options . contextFromStdin () {
197
+ if contextType == build . ContextTypeStdin {
201
198
return errors .New ("invalid argument: can't use stdin for both build context and dockerfile" )
202
199
}
203
200
dockerfileCtx = dockerCli .In ()
204
201
}
205
202
206
- specifiedContext := options .context
207
203
progBuff = dockerCli .Out ()
208
204
buildBuff = dockerCli .Out ()
209
205
if options .quiet {
@@ -217,38 +213,43 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
217
213
}
218
214
}
219
215
220
- switch {
221
- case options . contextFromStdin () :
216
+ switch contextType {
217
+ case build . ContextTypeStdin :
222
218
// buildCtx is tar archive. if stdin was dockerfile then it is wrapped
223
219
buildCtx , relDockerfile , err = build .GetContextFromReader (dockerCli .In (), options .dockerfileName )
224
- case isLocalDir (specifiedContext ):
225
- contextDir , relDockerfile , err = build .GetContextFromLocalDir (specifiedContext , options .dockerfileName )
226
- if err == nil && strings .HasPrefix (relDockerfile , ".." + string (filepath .Separator )) {
220
+ if err != nil {
221
+ return fmt .Errorf ("unable to prepare context from STDIN: %w" , err )
222
+ }
223
+ case build .ContextTypeLocal :
224
+ contextDir , relDockerfile , err = build .GetContextFromLocalDir (options .context , options .dockerfileName )
225
+ if err != nil {
226
+ return errors .Errorf ("unable to prepare context: %s" , err )
227
+ }
228
+ if strings .HasPrefix (relDockerfile , ".." + string (filepath .Separator )) {
227
229
// Dockerfile is outside of build-context; read the Dockerfile and pass it as dockerfileCtx
228
230
dockerfileCtx , err = os .Open (options .dockerfileName )
229
231
if err != nil {
230
232
return errors .Errorf ("unable to open Dockerfile: %v" , err )
231
233
}
232
234
defer dockerfileCtx .Close ()
233
235
}
234
- case urlutil .IsGitURL (specifiedContext ):
235
- tempDir , relDockerfile , err = build .GetContextFromGitURL (specifiedContext , options .dockerfileName )
236
- case urlutil .IsURL (specifiedContext ):
237
- buildCtx , relDockerfile , err = build .GetContextFromURL (progBuff , specifiedContext , options .dockerfileName )
238
- default :
239
- return errors .Errorf ("unable to prepare context: path %q not found" , specifiedContext )
240
- }
241
-
242
- if err != nil {
243
- if options .quiet && urlutil .IsURL (specifiedContext ) {
244
- _ , _ = fmt .Fprintln (dockerCli .Err (), progBuff )
236
+ case build .ContextTypeGit :
237
+ var tempDir string
238
+ tempDir , relDockerfile , err = build .GetContextFromGitURL (options .context , options .dockerfileName )
239
+ if err != nil {
240
+ return errors .Errorf ("unable to prepare context: %s" , err )
245
241
}
246
- return errors .Errorf ("unable to prepare context: %s" , err )
247
- }
248
-
249
- if tempDir != "" {
250
- defer os .RemoveAll (tempDir )
242
+ defer func () {
243
+ _ = os .RemoveAll (tempDir )
244
+ }()
251
245
contextDir = tempDir
246
+ case build .ContextTypeRemote :
247
+ buildCtx , relDockerfile , err = build .GetContextFromURL (progBuff , options .context , options .dockerfileName )
248
+ if err != nil && options .quiet {
249
+ _ , _ = fmt .Fprintln (dockerCli .Err (), progBuff )
250
+ }
251
+ default :
252
+ return errors .Errorf ("unable to prepare context: path %q not found" , options .context )
252
253
}
253
254
254
255
// read from a directory into tar archive
@@ -415,11 +416,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
415
416
return nil
416
417
}
417
418
418
- func isLocalDir (c string ) bool {
419
- _ , err := os .Stat (c )
420
- return err == nil
421
- }
422
-
423
419
type translatorFunc func (context.Context , reference.NamedTagged ) (reference.Canonical , error )
424
420
425
421
// validateTag checks if the given image name can be resolved.
0 commit comments