Container Support with fal
fal now supports running functions within custom Docker containers, providing greater flexibility and control over your environment.
Example: Using Custom Containers with fal functions
Here’s a complete example demonstrating how to use custom containers with fal
.
import fal
from fal.container import ContainerImage
dockerfile_str = """FROM python:3.11
RUN apt-get update && apt-get install -y ffmpegRUN pip install pyjokes ffmpeg-python"""
@fal.function( kind="container", image=ContainerImage.from_dockerfile_str(dockerfile_str),)def test_container(): # A dependency that might have complex installation requirements. import ffmpeg ( ffmpeg.input("input.mp4") .filter('thumbnail', n=300) .output("thumbnail_filter_2.png") .run() ) # And tell me a joke! import pyjokes print(pyjokes.get_joke())
print("done")
if __name__ == "__main__": test_container()
Detailed Explanation
- Importing fal and ContainerImage:
import falfrom fal.container import ContainerImage
- Creating a Dockerfile String:
A multi-line string (
dockerfile_str
) is defined, specifying the base image aspython:3.11
, and installingffmpeg
andpyjokes
packages.
dockerfile_str = """FROM python:3.11
RUN apt-get update && apt-get install -y ffmpegRUN pip install pyjokes ffmpeg-python"""
Alternatively, you can use a Dockerfile path to specify the Dockerfile location:
import pathlibPWD = Path(__file__).resolve().parent@fal.function( kind="container", image=ContainerImage.from_dockerfile(f"{PWD}/Dockerfile"),)def test_container(): ...
-
Defining the Container Function: The
@fal.function
decorator specifies that this function runs in a container. Theimage
parameter is set usingContainerImage.from_dockerfile_str(dockerfile_str)
, which builds the Docker image from the provided Dockerfile string.@fal.function(kind="container",image=ContainerImage.from_dockerfile_str(dockerfile_str),) -
Function Implementation: Inside
test_container
, theffmpeg
library processes a video to create a thumbnail image. Then, it usespyjokes
to print a random joke.def test_container():import ffmpeg(ffmpeg.input("input.mp4").filter('thumbnail', n=300).output("thumbnail_filter_2.png").run())import pyjokesprint(pyjokes.get_joke())print("done")
Running the Function
To run the function, save the code to a file (e.g., test_container.py
) and execute it using the fal run
command:
fal run test_container.py
or directly from the Python interpreter:
python test_container.py
This example demonstrates how to leverage Docker containers in fal, enabling customized execution environments for your functions. For more details and advanced usage, refer to the fal Container Documentation.