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.
Just like python task config, in container task, we have ContainerTaskConfig which take, image spec as image, env, resource and service_account as input.
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 Workspaceor seek assistance from your cluster administrator.
Create a workflow.py where we will write the code for our workflow and place in the project root folder. (With other dependent files and requirements.txt)workflow.py
Copy
Ask AI
from truefoundry.workflow import task, workflow, ContainerTaskConfig, ContainerTaskfrom truefoundry.deploy import Imagecontainer_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" """, ], ) ),)@workflowdef 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.
Now run the below command in the terminal to deploy your workflow, replace <workfspace-fqn> with the workspace fqn which you can find on the UI.