From 1567116c2545937cdde59bfc9a2e05347d3f0c58 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Wed, 11 Dec 2024 09:55:57 -0600 Subject: [PATCH 1/2] improve docker port configurability --- pkg/localnet/helpers.go | 4 ++-- pkg/localnet/infra/docker.go | 4 +--- pkg/localnet/infra/types.go | 7 ++++++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/localnet/helpers.go b/pkg/localnet/helpers.go index b48c1e9..b108f0c 100644 --- a/pkg/localnet/helpers.go +++ b/pkg/localnet/helpers.go @@ -50,12 +50,12 @@ func PlatformDirMount(ctx context.Context, platform tools.Platform) (volume infr // FromHostAddress returns address of the app service seen from the host. func FromHostAddress(proto string, app *infra.App, port infra.PortName) string { - return infra.JoinNetAddr(proto, app.Info().HostFromHost, app.Ports[port]) + return infra.JoinNetAddr(proto, app.Info().HostFromHost, app.Ports[port].HostPort) } // FromContainerAddress returns address of the app service seen from the container. func FromContainerAddress(proto string, app *infra.App, port infra.PortName) string { - return infra.JoinNetAddr(proto, app.Info().HostFromContainer, app.Ports[port]) + return infra.JoinNetAddr(proto, app.Info().HostFromContainer, app.Ports[port].ContainerPort) } func appDir(ctx context.Context, appName string) string { diff --git a/pkg/localnet/infra/docker.go b/pkg/localnet/infra/docker.go index 0f1965e..1c0dd38 100644 --- a/pkg/localnet/infra/docker.go +++ b/pkg/localnet/infra/docker.go @@ -9,7 +9,6 @@ import ( "os" osexec "os/exec" "path/filepath" - "strconv" "strings" "sync" "time" @@ -179,8 +178,7 @@ func (d *Docker) prepareRunArgs(app *App) []string { runArgs = append(runArgs, "--user", fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid())) } for _, port := range app.Ports { - portStr := strconv.Itoa(port) - runArgs = append(runArgs, "-p", "127.0.0.1:"+portStr+":"+portStr+"/tcp") + runArgs = append(runArgs, "-p", fmt.Sprintf("127.0.0.1:%d:%d/tcp", port.HostPort, port.ContainerPort)) } for _, v := range app.Volumes { runArgs = append(runArgs, "-v", lo.Must(filepath.EvalSymlinks(lo.Must(filepath.Abs(v.Source))))+":"+ diff --git a/pkg/localnet/infra/types.go b/pkg/localnet/infra/types.go index b2a4afd..236c7d0 100644 --- a/pkg/localnet/infra/types.go +++ b/pkg/localnet/infra/types.go @@ -192,8 +192,13 @@ type Volume struct { // PortName represents the name of the port. type PortName string +type DockerPort struct { + HostPort int + ContainerPort int +} + // Ports is the map of exposed ports. -type Ports map[PortName]int +type Ports map[PortName]DockerPort // App represents application to be deployed. type App struct { From 5617fe628c78b769c646cbd44dc7029ecab91690 Mon Sep 17 00:00:00 2001 From: Uday Patil Date: Wed, 11 Dec 2024 11:08:06 -0600 Subject: [PATCH 2/2] fix lint --- pkg/localnet/infra/types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/localnet/infra/types.go b/pkg/localnet/infra/types.go index 236c7d0..b529be5 100644 --- a/pkg/localnet/infra/types.go +++ b/pkg/localnet/infra/types.go @@ -192,6 +192,7 @@ type Volume struct { // PortName represents the name of the port. type PortName string +// DockerPort is the port mapping from host to container. type DockerPort struct { HostPort int ContainerPort int