Skip to content

Commit fb19695

Browse files
authored
fix(daemon): copy ExposedPorts from source config in computeImageConfig (#2356)
* daemon: ExposedPorts always dropped from config due to ranging over the wrong map in computeImageConfig
1 parent 0c9d911 commit fb19695

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎pkg/v1/daemon/image.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (i *image) computeImageConfig(config *specs.DockerOCIImageConfig) v1.Config
369369

370370
if len(config.ExposedPorts) > 0 {
371371
c.ExposedPorts = map[string]struct{}{}
372-
for port := range c.ExposedPorts {
372+
for port := range config.ExposedPorts {
373373
c.ExposedPorts[port] = struct{}{}
374374
}
375375
}

‎pkg/v1/daemon/image_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
api "github.com/moby/moby/api/types/image"
2828
"github.com/moby/moby/api/types/storage"
2929
"github.com/moby/moby/client"
30+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3031

32+
"github.com/google/go-cmp/cmp"
3133
"github.com/google/go-containerregistry/pkg/name"
3234
"github.com/google/go-containerregistry/pkg/v1/compare"
3335
"github.com/google/go-containerregistry/pkg/v1/tarball"
@@ -289,6 +291,45 @@ func TestImageFileBuffered(t *testing.T) {
289291
})
290292
}
291293

294+
func TestImageConfigExposedPorts(t *testing.T) {
295+
want := map[string]struct{}{
296+
"80/tcp": {},
297+
"443/tcp": {},
298+
"53/udp": {},
299+
}
300+
301+
resp := inspectResp
302+
resp.Config = &specs.DockerOCIImageConfig{
303+
ImageConfig: ocispec.ImageConfig{
304+
ExposedPorts: want,
305+
},
306+
}
307+
308+
mc := &MockClient{
309+
path: imagePath,
310+
inspectResp: resp,
311+
}
312+
313+
tag, err := name.NewTag("unused", name.WeakValidation)
314+
if err != nil {
315+
t.Fatalf("NewTag: %v", err)
316+
}
317+
318+
dmn, err := Image(tag, WithClient(mc), WithUnbufferedOpener())
319+
if err != nil {
320+
t.Fatalf("Image(): %v", err)
321+
}
322+
323+
cf, err := dmn.ConfigFile()
324+
if err != nil {
325+
t.Fatalf("ConfigFile(): %v", err)
326+
}
327+
328+
if diff := cmp.Diff(want, cf.Config.ExposedPorts); diff != "" {
329+
t.Errorf("ExposedPorts mismatch (-want +got):\n%s", diff)
330+
}
331+
}
332+
292333
func TestImageDefaultClient(t *testing.T) {
293334
wantErr := fmt.Errorf("bad client")
294335
defaultClient = func() (Client, error) {

0 commit comments

Comments
 (0)