Skip to content

Commit

Permalink
Adds example of deploying a flow as a Docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilRex committed Nov 15, 2024
1 parent eb09f70 commit 6b729fc
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
67 changes: 67 additions & 0 deletions deploy/source-docker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
# Deploy a flow using Docker
This example shows how to deploy a flow in a Prefect built Docker image.
It assumes you have a work pool named `docker`. You can implicitly create the
work pool by starting a worker:
```bash
prefect worker start --pool docker --type docker
```
"""

import subprocess

from prefect import flow
from prefect.docker import DockerImage

REGISTRY_URL = ""
IMAGE_NAME = "flow-hello-world"


def get_image_tag():
"""Return the current git sha if available else latest"""
try:
return (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError:
return "latest"


def deploy():
flow.from_source(
source="./basics",
entrypoint="hello-world.py:hello",
).deploy(
name="default",
# The name of the work pool to use for this deployment
work_pool_name="docker",
image=DockerImage(
name=f"{REGISTRY_URL}/{IMAGE_NAME}" if REGISTRY_URL else IMAGE_NAME,
# Tagging the image with the current git sha is a good way to track
# which version of the code is deployed
tag=get_image_tag(),
# Autogenerate a Dockerfile based on the installed Prefect version
# This pulls in requirements.txt if present
dockerfile="auto",
# When building on an M series Mac and deploying to a cloud provider,
# we generally need to specify the platform.
platform="linux/amd64",
),
# Build the image before pushing it to the registry
build=True,
# Only push if we have a registry URL
push=True if REGISTRY_URL else False,
# Override work pool defaults with job variables
# For example, avoid pulling from the registry if we're building locally
job_variables={
"pull_policy": "Always" if REGISTRY_URL else "Never",
},
)


if __name__ == "__main__":
deploy()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ description = "Examples of how to use Prefect"
readme = "README.md"
requires-python = ">=3.9"
dependencies = [
"prefect-docker>=0.6.1",
"prefect>=3.1.2",
"psutil>=6.1.0",
]

[project.urls]
Code = "https://github.com/PrefectHQ/examples"
Code = "https://github.com/PrefectHQ/examples"
16 changes: 16 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b729fc

Please sign in to comment.