:wave: Hello, team! How do Ii properly configure p...
# ask-community
b
👋 Hello, team! How do Ii properly configure prefect==2.20.4 for use in Docker? I built a docker image using python base image and set the environment variables below. Then I spun up a container with the following command. I can reach http://localhost:4200/api/hello just fine, but I get an error when trying to run my python file because it can not reach /api. ============================================================================================ Docker Env Variables: ENV PREFECT_SERVER_ANALYTICS_ENABLED=False \ PREFECT_API_URL=http://host.docker.internal:4200/api \ PREFECT_SERVER_API_HOST=http://host.docker.internal:4200/api CMD ["prefect", "server", "start", "--host", "0.0.0.0", "--port", "4200"] ============================================================================================ docker run -it --gpus all -d --name parse-container --network="host" -v %cd%:/app py-dev-gpu:2024SEP ============================================================================================ http://localhost:4200/api/ { "detail": "Not Found" } ============================================================================================ File "/usr/local/lib/python3.11/site-packages/prefect/engine.py", line 364, in create_then_begin_flow_run await check_api_reachable(client, "Cannot create flow run") File "/usr/local/lib/python3.11/site-packages/prefect/utilities/engine.py", line 680, in check_api_reachable raise RuntimeError( RuntimeError: Cannot create flow run. Failed to reach API at http://host.docker.internal:4200/api/.
1
c
are you running your flow from within the same docker container? or from a process outside of the container?
it looks like Marvin is helping you in another thread - either way, the
host.docker.internal
values don't belong there; if you're running from within the container, the API_URL should be
<http://localhost:4200/api>
or
<http://0.0.0.0:4200/api>
upvote 1
b
Yes. I was trying with Marvin without success. I am running the flow inside the docker container. I tried your suggestion and I still get the same error. No change in errors when I changed the api_url. Any other things I can try? I want to get this working so I can use prefect. Appreciate any help. If I don’t set the api_url and run the flow with the cli it works fine. But then when I try to use the server it errors like above.
c
When you're working on the same machine,
prefect server start
outputs the appropriate value for your API_URL; so in this case:
Copy code
prefect server start --host 0.0.0.0 --port 4200
and I see:
Copy code
Configure Prefect to communicate with the server with:

    prefect config set PREFECT_API_URL=<http://0.0.0.0:4200/api>
so if you run that configuration command within the docker container, your flows should run just fine after that
b
@Chris White Thank you for taking the time to help out --- okay, I made the change to match the api_url above. I got the following error when I run my script (attached). If I visit http://0.0.0.0:4200/api/health in my browser, it returns true -- also, if I visit http://127.0.0.1:4200/api/docs they all seem to work --- But seems to error out when I run my script in the container (wont talk to the server). I still am unable to get prefect working. Is there some other ENV vars I need to change? ========================================================================================= ENV PREFECT_SERVER_ANALYTICS_ENABLED=false \ PREFECT_API_URL=http://0.0.0.0:4200/api ========================================================================================= docker run -it --gpus all -d --name parse-container --network="host" -v %cd%:/app py-dev-gpu:2024SEP ========================================================================================= Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/prefect/client/orchestration.py", line 415, in api_healthcheck await self._client.get("/health") File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1801, in get return await self.request( ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1574, in request return await self.send(request, auth=auth, follow_redirects=follow_redirects) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/client/base.py", line 358, in send response.raise_for_status() File "/usr/local/lib/python3.11/site-packages/prefect/client/base.py", line 171, in raise_for_status raise PrefectHTTPStatusError.from_httpx_error(exc) from exc.cause prefect.exceptions.PrefectHTTPStatusError: Server error '503 Service Unavailable' for url 'http://0.0.0.0:4200/api/health' For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503 The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/app/scripts/hello_prefect.py", line 20, in <module> my_flow() File "/usr/local/lib/python3.11/site-packages/prefect/flows.py", line 1237, in call return enter_flow_run_engine_from_flow_call( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/engine.py", line 297, in enter_flow_run_engine_from_flow_call retval = from_sync.wait_for_call_in_loop_thread( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/_internal/concurrency/api.py", line 218, in wait_for_call_in_loop_thread return call.result() ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/_internal/concurrency/calls.py", line 318, in result return self.future.result(timeout=timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/_internal/concurrency/calls.py", line 179, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/usr/local/lib/python3.11/site-packages/prefect/_internal/concurrency/calls.py", line 389, in _run_async result = await coro ^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/client/utilities.py", line 100, in with_injected_client return await fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/prefect/engine.py", line 364, in create_then_begin_flow_run await check_api_reachable(client, "Cannot create flow run") File "/usr/local/lib/python3.11/site-packages/prefect/utilities/engine.py", line 680, in check_api_reachable raise RuntimeError( RuntimeError: Cannot create flow run. Failed to reach API at http://0.0.0.0:4200/api/.
🧐 1
c
It looks like you’re trying to run the flow in a process outside of the docker container, which is a different setup; in that case you need to set an API URL for the process that runs the flow. I unfortunately need to step away from the computer for a minute, but there are two things you’ll need to do: • port forward the appropriate port from the docker image to your local machine • Set the API URL on your machine that runs the flow Let me see if I can get someone else to hop in and send you the appropriate commands
🙏 1
n
hey @Brock ! i see that docker run command above, can you rerun that with -p 4200:4200 to expose it on port 4200 of your machine?
if its helpful to see, what i normally do when i run
prefect server start
in a container then flows locally is the following
Copy code
(prefect) nate :: ~/github.com/prefecthq/prefect ‹main›
» prefect config view
PREFECT_PROFILE='bleeding'
PREFECT_API_URL='<http://0.0.0.0:4200/api>' (from profile)

» docker run -d -p 4200:4200 --rm \
    prefecthq/prefect:3.0.0-python3.11 \
    prefect server start --host 0.0.0.0
where you can set the api url (locally where you're running flows) like
Copy code
prefect config set PREFECT_API_URL=<http://0.0.0.0:4200/api>
b
That didn't change the error (see attached) -- my workflow is a bit different due to my enterprise. I have a python base image where I install prefect -- then fire up the container -- open the container in vs code and run my scripts. Hopefully this helps understand where my issue may be. Thanks for jumping in!
@Chris White @Nate Thanks all. I appreciate you getting it working!
🙌 1
🫡 1
n
catjam