No, the base images available on DockerHub only include the orchestration_extras
.
These include:
You can use the EXTRA_PIP_PACKAGES
environment variable to install additional Prefect extras required by your task. For Postgres, it would be:
from prefect.run_configs import UniversalRun # DockerRun, ECSRun, KubernetesRun work the same way
flow.run_config = UniversalRun(env={"EXTRA_PIP_PACKAGES": "prefect[postgres]"})
1. Installing Extra Dependencies at Runtime
This is a copy from the docs :
If you’re using the prefecthq/prefect
image (or an image based on prefecthq/prefect
), you can make use of the EXTRA_PIP_PACKAGES
environment variable to install dependencies at runtime. If defined, pip install ${EXTRA_PIP_PACKAGES}
is executed before the flow run starts.
For example, here we configure a flow running on Kubernetes to install scikit-learn
and matplotlib
at runtime.
from prefect.run_configs import KubernetesRun
flow.run_config = KubernetesRun(env={"EXTRA_PIP_PACKAGES": "scikit-learn matplotlib"})
Note that: For production deploys, we recommend building a custom image (as described below). Installing dependencies during each flow run can be costly (since you’re downloading from PyPI on each execution) and adds another opportunity for failure.
Use of EXTRA_PIP_PACKAGES
can be useful during development though, as it allows you to iterate on dependencies without building a new image each time.
2. Building your Own Image
You have two options:
- You can use one of the provided
prefecthq/prefect
base images available on DockerHub, - You can build your own image from scratch using e.g. one of Python or Ubuntu base images.
We strongly recommend the first option, i.e. using Prefect base images.
2.1. Extending the prefecthq/prefect
image
Here we provide an example Dockerfile
for building an image based on docker pull prefecthq/prefect:latest
, but with scikit-learn
installed.
FROM prefecthq/prefect:latest
RUN pip install scikit-learn
2.2. Building a new image from scratch
Alternatively, you can build your own image without relying on the provided prefecthq/prefect
images. The only requirement is that prefect
is installed and on $PATH
.
Here we provide an example Dockerfile
for building an image with prefect
(with the github
extra), as well as scikit-learn
and matplotlib
. We use the python:3.8-buster image as the base image.
FROM python:3.8-buster
RUN pip install prefect[github] scikit-learn matplotlib
With Python base images, you shouldn’t have to modify Python PATH. But with images such as Ubuntu <= 18.04, you may have to do that because those images rely on a prebuilt Python version which is <= 3.6 and Prefect >= 1.0 requires Python >= 3.7. See the following example Dockerfile installing the latest Python version with Miniconda:
FROM ubuntu:18.04
ENV PATH="/root/miniconda3/bin:$PATH" # modifying PATH
RUN apt-get update
RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*
RUN wget \
https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& mkdir /root/.conda \
&& bash Miniconda3-latest-Linux-x86_64.sh -b \
&& rm -f Miniconda3-latest-Linux-x86_64.sh
RUN conda install pip
RUN conda install -c conda-forge some_conda_specific_package
RUN pip install prefect[github] scikit-learn matplotlib
In either case, after you’ve built the image and pushed it to a registry:
docker build -t your_org/your_image:latest .
docker login -u username -p password
docker push your_org/your_image:latest
You can configure your flow to use it via the image
field in your flow’s run config. For example, here we configure a flow deployed on Kubernetes to use the your_org/your_image:latest
image.
from prefect.run_configs import KubernetesRun
flow.run_configs = KubernetesRun(image="your_org/your_image:latest")