code: def get_docker_network(client) -> Optional[str]:
      """Do a best-effort guess if the caller is already in a docker network
  
      Check if `hostname` exists in list of docker containers.
      If a container is found, check its network id
  
      :param client: docker client object
      :return: network id if exists
      """
      import docker
  
      if TYPE_CHECKING:  # pragma: no cover
          from docker.models.containers import Container
  
      container: "Container" = None
      try:
          hostname = socket.gethostname()
          container = client.containers.get(hostname)
      except docker.errors.NotFound:
          try:
              # https://stackoverflow.com/a/52988227/15683245
              with open("/proc/1/cpuset", encoding="utf-8") as f:
                  hostname = os.path.basename(f.read().rstrip())
              container = client.containers.get(hostname)
          except Exception:
              return None
      try:
          networks = container.attrs["NetworkSettings"]["Networks"]
          if networks:
              net_mode = list(networks.keys())[0]
              return networks[net_mode]["NetworkID"]
          else:
              return None
      except Exception:
          return None
  
imports: import os
  import socket
  from typing import TYPE_CHECKING, Optional
label_desc: Use image node_id 1 for calling a global function with example usage: get_docker_network(client) and returns: networks, None, None, None