Skip to content

fix: PortCommand parser doesn't handle single-port query format #231

@joshrotenberg

Description

@joshrotenberg

Description

When querying a specific port with PortCommand::new(container).port(6379).run(), Docker returns a different output format than when listing all ports, and the parser doesn't handle it.

Expected vs Actual Output

Listing all ports (docker port <container>):

6379/tcp -> 0.0.0.0:40998
6379/tcp -> [::]:40998

Querying specific port (docker port <container> 6379):

0.0.0.0:40998
[::]:40998

Current Behavior

The parser in parse_port_mappings() expects the format <port>/<proto> -> <host>:<port> but when querying a specific port, Docker omits the container port prefix, returning just <host>:<port>.

This causes host_port() in ContainerGuard to fail with:

InvalidConfig("No host port mapping found for container port 6379")

Proposed Fix

Update parse_port_mappings() in src/command/port.rs to handle both formats:

  1. Full format: 80/tcp -> 0.0.0.0:8080
  2. Simple format: 0.0.0.0:8080 (when specific port was queried)

For the simple format, the container port can be inferred from the port field in the PortCommand struct.

Affected Code

  • src/command/port.rs: parse_port_mappings() function
  • src/testing.rs: host_port() method on ContainerGuard

Discovered While

Adding the examples/testing_utilities.rs example for PR #230.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions