Test a Function Locally

You can test Functions in your local environment before deploying them to the Compute Environment you created. You'll build a local image, run it in a local Docker container, and validate the Function by invoking it locally.

Run a Function Locally

First, make sure the Docker desktop client or command-line daemon is running.

Make sure your current directory is the Function directory you want to run by changing to that directory. If your terminal is in the project root directory, use the following command to switch to the Function directory:

Next, build and run the Function by using the following command:

This command runs the newly created image in a local Docker container. The initial build and container launch might take a couple minutes, and you should see various build and launch lines. The Function is ready to receive requests once you see the "Starting " message similar to the following:

Java Functions will have similar but slightly different output.

The Function will continue to run and wait for requests until you use Ctrl-C to stop the running Function container. Leave the container terminal window running for now and use a new terminal window for the next step.

Invoke a Local Function

Now that your function is running, you can test it by sending it a mock json payload. Create a file, functions/myfunction/payload.json, for the payload:

cd to your DX project root directory and invoke the function:

This command invokes the Function running on localhost port 8080 (the default for locally running Functions), with the payload JSON file. You will see output similar to:

The execution logs will start streaming in the terminal running the Function and should look something like this:

You can now stop the local function (Ctrl-C) in the terminal window where you ran run:function:start.

Note that it's also possible to attach a debugger like VSCode's debugger to your local running Function. For more details, see Salesforce Functions Developer Guide : Debugging Functions