From 52af5b499dd84a446d74eb81eba85d3392441550 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Tue, 22 Apr 2025 09:27:35 +0100 Subject: [PATCH] Docker-compose host port on localhost --- internal/local_runner.go | 36 +++++++++++++++++++++++++----------- main.go | 4 +++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/internal/local_runner.go b/internal/local_runner.go index bb4debf..7098c5b 100644 --- a/internal/local_runner.go +++ b/internal/local_runner.go @@ -59,6 +59,9 @@ type LocalRunner struct { tasksMtx sync.Mutex tasks map[string]*task taskUpdateCh chan struct{} + + // wether to bind the ports to the local interface + bindHostPortsLocally bool } type task struct { @@ -88,12 +91,14 @@ func newDockerClient() (*client.Client, error) { return client, nil } -func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool) (*LocalRunner, error) { +func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string, interactive bool, bindHostPortsLocally bool) (*LocalRunner, error) { client, err := newDockerClient() if err != nil { return nil, fmt.Errorf("failed to create docker client: %w", err) } + fmt.Println(bindHostPortsLocally) + // merge the overrides with the manifest overrides if overrides == nil { overrides = make(map[string]string) @@ -134,15 +139,16 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string } d := &LocalRunner{ - out: out, - manifest: manifest, - client: client, - reservedPorts: map[int]bool{}, - overrides: overrides, - handles: []*exec.Cmd{}, - tasks: tasks, - taskUpdateCh: make(chan struct{}), - exitErr: make(chan error, 2), + out: out, + manifest: manifest, + client: client, + reservedPorts: map[int]bool{}, + overrides: overrides, + handles: []*exec.Cmd{}, + tasks: tasks, + taskUpdateCh: make(chan struct{}), + exitErr: make(chan error, 2), + bindHostPortsLocally: bindHostPortsLocally, } if interactive { @@ -580,10 +586,18 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{} service["entrypoint"] = s.entrypoint } + fmt.Println("XXX") + if len(s.ports) > 0 { ports := []string{} + fmt.Println("x", d.bindHostPortsLocally) + for _, p := range s.ports { - ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port)) + if d.bindHostPortsLocally { + ports = append(ports, fmt.Sprintf("127.0.0.1:%d:%d", p.HostPort, p.Port)) + } else { + ports = append(ports, fmt.Sprintf("%d:%d", p.HostPort, p.Port)) + } } service["ports"] = ports } diff --git a/main.go b/main.go index 7c3f31c..b64deae 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ var dryRun bool var interactive bool var timeout time.Duration var logLevelFlag string +var bindExternal bool var rootCmd = &cobra.Command{ Use: "playground", @@ -167,6 +168,7 @@ func main() { recipeCmd.Flags().BoolVar(&interactive, "interactive", false, "interactive mode") recipeCmd.Flags().DurationVar(&timeout, "timeout", 0, "") // Used for CI recipeCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "log level") + recipeCmd.Flags().BoolVar(&bindExternal, "bind-external", false, "bind host ports to external interface") cookCmd.AddCommand(recipeCmd) } @@ -234,7 +236,7 @@ func runIt(recipe internal.Recipe) error { } } - dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive) + dockerRunner, err := internal.NewLocalRunner(artifacts.Out, svcManager, overrides, interactive, !bindExternal) if err != nil { return fmt.Errorf("failed to create docker runner: %w", err) }