Skip to content

Commit 6f36539

Browse files
ndeloofglours
authored andcommitted
Fix support for port range
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 3052934 commit 6f36539

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

pkg/compose/create.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,17 @@ func (s *composeService) getCreateConfigs(ctx context.Context,
210210
if err != nil {
211211
return createConfigs{}, err
212212
}
213+
214+
exposed, err := buildContainerPorts(service)
215+
if err != nil {
216+
return createConfigs{}, err
217+
}
218+
213219
containerConfig := container.Config{
214220
Hostname: service.Hostname,
215221
Domainname: service.DomainName,
216222
User: service.User,
217-
ExposedPorts: buildContainerPorts(service),
223+
ExposedPorts: exposed,
218224
Tty: tty,
219225
OpenStdin: stdinOpen,
220226
StdinOnce: opts.AttachStdin && stdinOpen,
@@ -763,17 +769,24 @@ func setBlkio(blkio *types.BlkioConfig, resources *container.Resources) {
763769
}
764770
}
765771

766-
func buildContainerPorts(s types.ServiceConfig) nat.PortSet {
772+
func buildContainerPorts(s types.ServiceConfig) (nat.PortSet, error) {
767773
ports := nat.PortSet{}
768774
for _, s := range s.Expose {
769-
p := nat.Port(s)
770-
ports[p] = struct{}{}
775+
proto, port := nat.SplitProtoPort(s)
776+
start, end, err := nat.ParsePortRange(port)
777+
if err != nil {
778+
return nil, err
779+
}
780+
for i := start; i <= end; i++ {
781+
p := nat.Port(fmt.Sprintf("%d/%s", i, proto))
782+
ports[p] = struct{}{}
783+
}
771784
}
772785
for _, p := range s.Ports {
773786
p := nat.Port(fmt.Sprintf("%d/%s", p.Target, p.Protocol))
774787
ports[p] = struct{}{}
775788
}
776-
return ports
789+
return ports, nil
777790
}
778791

779792
func buildContainerPortBindingOptions(s types.ServiceConfig) nat.PortMap {

pkg/e2e/expose_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright 2020 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package e2e
20+
21+
import (
22+
"os"
23+
"path/filepath"
24+
"testing"
25+
26+
"gotest.tools/v3/assert"
27+
)
28+
29+
// see https://github.com/docker/compose/issues/13378
30+
func TestExposeRange(t *testing.T) {
31+
c := NewParallelCLI(t)
32+
33+
f := filepath.Join(t.TempDir(), "compose.yaml")
34+
err := os.WriteFile(f, []byte(`
35+
name: test-expose-range
36+
services:
37+
test:
38+
image: alpine
39+
expose:
40+
- "9091-9092"
41+
`), 0o644)
42+
assert.NilError(t, err)
43+
44+
t.Cleanup(func() {
45+
c.cleanupWithDown(t, "test-expose-range")
46+
})
47+
c.RunDockerComposeCmd(t, "-f", f, "up")
48+
}

0 commit comments

Comments
 (0)