Skip to content

Commit 63023a1

Browse files
authored
fix: Use Docker SDK to configure port binding (#526)
#### Summary While testing cloudquery/cloudquery#21179 I noticed syncing with docker plugins stopped working (even before cloudquery/cloudquery#21179). Looks like the host port is not mapped correctly (probably due to changes with recent docker engines), so the CLI can't connect to the plugin gRPC server. This PR fixes the issue by using the Docker SDK to get the port binding from a string value, ensuring the mapping is done correctly (hopefully for future versions as well) ---
1 parent 4a6c8dc commit 63023a1

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

managedplugin/plugin.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,32 @@ func (c *Client) startDockerPlugin(ctx context.Context, configPath string) error
290290
}
291291
cli.NegotiateAPIVersion(ctx)
292292
pluginArgs := c.getPluginArgs()
293+
294+
portMappings, err := nat.ParsePortSpec("7777/tcp")
295+
if err != nil {
296+
return fmt.Errorf("failed to parse port spec: %w", err)
297+
}
298+
299+
if len(portMappings) == 0 {
300+
return fmt.Errorf("failed to parse port spec: %w", errors.New("no port mappings found"))
301+
}
302+
303+
portMapping := portMappings[0]
304+
293305
config := &container.Config{
294306
ExposedPorts: nat.PortSet{
295-
"7777/tcp": struct{}{},
307+
portMapping.Port: {},
296308
},
297309
Image: configPath,
298310
Cmd: pluginArgs,
299311
Tty: true,
300312
Env: c.config.Environment,
301313
}
314+
302315
hostConfig := &container.HostConfig{
303316
ExtraHosts: c.dockerExtraHosts,
304317
PortBindings: map[nat.Port][]nat.PortBinding{
305-
"7777/tcp": {
306-
{
307-
HostIP: "localhost",
308-
HostPort: "", // let host assign a random unused port
309-
},
310-
},
318+
portMapping.Port: {portMapping.Binding},
311319
},
312320
}
313321

@@ -330,7 +338,7 @@ func (c *Client) startDockerPlugin(ctx context.Context, configPath string) error
330338

331339
var hostConnection string
332340
err = retry.Do(func() error {
333-
hostConnection, err = getHostConnection(ctx, cli, resp.ID)
341+
hostConnection, err = getHostConnection(ctx, cli, resp.ID, portMapping.Port)
334342
return err
335343
}, retry.RetryIf(func(err error) bool {
336344
return err.Error() == "failed to get port mapping for container"
@@ -362,7 +370,7 @@ func (c *Client) startDockerPlugin(ctx context.Context, configPath string) error
362370
return c.connectUsingTCP(ctx, hostConnection)
363371
}
364372

365-
func getHostConnection(ctx context.Context, cli *dockerClient.Client, containerID string) (string, error) {
373+
func getHostConnection(ctx context.Context, cli *dockerClient.Client, containerID string, port nat.Port) (string, error) {
366374
// Retrieve the dynamically assigned HOST port
367375
containerJSON, err := cli.ContainerInspect(ctx, containerID)
368376
if err != nil {
@@ -374,11 +382,11 @@ func getHostConnection(ctx context.Context, cli *dockerClient.Client, containerI
374382
return "", errors.New("container exited prematurely with error " + containerJSON.State.Error + ", exit code " + strconv.Itoa(containerJSON.State.ExitCode) + " and status " + containerJSON.State.Status)
375383
}
376384
}
377-
if len(containerJSON.NetworkSettings.Ports) == 0 || len(containerJSON.NetworkSettings.Ports["7777/tcp"]) == 0 {
385+
if len(containerJSON.NetworkSettings.Ports) == 0 || len(containerJSON.NetworkSettings.Ports[port]) == 0 {
378386
return "", errors.New("failed to get port mapping for container")
379387
}
380-
hostPort := containerJSON.NetworkSettings.Ports["7777/tcp"][0].HostPort
381-
return "localhost:" + hostPort, nil
388+
portBinding := containerJSON.NetworkSettings.Ports[port][0]
389+
return portBinding.HostIP + ":" + portBinding.HostPort, nil
382390
}
383391

384392
func waitForContainerRunning(ctx context.Context, cli *dockerClient.Client, containerID string) error {

0 commit comments

Comments
 (0)