Vincent Chéry
07/02/2025, 12:22 PMMarvin
07/02/2025, 12:22 PMMarvin
07/02/2025, 12:24 PMmy_project/
├── flows/
│ └── my_flow.py
└── Dockerfile
2. Create a Dockerfile in your project directory:
dockerfile
FROM prefecthq/prefect:2-python3.9
COPY flows/ /opt/prefect/flows/
3. In your flow file (my_flow.py
), define your flow and deployment:
python
from prefect import flow
from prefect.deployments import Deployment
from prefect.infrastructure.docker import DockerContainer
@flow
def my_flow():
print("Hello from Docker!")
if __name__ == "__main__":
docker_block = DockerContainer(
image="my-flow-image:latest",
volumes=["local/path:/opt/prefect/flows"]
)
deployment = my_flow.from_source(
source=".", # Use local directory as source
entrypoint="flows/my_flow.py:my_flow"
).deploy(
name="docker-flow",
work_pool_name="my-docker-pool",
infrastructure=docker_block
)
4. Create a Docker work pool in the Prefect UI or via CLI:
bash
prefect work-pool create my-docker-pool --type docker
5. Build your Docker image:
bash
docker build -t my-flow-image .
6. Deploy your flow:
bash
python flows/my_flow.py
Key points to remember:
- The volumes
parameter in DockerContainer
maps your local directory to the container path
- The source="."
parameter tells Prefect to use the local directory as the code source
- The entrypoint
parameter specifies the path to your flow function relative to the source directory
- Make sure your Docker work pool is running to execute the deployment
For more details, check out the Docker deployment documentation.
Note: If you're planning to run this in production, you might want to consider using a more robust code storage solution like GitHub, but this setup works well for local development and testing.Vincent Chéry
07/02/2025, 12:29 PMMarvin
07/02/2025, 12:29 PMMarvin
07/02/2025, 12:30 PMmy_project/
├── flows/
│ └── my_flow.py
└── Dockerfile
2. Create your Dockerfile:
dockerfile
FROM prefecthq/prefect:2-python3.9
COPY flows/ /opt/prefect/flows/
3. In your flow file (my_flow.py
):
python
from prefect import flow
@flow
def my_flow():
print("Hello from Docker!")
if __name__ == "__main__":
deployment = my_flow.from_source(
source=".", # Use local directory as source
entrypoint="flows/my_flow.py:my_flow"
).deploy(
name="docker-flow",
work_pool_name="my-docker-pool",
)
4. Create a Docker work pool (either through the UI or CLI):
bash
prefect work-pool create my-docker-pool --type docker
5. Build your Docker image:
bash
docker build -t my-flow-image .
6. Start a worker for your Docker work pool:
bash
prefect worker start --pool my-docker-pool
7. Deploy your flow:
bash
python flows/my_flow.py
The key differences from the previous response are:
- We no longer use DockerContainer
as it's been removed in 3.x
- Docker configuration is handled at the work pool level instead of in the deployment code
- Workers handle the execution of flows in Docker containers
You can configure Docker-specific settings (like volume mounts, environment variables, etc.) through the work pool settings in the Prefect UI or via the CLI.
For more details about Docker deployments in Prefect 3.x, check out the official documentation.