Using raw container task

In Truefoundry workflow, a container task allows you to execute a command or script inside a Docker container without needing to write a custom Python function for that specific task. It provides a way to use pre-built Docker images and run commands directly within those containers.

ContainerTaskConfig

  • Just like python task config, in container task, we have ContainerTaskConfig which take, image spec as image, env, resource and service_account as input.
from truefoundry.workflow import task, workflow, ContainerTaskConfig, ContainerTask
from truefoundry.deploy import Image

container_task = ContainerTask(
    name="container_task",
    task_config=ContainerTaskConfig(
        image=Image(
            image_uri="...",
            command=[...],
        ),
        service_account="...",  # replace with your service account
    ),
)

Example

Now lets take a look at the example where we will just use the alpine base image and then print the number from 1 to 10 and will print their sum.

Prerequisites

Before you proceed with the guide, make sure you have the following:

  • Truefoundry CLI: Set up and configure the TrueFoundry CLI tool on your local machine by following the Setup for CLI guide.
  • Workspace: To deploy your workflow, you'll need a workspace. If you don't have one, you can create it using this guide: Creating a Workspace or seek assistance from your cluster administrator.

Creating the workflow

Create a workflow.py where we will write the code for our workflow and then create deploy.py file where we will store the truefoundry configuration for deploying workflow and a Dockerfile.

Your directory structure will then appear as follows:

.  
├── workflow.py  
└── deploy.py

workflow.py

from truefoundry.workflow import task, workflow, ContainerTaskConfig, ContainerTask
from truefoundry.deploy import Image

container_task = ContainerTask(
    name="container_task",
    task_config=ContainerTaskConfig(
        image=Image(
            image_uri="alpine:3.12",
            command=[
                "/bin/sh",
                "-c",
                """
                # Generate a sequence of numbers from 1 to 10
                seq 1 10 | tee /var/data/numbers.txt
                
                # Calculate the sum of these numbers
                sum=$(seq 1 10 | awk '{s+=$1} END {print s}')
                
                # Save the sum to a file
                echo "Sum: $sum"
                """,
            ],
        ),
        service_account="<your-service-account-url>",  # replace with your service account
    ),
)


@workflow
def sum_of_numbers() -> str:
    container_task()
    return "The sum of numbers from 1 to 10 has been calculated."

  • So here we have defined container_task where we are using alpine image and the command which is basically a bash script to print the number from 1 to 10 and their sum.
  • The container task can be called in the same way as you call the python task.

deploy.py

from truefoundry.deploy import Workflow, LocalSource


workflow = Workflow(
    name="container-task-example",  # your workflow application name
    workflow_file_path="workflow.py",
)
workflow.deploy(workspace_fqn="<workspace-fqn>")

Now run the below command in the terminal to deploy your workflow, replace with the workspace fqn which you can find on the ui.

python deploy.py

Run the above command from the same directory containing the deploy.py file.