Caio Henrique
07/21/2025, 6:19 PMMarvin
07/21/2025, 6:19 PMMarvin
07/21/2025, 6:20 PMflow.py
with a basic flow:
python
from prefect import flow, task
@task
def say_hello(name):
return f"Hello, {name}!"
@flow
def hello_flow(name: str = "world"):
message = say_hello(name)
print(message)
return message
2. Create a simple Dockerfile
in the same directory:
dockerfile
# Use the official Python image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt
# Copy your flow code
COPY flow.py .
# Set the command to run your flow
CMD ["python", "flow.py"]
3. Create a requirements.txt
file:
prefect>=3.0.0
4. Create a Docker work pool using the CLI:
bash
prefect work-pool create my-docker-pool --type docker
5. Create a deployment script named `deploy.py`:
python
from prefect.deployments import Deployment
from prefect.infrastructure.docker import DockerContainer
from flow import hello_flow
if __name__ == "__main__":
docker_container = DockerContainer(
image="my-flow-image:latest",
image_registry={"dockerfile": "Dockerfile"},
)
deployment = hello_flow.deploy(
name="docker-example",
work_pool_name="my-docker-pool",
infrastructure=docker_container,
)
6. Build and deploy your flow:
bash
# Run the deployment script
python deploy.py
7. Start a worker to run your flows:
bash
prefect worker start --pool my-docker-pool
8. Run your flow:
bash
prefect deployment run hello-flow/docker-example
Some important notes:
1. Make sure Docker is installed and running on your system
2. The worker needs to have access to Docker to build and run containers
3. All required dependencies should be listed in your requirements.txt
4. The Docker work pool will handle building the image and running the container for each flow run
This is a minimal setup that demonstrates the basic concepts. You can enhance it by:
- Adding more dependencies to requirements.txt
- Customizing the Dockerfile with additional system packages or configuration
- Adding environment variables or secrets management
- Implementing more complex flows with multiple tasks
The Docker work pool will handle the container lifecycle, ensuring each flow run gets its own isolated environment.
Links for further reading:
- Docker Infrastructure Guide
- Work Pools and Workers Guide
Would you like me to explain any part of this setup in more detail?Caio Henrique
07/21/2025, 6:24 PMMarvin
07/21/2025, 6:24 PMMarvin
07/21/2025, 6:24 PMbash
prefect work-pool create local-pool --type docker
Choose one of the two approaches I outlined above (either use your existing Dockerfile or let Prefect generate one), modify your deploy.py
accordingly, and try running it again.
Would you like me to:
1. Help you debug your existing Dockerfile setup?
2. Show you what should be in your Dockerfile if you want to keep using it?
3. Help you set up the automatic Dockerfile generation approach?
Just let me know which path you'd prefer to take!Caio Henrique
07/21/2025, 6:26 PMMarvin
07/21/2025, 6:26 PMMarvin
07/21/2025, 6:27 PMflow.py
):
python
from prefect import flow, task
@task
def say_hello(name):
return f"Hello, {name}!"
@flow
def hello_flow(name: str = "world"):
message = say_hello(name)
print(message)
return message
if __name__ == "__main__":
hello_flow()
2. Your `requirements.txt`:
prefect>=3.0.0
3. Your `Dockerfile`:
dockerfile
FROM prefecthq/prefect:3-latest
COPY requirements.txt .
COPY flow.py .
4. And finally, your `deploy.py`:
python
from prefect.deployments import Deployment
from prefect.infrastructure.docker import DockerContainer
from flow import hello_flow
if __name__ == "__main__":
docker_container = DockerContainer(
image="my-flow-image:latest",
image_registry={"dockerfile": "Dockerfile"}
)
deployment = hello_flow.deploy(
name="docker-example",
work_pool_name="local-pool",
infrastructure=docker_container
)
Now, follow these steps:
1. Make sure all these files are in the same directory
2. Run the deployment:
bash
python deploy.py
3. Start a worker:
bash
prefect worker start --pool local-pool
4. In a new terminal, run your flow:
bash
prefect deployment run hello-flow/docker-example
This should work correctly. The main differences from what you were trying are:
1. We're using DockerContainer
to properly configure the Docker infrastructure
2. We're specifying the Dockerfile explicitly
3. We're using the correct deployment structure
If you get any errors with these steps, please share them and I'll help you debug them. Also, make sure:
1. Docker is installed and running on your system
2. You're in the correct directory when running these commands
3. All the files have exactly the content shown above
Would you like me to explain any part of this in more detail?Caio Henrique
07/21/2025, 6:28 PMprefect.deployments:Deployment
has been removed. Use flow.serve()
, flow.deploy()
, or prefect deploy
instead.
(venv) PS C:\opt\mds-test\mdsstack>Marvin
07/21/2025, 6:28 PMMarvin
07/21/2025, 6:29 PMlocal-pool
work pool created.
Now follow these steps:
1. Make sure all the files are saved with the correct content
2. Run the deployment:
bash
python deploy.py
3. Start a worker in a separate terminal:
bash
prefect worker start --pool local-pool
4. In another terminal, run your flow:
bash
prefect deployment run hello-flow/docker-example
This should work now with the corrected code. The error you encountered was because Prefect 3.x has simplified the deployment process and removed the older Deployment
class in favor of the more straightforward .deploy()
method.
If you encounter any other errors, please share them and I'll help you resolve them. Also, make sure:
1. Docker is running on your system
2. You're in the directory containing all these files when running the commands
3. Your virtual environment is activated and has Prefect 3.x installed
Would you like me to explain any part of this in more detail?