Adding Template Functions and Arguments to Service Definitions

You can add template functions to a service definition, and pass in template arguments, enabling you to programmatically access and update details about the service when it’s deployed.

To add a template function or argument to a service definition:
  1. On the Services page of the Container Console, click the Edit button beside the service name to display the Service Editor.
  2. Add the required template function or template argument on the Builder tab, the Docker Run tab, or the YAML tab.
    You’ll usually find it easiest to add and edit template functions and arguments using the YAML tab.

    Note that template function calls must always be enclosed within double curly braces as {{function-name}}, argument names must always be preceded by a period, and that function and argument names must always be capitalized exactly as shown in Template Function and Argument Reference. Beyond that, the steps to add template functions or arguments vary from tab to tab as follows:

    • On the Builder tab, click Add or Edit beside a configuration option, and enter the template function or argument in the appropriate text field. For example:
      • to call the api_token function to return the token of the deployment creator and set an environment variable named MY_DEPLOY_CREATOR_TOKEN_ENV_VAR to that value, you’d select Environment Variables from the Available Options list, click Add, and then type MY_DEPLOY_CREATOR_TOKEN_ENV_VAR in the Name field and {{api_token}} in the Value field

      • to use the .ServiceID template argument to set the value of an environment variable named MY_SERVICE_ID_ENV_VAR to the service ID, you’d select Environment Variables from the Available Options list, click Add, and then type MY_SERVICE_ID_ENV_VAR in the Name field and {{.ServiceID}} in the Value field

    • On the Docker Run tab, enter the template function or argument in the appropriate location. For example:

      • to call the api_token function to return the token of the deployment creator and set an environment variable named MY_DEPLOY_CREATOR_TOKEN_ENV_VAR to that value, you’d enter:

        -e="MY_DEPLOY_CREATOR_TOKEN_ENV_VAR={{api_token}}"
      • to use the .ServiceID template argument to set the value of an environment variable named MY_SERVICE_ID_ENV_VAR to the service ID, you’d enter:

        -e="MY_SERVICE_ID_ENV_VAR={{.ServiceID}}"

      Note that on the Docker Run tab, any double quotation marks surrounding arguments in function calls must be preceded by a backslash (\) escape character.

    • On the YAML tab, enter the template function or argument in the appropriate location. For example:
      • to call the api_token function to return the token of the deployment creator and set an environment variable named MY_DEPLOY_CREATOR_TOKEN_ENV_VAR to that value, you’d enter:
        environment:
          - 'MY_DEPLOY_CREATOR_TOKEN_ENV_VAR={{api_token}}'
      • to use the .ServiceID template argument to set the value of an environment variable named MY_SERVICE_ID_ENV_VAR to the service ID, you’d enter:
        environment:
          - 'MY_SERVICE_ID_ENV_VAR={{.ServiceID}}'

    To find out the template functions and arguments that are available, along with the correct syntax to use, see Template Function and Argument Reference.

  3. Click Save to save your changes and close the Service Editor.
When the service is next deployed, the template functions and arguments you’ve specified will be used.