Ravish Mallya
08/21/2025, 11:43 AMMarvin
08/21/2025, 11:43 AMMarvin
08/21/2025, 11:55 AMjob_variables.container_create_kwargs.ports
.
Here are two practical examples you can copy-paste.
1) Python: deploy with Flow.from_source(...).deploy
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
# 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:
prefect deploy
Create and run the Docker work pool/worker
- Create a Docker work pool (once):
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):
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.