<@ULVA73B9P> give me an example of docker pool bas...
# marvin-ai
r
@Marvin give me an example of docker pool based deployment that maps container port to host port
m
thought for 672.7 seconds
Assuming Prefect 3.x with a Docker work pool, you can map container ports to host ports by passing Docker SDK kwargs via
job_variables.container_create_kwargs.ports
. Here are two practical examples you can copy-paste. 1) Python: deploy with Flow.from_source(...).deploy
Copy code
from prefect import Flow

# Create the deployment pointing at your repo and entrypoint
flow = Flow.from_source(
    source="<https://github.com/your-org/your-repo>",
    entrypoint="flows/my_flow.py:my_flow",
)

# Deploy to a pre-created Docker work pool, mapping container 8080 -> host 18080
flow.deploy(
    name="web-service",
    work_pool_name="my-docker-pool",
    job_variables={
        # The image your worker will run; must contain your flow code and deps
        "image": "your-repo/your-image:latest",
        # Optional: command that starts your service inside the container
        # "command": "python -m uvicorn app:api --host 0.0.0.0 --port 8080",
        # Port mapping lives under container_create_kwargs -> ports
        "container_create_kwargs": {
            # Either of these forms works with the Docker SDK:
            # Map container 8080/tcp to host 18080 on all interfaces
            "ports": {"8080/tcp": 18080}
            # Or explicitly bind to a specific host address:
            # "ports": {"8080/tcp": ("0.0.0.0", 18080)}
        },
    },
)
2) YAML: prefect.yaml deployment config
Copy code
# prefect.yaml
deployments:
  - name: web-service
    entrypoint: flows/my_flow.py:my_flow
    work_pool:
      name: my-docker-pool
      job_variables:
        image: your-repo/your-image:latest
        # optional: command: "python -m uvicorn app:api --host 0.0.0.0 --port 8080"
        container_create_kwargs:
          ports:
            "8080/tcp": 18080
Then, from the project root:
Copy code
prefect deploy
Create and run the Docker work pool/worker - Create a Docker work pool (once):
Copy code
prefect work-pool create --type docker my-docker-pool
- Start a worker attached to that pool (on the machine where you want the container to run):
Copy code
prefect worker start --pool my-docker-pool
Notes and tips - The port mapping happens on the host where the worker is running. If your worker is remote, the mapped host port is on that remote host. - Ensure your app inside the container listens on 0.0.0.0 and the container port you map (e.g., 8080). - Don’t use
network_mode: "host"
if you need host port mappings; Docker ignores port bindings in host network mode on Linux. - The Docker worker lives in the
prefect-docker
collection; make sure your worker environment has it installed:
pip install prefect-docker
. Docs and references - Docker work pool and deployment guide: Prefect 3.x Docker deployments - Prefect + Docker overview: Blog: Orchestrating with Prefect and Docker If you share your desired container/host port and how you’re deploying (Python vs YAML), I can tailor the example exactly to your setup.