Skip to main content

ContainerTask

This is an intermediate class that represents Flyte Tasks that run a container at execution time. This is the vast majority of tasks - the typical @task decorated tasks; for instance, all run a container. An example of something that doesn't run a container would be something like the Athena SQL task.

Attributes​

AttributeTypeDescription
MetadataFormattyping.Literal = Literal["JSON", "YAML", "PROTO"]The format of the output file, which can be "JSON", "YAML", or "PROTO".

Constructor​

Signature​

def ContainerTask(
self,
name: str,
image: Union[str, Image],
command: List[str],
inputs: Optional[Dict[str, Type]] = None,
arguments: Optional[List[str]] = None,
outputs: Optional[Dict[str, Type]] = None,
input_data_dir: str | pathlib.Path = "/var/inputs",
output_data_dir: str | pathlib.Path = "/var/outputs",
metadata_format: MetadataFormat = "JSON",
local_logs: bool = True,
block_network: bool = False,
**kwargs,
): ...

Parameters​

NameTypeDescription
namestrName of the task.
imageUnion[str, [Image](../../image/image.md?sid=flyte__image_image)]The container image to use for the task. This can be a string or an Image object.
commandList[str]The command to run in the container. This can be a list of strings or a single string.
inputsOptional[Dict[str, Type]] = NoneThe inputs to the task. This is a dictionary of input names to types.
argumentsOptional[List[str]] = NoneThe arguments to pass to the command. This is a list of strings.
outputsOptional[Dict[str, Type]] = NoneThe outputs of the task. This is a dictionary of output names to types.
input_data_dir`strpathlib.Path` = "/var/inputs"
output_data_dir`strpathlib.Path` = "/var/outputs"
metadata_formatMetadataFormat = "JSON"The format of the output file. This can be "JSON", "YAML", or "PROTO".
local_logsbool = TrueIf True, logs will be printed to the console in the local execution.
block_networkbool = FalseIf True, blocks all outbound network access. Locally this sets Docker network_mode=none. On-cluster it applies the pod template sandboxed-pod-template. Defaults to False.
kwargsAnyAdditional keyword arguments passed to the superclass TaskTemplate initializer.

Methods​


execute()​

async def execute(self, **kwargs) -> Any: ...

Executes the container task locally using Docker, setting up volume bindings, streaming logs, and returning parsed output values.

Parameters​

NameTypeDescription
kwargsAnyTask inputs passed to the container command template and volume mounts

Returns​

TypeDescription
AnyThe task execution outputs parsed into their expected types

data_loading_config()​

def data_loading_config(self, sctx: SerializationContext) -> tasks_pb2.DataLoadingConfig: ...

Constructs the Flyte DataLoadingConfig protobuf message specifying container input/output directories and data format.

Parameters​

NameTypeDescription
sctxSerializationContextThe serialization context used during task compilation

Returns​

TypeDescription
tasks_pb2.DataLoadingConfigThe protobuf message configuring data loading behavior for Flyte engine execution

container_args()​

def container_args(self, sctx: SerializationContext) -> List[str]: ...

Builds the full sequence of command and argument strings to pass to the container entrypoint.

Parameters​

NameTypeDescription
sctxSerializationContextThe serialization context used during task compilation

Returns​

TypeDescription
List[str]A list of strings representing the combined container command and arguments