Repository: PrefectHQ/prefect

Issue:
Unique hostname label is `docker-desktop` for Docker "host" networking type
## Current behavior
The Local Agent (and Flow registration) typically generate a unique string (based on a call to `socket.gethostname()`) that gets used as a label to avoid collisions when multiple people are running Local Agents within an organization. If the Local Agent or Flow registration is running in a Docker container with the networking type set to "host" this unique string ends up as `"docker-desktop"`, negating the benefit of using this as a label that is unique to this particular Local Agent.

## Proposed behavior
If the hostname is detected as `"docker-desktop"`, generate a string of random characters (or some other approach). When docker is run without "host" networking type, docker automatically generates a random hostname. Mimicking the default docker behavior might be a good example to follow. Such a change would avoid engineers being confused when their Flow runs get picked up by someone else's local agent.

## Example
![image](https://user-images.githubusercontent.com/2823453/82497158-cd2c0f00-9abb-11ea-9168-2c1bdf88f897.png)




Discussion:
Ah great catch @joeschmid! In the meantime I would say add an extra label to your engineer-specific flows and agents to avoid this behavior. 🙂 

The downfall in generating a random string when detecting `docker-desktop` is we would need to use the _same_ random string for both the flow and the agent. I wonder if there is some other identifiable information we can pull out of the environment when that hostname is detected
@joshmeek That makes perfect sense on the requirement for the two labels needing to match. Maybe IP would is a decent alternative? (I'm sure there could be some `192.168.1.101` type duplicates, but at least far less likely.)

For our workaround, we already have engineer-specific config to have personal projects in Prefect Cloud so we ended up doing `prefect agent start local --label $PREFECT_PROJECT_NAME` in our docker-compose.yaml (Where PREFECT_PROJECT_NAME=Joe, etc.)

Resolving patch:
diff --git a/src/prefect/agent/local/agent.py b/src/prefect/agent/local/agent.py
--- a/src/prefect/agent/local/agent.py
+++ b/src/prefect/agent/local/agent.py
@@ -74,6 +74,11 @@ def __init__(
             no_cloud_logs=no_cloud_logs,
         )
         hostname = socket.gethostname()
+
+        # Resolve common Docker hostname by using IP
+        if hostname == "docker-desktop":
+            hostname = socket.gethostbyname(hostname)
+
         if hostname_label and (hostname not in self.labels):
             assert isinstance(self.labels, list)
             self.labels.append(hostname)


Test patch:
diff --git a/tests/agent/test_local_agent.py b/tests/agent/test_local_agent.py
--- a/tests/agent/test_local_agent.py
+++ b/tests/agent/test_local_agent.py
@@ -73,6 +73,14 @@ def test_local_agent_config_options_hostname(runner_token):
         }
 
 
+def test_local_agent_uses_ip_if_dockerdesktop_hostname(monkeypatch, runner_token):
+    monkeypatch.setattr("socket.gethostname", MagicMock(return_value="docker-desktop"))
+    monkeypatch.setattr("socket.gethostbyname", MagicMock(return_value="IP"))
+
+    agent = LocalAgent()
+    assert "IP" in agent.labels
+
+
 def test_populate_env_vars_uses_user_provided_env_vars(runner_token):
     with set_temporary_config(
         {