Luca Marantelli
12/01/2024, 11:47 AMFROM python:3.9-slim
WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "prefect-test.py"]
prefect-test.py
is a simple file that print a message. If I build the image locally with Docker Desktop and run it, it works. I already pushed it on docker hub in a public repo.
Now I want to start a Prefect server versione 3 (not 2, it's important).
I created this Dokefile:
FROM prefecthq/prefect:3-latest
WORKDIR /app
CMD ["prefect", "server", "start", "--host", "0.0.0.0"]
I build the image and ran it with:
docker build -t prefect-server .
docker run -d -p 4200:4200 --name prefect-server prefect-server
Then I access the GIU with:
http://localhost:4200
Everything works.
Now I want to create a work pool, start an agent and do a deploy. The target is to create a deploy file that run the Dockerimage that run the prefect-test.py file.
I create the work pool with:
prefect work-pool create --type docker my-work-pool
I started a worker:
prefect worker start --pool docker my-work-pool
Now I have to define the deployment. I created a file named deploy_buy.py in a different folder than the root where I have the Dockerfile and prefect-test.py file:
from prefect import flow
@flow(log_prints=True)
def buy():
print("Selling securities")
if __name__ == "__main__":
buy.deploy(
name="my-deployment",
work_pool_name="my-work-pool",
image="docker.io/xxx/image"
)
I copied it from official doc here: [https://docs.prefect.io/v3/deploy/infrastructure-examples/docker][1]
[1]: https://docs.prefect.io/v3/deploy/infrastructure-examples/docker
If I run the task, I see the message "Buying securities" in logs and at the end I see:
Finished in state Completed()
Process for flow run 'xxx' exited cleanly.
But the file prefect-test.py wasn't invoked, because I don't see any other log.
I used a different folder for deploy file because, if I don't do that, when I run deploy, it gives me an error saying that a Dockerfile is already in that folder. Executing in a different folder, it creates automatically a Dockerfile with this:
FROM prefecthq/prefect:3.1.4-python3.10
COPY . /opt/prefect/deploy/
WORKDIR /opt/prefect/deploy/
This is not clear, because that file doesn't contain anything about how to run my prefect-test.py file. For this reason I created another deploy file in the root of my project with this:
from prefect import flow
from prefect.docker import DockerImage
@flow(log_prints=True)
def buy():
print("Selling securities")
if __name__ == "__main__":
buy.deploy(
name="my-deployment",
work_pool_name="my-work-pool",
image=DockerImage(
name="docker.io/xxx/image",
dockerfile="Dockerfile"
),
push=False
)
I used dockerfile="Dockerfile" because I want Prefect to use my Dockerfile. If I remove that line and try to make a deploy, as I wrote above, Prefect tells me that I already have a Dockerfile in that folder. With that line, I get no error.
I don't want prefect create another build and push it, but I want it uses the image I already have on dockerhub. For this reason I put also:
push=False
If I deploy it and run the task, I get error:
Flow could not be retrieved from deployment.
Traceback (most recent call last):
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "deploy.py", line 2, in <module>
from prefect.docker import DockerImage
ImportError: cannot import name 'DockerImage' from 'prefect.utilities.dockerutils' (/usr/local/lib/python3.9/site-packages/prefect/utilities/dockerutils.py)
The above exception was the direct cause of the following exception:
...
prefect.exceptions.ScriptError: Script at 'deploy.py' encountered an exception: ImportError("cannot import name 'DockerImage' from 'prefect.utilities.dockerutils' (/usr/local/lib/python3.9/site-packages/prefect/utilities/dockerutils.py)")
This is strange because that import is the same of the one in the example on Prefect v3 docs.
Any ideas?
ThanksNate
12/01/2024, 5:12 PMNate
12/01/2024, 6:20 PMLuca Marantelli
12/02/2024, 9:42 AMTinh
02/21/2025, 10:36 AMLuca Marantelli
03/01/2025, 7:45 AM