Skip to content

Commit e4d5895

Browse files
authored
Merge pull request #385 from ndeloof/additional_context_path
2 parents 5390f0a + 1592cda commit e4d5895

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

loader/full-struct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
5757
Target: "foo",
5858
Network: "foo",
5959
CacheFrom: []string{"foo", "bar"},
60-
AdditionalContexts: map[string]*string{"foo": strPtr("/bar")},
60+
AdditionalContexts: types.Mapping{"foo": "/bar"},
6161
Labels: map[string]string{"FOO": "BAR"},
6262
Secrets: []types.ServiceSecretConfig{
6363
{

loader/normalize.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,23 @@ func Normalize(project *types.Project, resolvePaths bool) error {
7575
if s.Build.Dockerfile == "" && s.Build.DockerfileInline == "" {
7676
s.Build.Dockerfile = "Dockerfile"
7777
}
78-
localContext := absPath(project.WorkingDir, s.Build.Context)
79-
if _, err := os.Stat(localContext); err == nil {
80-
if resolvePaths {
78+
if resolvePaths {
79+
// Build context might be a remote http/git context. Unfortunately supported "remote"
80+
// syntax is highly ambiguous in moby/moby and not defined by compose-spec,
81+
// so let's assume runtime will check
82+
localContext := absPath(project.WorkingDir, s.Build.Context)
83+
if _, err := os.Stat(localContext); err == nil {
8184
s.Build.Context = localContext
8285
}
83-
// } else {
84-
// might be a remote http/git context. Unfortunately supported "remote" syntax is highly ambiguous
85-
// in moby/moby and not defined by compose-spec, so let's assume runtime will check
86+
for name, path := range s.Build.AdditionalContexts {
87+
if strings.Contains(path, "://") { // `docker-image://` or any builder specific context type
88+
continue
89+
}
90+
path = absPath(project.WorkingDir, path)
91+
if _, err := os.Stat(path); err == nil {
92+
s.Build.AdditionalContexts[name] = path
93+
}
94+
}
8695
}
8796
s.Build.Args = s.Build.Args.Resolve(fn)
8897
}

loader/normalize_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,55 @@ networks:
245245
assert.NilError(t, err)
246246
assert.Equal(t, expected, string(marshal))
247247
}
248+
249+
func TestNormalizeAdditionalContexts(t *testing.T) {
250+
project := types.Project{
251+
Name: "myProject",
252+
Services: types.Services{
253+
types.ServiceConfig{
254+
Name: "test",
255+
Build: &types.BuildConfig{
256+
Context: ".",
257+
Dockerfile: "Dockerfile",
258+
AdditionalContexts: map[string]string{
259+
"image": "docker-image://foo",
260+
"oci": "oci-layout://foo",
261+
"abs_path": "/tmp",
262+
"rel_path": "./testdata",
263+
},
264+
},
265+
},
266+
},
267+
}
268+
269+
absCwd, _ := filepath.Abs(".")
270+
expected := types.Project{
271+
Name: "myProject",
272+
Services: types.Services{
273+
types.ServiceConfig{
274+
Name: "test",
275+
Build: &types.BuildConfig{
276+
Context: absCwd,
277+
Dockerfile: "Dockerfile",
278+
AdditionalContexts: map[string]string{
279+
"image": "docker-image://foo",
280+
"oci": "oci-layout://foo",
281+
"abs_path": "/tmp",
282+
"rel_path": filepath.Join(absCwd, "testdata"),
283+
},
284+
},
285+
Networks: map[string]*types.ServiceNetworkConfig{
286+
"default": nil,
287+
},
288+
},
289+
},
290+
Networks: types.Networks{"default": types.NetworkConfig{
291+
Name: "myProject_default",
292+
}},
293+
WorkingDir: absCwd,
294+
ComposeFiles: []string{},
295+
}
296+
err := Normalize(&project, true)
297+
assert.NilError(t, err)
298+
assert.DeepEqual(t, expected, project)
299+
}

types/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ type BuildConfig struct {
303303
CacheFrom StringList `mapstructure:"cache_from" yaml:"cache_from,omitempty" json:"cache_from,omitempty"`
304304
CacheTo StringList `mapstructure:"cache_to" yaml:"cache_to,omitempty" json:"cache_to,omitempty"`
305305
NoCache bool `mapstructure:"no_cache" yaml:"no_cache,omitempty" json:"no_cache,omitempty"`
306-
AdditionalContexts MappingWithEquals `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
306+
AdditionalContexts Mapping `mapstructure:"additional_contexts" yaml:"additional_contexts,omitempty" json:"additional_contexts,omitempty"`
307307
Pull bool `mapstructure:"pull" yaml:"pull,omitempty" json:"pull,omitempty"`
308308
ExtraHosts HostsList `mapstructure:"extra_hosts" yaml:"extra_hosts,omitempty" json:"extra_hosts,omitempty"`
309309
Isolation string `yaml:",omitempty" json:"isolation,omitempty"`

0 commit comments

Comments
 (0)