Inline Scripts in Docker
Writing code directly inside your task.
To get started with a Script task, paste your custom script inline in your YAML workflow definition along with any other configuration.
id: api_json_to_mongodbnamespace: company.teamtasks: - id: extract type: io.kestra.plugin.scripts.python.Script containerImage: python:3.11-slim beforeCommands: - pip install requests kestra > /dev/null outputFiles: - "*.json" script: | import requests import json from kestra import Kestra
response = requests.get("https://api.github.com") data = response.json()
with open("output.json", "w") as output_file: json.dump(data, output_file)
Kestra.outputs({"status": response.status_code})
- id: load type: io.kestra.plugin.mongodb.Load connection: uri: mongodb://host.docker.internal:27017/ database: local collection: github from: "{{ outputs.extract.outputFiles['output.json'] }}" description: "you can start MongoDB using: docker run -d mongo"
The example above uses a Python script added as a multiline string into the script
property. The script fetches data from an API and stores it as a JSON file in Kestra’s internal storage using the outputFiles
property. The Kestra.outputs
method captures additional output variables, such as the API response status code.
The image
argument of the docker
property specifies (optionally) the Docker image to use for the script. If you don’t specify an image, Kestra uses the default image for the language you are using. In the above example, we use the python:3.11-slim
image.
You can also optionally use the beforeCommands
property to install libraries used in your inline script. Above, the command pip install requests kestra
installs pip
packages not available in the base image python:3.11-slim
.