Repository: PrefectHQ/prefect

Issue:
Add optional pull to local agent
If the image is present locally we don't want to attempt a pull from a registry


Discussion:

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
@@ -1,6 +1,6 @@
 import docker
 
-from prefect import config
+from prefect import config, context
 from prefect.agent import Agent
 from prefect.environments.storage import Docker
 from prefect.serialization.storage import StorageSchema
@@ -15,9 +15,11 @@ class LocalAgent(Agent):
         - base_url (str, optional): URL for a Docker daemon server. Defaults to
             `unix:///var/run/docker.sock` however other hosts such as
             `tcp://0.0.0.0:2375` can be provided
+        - no_pull (bool, optional): Flag on whether or not to pull flow images.
+            Defaults to `False` if not provided here or in context.
     """
 
-    def __init__(self, base_url: str = None) -> None:
+    def __init__(self, base_url: str = None, no_pull: bool = None) -> None:
         super().__init__()
 
         base_url = base_url or "unix://var/run/docker.sock"
@@ -32,6 +34,10 @@ def __init__(self, base_url: str = None) -> None:
             )
             raise exc
 
+        self.no_pull = no_pull or context.get("no_pull")
+        if self.no_pull is None:
+            self.no_pull = False
+
     def deploy_flows(self, flow_runs: list) -> None:
         """
         Deploy flow runs on your local machine as Docker containers
@@ -50,8 +56,8 @@ def deploy_flows(self, flow_runs: list) -> None:
 
             env_vars = self.populate_env_vars(flow_run=flow_run)
 
-            # Pull image if it doesn't exist locally
-            self.docker_client.pull(storage.name)
+            if not self.no_pull:
+                self.docker_client.pull(storage.name)
 
             # Create a container
             container = self.docker_client.create_container(
diff --git a/src/prefect/cli/agent.py b/src/prefect/cli/agent.py
--- a/src/prefect/cli/agent.py
+++ b/src/prefect/cli/agent.py
@@ -1,6 +1,6 @@
 import click
 
-from prefect import config
+from prefect import config, context
 from prefect.utilities.configuration import set_temporary_config
 from prefect.utilities.serialization import from_qualified_name
 
@@ -39,7 +39,8 @@ def agent():
 @click.option(
     "--token", "-t", required=False, help="A Prefect Cloud API token.", hidden=True
 )
-def start(name, token):
+@click.option("--no-pull", is_flag=True, help="Pull images flag.", hidden=True)
+def start(name, token, no_pull):
     """
     Start an agent.
 
@@ -51,6 +52,8 @@ def start(name, token):
     \b
     Options:
         --token, -t     TEXT    A Prefect Cloud api token
+        --no-pull               Pull images for a LocalAgent
+                                Defaults to pulling if not provided
     """
     with set_temporary_config(
         {"cloud.agent.auth_token": token or config.cloud.agent.auth_token}
@@ -61,4 +64,5 @@ def start(name, token):
             click.secho("{} is not a valid agent".format(name), fg="red")
             return
 
-        from_qualified_name(retrieved_agent)().start()
+        with context(no_pull=no_pull):
+            from_qualified_name(retrieved_agent)().start()


Test patch: