Skip to content
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

Feature: Proper type hinting for UDP ports bindings #674

Open
champialex opened this issue Aug 12, 2024 · 3 comments · May be fixed by #690
Open

Feature: Proper type hinting for UDP ports bindings #674

champialex opened this issue Aug 12, 2024 · 3 comments · May be fixed by #690

Comments

@champialex
Copy link

I'm trying to bind udp ports:

def telegraf(network: Network):
    ctr = DockerContainer(image="telegraf:1.31.1")
    ctr.with_volume_mapping(f"{CURRENT_DIR}/telegraf_test.conf", "/etc/telegraf/telegraf.d/telegraf_test.conf")
    ctr.with_exposed_ports("8094/tcp")  # Telegraf ports
    ctr.with_bind_ports("8125/udp", "8125/udp")
    ctr.with_bind_ports("8092/udp", "8092/udp")
    _connect_to_network(ctr, network, "telegraf")
    return ctr

It works fine, except that ports are type hinted as int, leading to sad squiggly lines in IDEs.

Could we change type hints of port APIs to allow strings, to make it nicer to define more fine grain port specs?

    def with_bind_ports(self, container: str, host: Optional[str] = None): ...

    def with_exposed_ports(self, *ports: str): ...
  
  def get_exposed_port(self, port: str): ...
@alexanderankin
Copy link
Collaborator

open to a pr for this!

@Tranquility2
Copy link
Contributor

Tranquility2 commented Aug 21, 2024

please note that def get_exposed_port(self, port: str): ... is probably wrong as it uses

mapped_port = self.get_docker_client().port(self._container.id, port)

->

port_mappings = self.client.api.port(container_id, port)

->

    def port(self, container, private_port): 
        """
        Lookup the public-facing port that is NAT-ed to ``private_port``.
        Identical to the ``docker port`` command.

        Args:
            container (str): The container to look up
            private_port (int): The private port to inspect

So the docker API expect it to be an int

with_bind_ports & with_exposed_ports seems safe

@Tranquility2
Copy link
Contributor

Opened #690

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants