Skip to content

Docker-compose host port on localhost #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions internal/local_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down