Skip to content

Commit 0b28efe

Browse files
authored
Docker-compose host port on localhost (#106)
1 parent 0b1dc97 commit 0b28efe

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

internal/local_runner.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ type LocalRunner struct {
5959
tasksMtx sync.Mutex
6060
tasks map[string]*task
6161
taskUpdateCh chan struct{}
62+
63+
// wether to bind the ports to the local interface
64+
bindHostPortsLocally bool
6265
}
6366

6467
type task struct {
@@ -88,12 +91,14 @@ func newDockerClient() (*client.Client, error) {
8891
return client, nil
8992
}
9093

91-
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool) (*LocalRunner, error) {
94+
func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool) (*LocalRunner, error) {
9295
client, err := newDockerClient()
9396
if err != nil {
9497
return nil, fmt.Errorf("failed to create docker client: %w", err)
9598
}
9699

100+
fmt.Println(bindHostPortsLocally)
101+
97102
// merge the overrides with the manifest overrides
98103
if overrides == nil {
99104
overrides = make(map[string]string)
@@ -134,15 +139,16 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
134139
}
135140

136141
d := &LocalRunner{
137-
out: out,
138-
manifest: manifest,
139-
client: client,
140-
reservedPorts: map[int]bool{},
141-
overrides: overrides,
142-
handles: []*exec.Cmd{},
143-
tasks: tasks,
144-
taskUpdateCh: make(chan struct{}),
145-
exitErr: make(chan error, 2),
142+
out: out,
143+
manifest: manifest,
144+
client: client,
145+
reservedPorts: map[int]bool{},
146+
overrides: overrides,
147+
handles: []*exec.Cmd{},
148+
tasks: tasks,
149+
taskUpdateCh: make(chan struct{}),
150+
exitErr: make(chan error, 2),
151+
bindHostPortsLocally: bindHostPortsLocally,
146152
}
147153

148154
if interactive {
@@ -580,10 +586,18 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
580586
service["entrypoint"] = s.entrypoint
581587
}
582588

589+
fmt.Println("XXX")
590+
583591
if len(s.ports) > 0 {
584592
ports := []string{}
593+
fmt.Println("x", d.bindHostPortsLocally)
594+
585595
for _, p := range s.ports {
586-
ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port))
596+
if d.bindHostPortsLocally {
597+
ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d", p.HostPort, p.Port))
598+
} else {
599+
ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port))
600+
}
587601
}
588602
service["ports"] = ports
589603
}

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var dryRun bool
2424
var interactive bool
2525
var timeout time.Duration
2626
var logLevelFlag string
27+
var bindExternal bool
2728

2829
var rootCmd = &cobra.Command{
2930
Use: "playground",
@@ -167,6 +168,7 @@ func main() {
167168
recipeCmd.Flags().BoolVar(&interactive, "interactive", false, "interactive mode")
168169
recipeCmd.Flags().DurationVar(&timeout, "timeout", 0, "") // Used for CI
169170
recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level")
171+
recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface")
170172

171173
cookCmd.AddCommand(recipeCmd)
172174
}
@@ -234,7 +236,7 @@ func runIt(recipe internal.Recipe) error {
234236
}
235237
}
236238

237-
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive)
239+
dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal)
238240
if err != nil {
239241
return fmt.Errorf("failed to create docker runner: %w", err)
240242
}

0 commit comments

Comments
 (0)