Patrick ODonnell
07/11/2025, 3:36 PMMarvin
07/11/2025, 3:36 PMPatrick ODonnell
07/11/2025, 3:39 PMMarvin
07/11/2025, 3:39 PMMarvin
07/11/2025, 3:39 PMPatrick ODonnell
07/11/2025, 3:39 PMMarvin
07/11/2025, 3:39 PMMarvin
07/11/2025, 3:40 PMimage
parameter to flow.deploy()
, Prefect will automatically build the Docker image for you. It's rather convenient, if you're into that sort of thing.
The default behavior is:
1. Builds the image automatically (unless you explicitly set build=False
)
2. Uses a Prefect-based image as the base
3. Includes your flow's dependencies
For example:
python
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image="my-flow-image:latest"
)
If you're feeling particularly controlling (and who isn't?), you can also:
- Prevent the build by setting build=False
- Control whether the image gets pushed to a registry with the push
parameter
- Use the PREFECT_DEFAULT_DOCKER_BUILD_NAMESPACE
setting to set a default namespace for your images
You can find all the depressingly thorough details in the documentation:
- Docker deployments guide
- Python-based deployment guide
Would you like to know anything else about Docker deployments, or shall we move on to other existential questions about automation?Patrick ODonnell
07/11/2025, 3:41 PMMarvin
07/11/2025, 3:41 PMMarvin
07/11/2025, 3:41 PMDockerImage
class to point to your custom Dockerfile
3. Specify exact versions in your Dockerfile if you're feeling particularly precise
Here's a simple example of letting Prefect handle it automatically:
python
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image="my-flow-image:latest"
)
And here's how you'd do it if you don't trust the automation (wise choice, generally speaking):
python
from prefect.infrastructure import DockerImage
docker_image = DockerImage(
dockerfile="path/to/your/Dockerfile",
image_name="my-custom-image:latest"
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
You can find more details about Docker deployments in the documentation, though I warn you it's distressingly thorough.
Would you like to know anything else about Docker deployments, or shall we contemplate the existential implications of containerization?Patrick ODonnell
07/11/2025, 3:42 PMMarvin
07/11/2025, 3:42 PMMarvin
07/11/2025, 3:43 PMdockerfile
FROM --platform=linux/arm64 python:3.9
# ... rest of your Dockerfile
Or when building the image manually:
bash
docker build --platform linux/arm64 .
If you're using flow.deploy()
, you can use the DockerImage
class to point to your custom ARM64 Dockerfile:
python
from prefect.infrastructure import DockerImage
docker_image = DockerImage(
dockerfile="path/to/your/arm64/Dockerfile",
image_name="my-arm64-image:latest"
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
Given the increasing popularity of ARM architectures (particularly with those shiny M1/M2 Macs), you might want to check the Docker deployment documentation for the latest updates on official architecture support.
Would you like me to continue pondering the architectural mysteries of container builds, or shall we move on to something else?Marvin
07/11/2025, 3:47 PMflow.deploy()
with an image parameter, it does not automatically build the Docker image for you. You need to build the Docker image separately before deploying your flow. Here's how it works:
1. First, you need to build your Docker image using standard Docker commands or tools
2. Then, when deploying your flow, you specify the pre-built image name in the deployment
Here's a typical workflow:
python
from prefect import flow
@flow
def my_flow():
print("Hello from container!")
# You must build your Docker image first using docker build
# Then you can deploy using the image name
flow.deploy(
name="containerized-flow",
work_pool_name="my-docker-pool",
image="my-registry/my-image:latest" # This must already exist
)
If you want to automate the Docker image building process as part of your deployment workflow, you have a few options:
1. Use the prefect_docker
package's deployment steps:
python
from prefect_docker.deployments.steps import build_docker_image
flow.deploy(
name="containerized-flow",
work_pool_name="my-docker-pool",
build=[
build_docker_image(
image_name="my-image",
tag="latest",
dockerfile="Dockerfile"
)
]
)
2. Create a custom deployment step that builds your image
3. Build the image as part of your CI/CD pipeline before deployment
Remember that if you're using a private registry, you'll need to ensure your Prefect worker has the necessary credentials to pull the image.Patrick ODonnell
07/11/2025, 3:54 PMMarvin
07/11/2025, 3:54 PMMarvin
07/11/2025, 3:55 PMDockerImage
class rather than just passing a string as the image parameter.
Here's how you'd do it:
python
from prefect.infrastructure import DockerImage
docker_image = DockerImage(
image_name="my-image:latest",
platform="linux/arm64" # or "linux/amd64", etc.
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
If you're using a custom Dockerfile, you can combine both:
python
docker_image = DockerImage(
dockerfile="path/to/your/Dockerfile",
image_name="my-image:latest",
platform="linux/arm64"
)
Common platform values include:
- linux/amd64
(for x86_64)
- linux/arm64
(for ARM64/aarch64)
- linux/arm/v7
(for 32-bit ARM)
Would you like to know anything else about Docker architectures, or shall we contemplate the futility of cross-platform compatibility?Patrick ODonnell
07/11/2025, 3:58 PMMarvin
07/11/2025, 3:58 PMMarvin
07/11/2025, 3:58 PMpython
from prefect.docker import DockerImage # This is the correct import path
docker_image = DockerImage(
image_name="my-image:latest",
platform="linux/arm64" # or "linux/amd64", etc.
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
If for some reason that doesn't work (the universe being what it is), you can also try:
python
from prefect.docker.docker_image import DockerImage
Would you like to continue our journey through the ever-changing landscape of Python imports, or shall we move on to something else?Patrick ODonnell
07/11/2025, 4:01 PMdocker-py
documentation](https://docker-py.readthedocs.io/en/stable/images.html#docker.models.images.ImageCollection.build) for more information.
this is the docs when I hover over that new DockerImage class. Where do i pass my platform?Marvin
07/11/2025, 4:01 PMMarvin
07/11/2025, 4:01 PMbuild_kwargs
parameter since it's one of the Docker build options. Based on the Docker Python SDK documentation it references, you'd do it like this:
python
from prefect.docker import DockerImage
docker_image = DockerImage(
name="my-image",
tag="latest",
dockerfile="auto",
platform="linux/arm64" # this goes into build_kwargs
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
Or if you prefer to be more explicit:
python
docker_image = DockerImage(
name="my-image",
tag="latest",
dockerfile="auto",
**{"platform": "linux/arm64"} # same thing, just more verbose
)
The **build_kwargs
in the signature means any additional parameters you provide will be passed directly to the Docker build API. The docker-py documentation shows all the available build options, including platform
.
Would you like to know about other build options you can pass through, or shall we contemplate the elegant horror of kwargs?Patrick ODonnell
07/11/2025, 5:37 PMMarvin
07/11/2025, 5:38 PMMarvin
07/11/2025, 5:38 PMpython
from prefect.docker import DockerImage
docker_image = DockerImage(
name="my-image",
tag="latest",
dockerfile="auto",
platform="linux/amd64" # This is the standard AMD64/x86_64 platform
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
This is the most common platform for cloud infrastructure and standard PC hardware. It's equivalent to x86_64 and is what you'd typically want if you're:
- Deploying to most cloud providers (AWS, GCP, Azure)
- Running on standard Intel/AMD processors
- Need compatibility with most server environments
Would you like to know about other platform options, or shall we accept that the world of CPU architectures is surprisingly straightforward for once?Patrick ODonnell
07/11/2025, 5:38 PMPatrick ODonnell
07/11/2025, 5:39 PMMarvin
07/11/2025, 5:39 PMMarvin
07/11/2025, 5:39 PMtag
defaults to "latest" and dockerfile
defaults to "auto" as shown in the signature you shared.
Here's the minimal version that will work just fine:
python
from prefect.docker import DockerImage
docker_image = DockerImage(
name="my-image",
platform="linux/amd64"
)
flow.deploy(
name="my-deployment",
work_pool_name="my-docker-pool",
image=docker_image
)
When dockerfile
is "auto", Prefect will automatically generate a Dockerfile based on your current environment. This is usually what you want unless you have specific Docker configuration needs.
Would you like to know what that auto-generated Dockerfile looks like, or shall we bask in the rare joy of sensible defaults?