Custom Actions FileNotFoundError

For testing, I have a json file where the custom action will get its response from. This worked locally but when I deployed it to my server using docker-compose, it can no longer find the file and gives me this error:

FileNotFoundError: [Errno 2] No such file or directory: './data/mall.json'

Here is the custom action:

class ActionMallLocation(Action):

    def name(self) -> Text:
        return "action_mall_location"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        current_mall = next(tracker.get_latest_entity_values("mall"), None)
        malls = open('./data/mall.json')
        mall_data = json.load(malls)

        for i in range(len(mall_data['mall_details'])):

            mall_name = (mall_data['mall_details'][i]['mall_name'])
            if mall_name == current_mall:
                mall_location = mall_data['mall_details'][i]['mall_location']
                msg = f"{mall_name} is located at {mall_location}."
                dispatcher.utter_message(text=msg)

            if not mall_name:
                msg = f"I didn't recognize {current_mall}. Is it spelled correctly?"
                dispatcher.utter_message(text=msg)
                return []

        malls.close()

Here is my project structure:

Here is my action server on my docker-compose file: image

I was able to resolve this by adding the directory of the data folder on my volumes in the docker-compose file:

  action-server:
    image: rasa/rasa-sdk:2.8.2
    container_name: action-server
    build:
      context: actions
    volumes:
      - ./actions:/app/actions
      - ./data:/app/data
    ports:
      - 5055:5055  

By opening a file with the name “filename.ext” using the open() function, you are using a relative path to indicate that the file is located in the current working directory.

file = open('filename.ext') //relative path

In the code snippet above, only the name of the file is provided to the open() function, which is a relative path. If the file with the given name is not present in the working directory, it will raise a “FileNotFoundError: [Errno 2] No such file or directory” error. In such cases, using the exact or absolute path to the file can resolve the issue.

file = open(r'C:\path\to\your\filename.ext') //absolute path

The above code uses an absolute path, which contains all the information needed to locate the file, including the root directory and any subdirectories.

If the user does not provide the full path to the file (which, on Unix systems, must start with a slash), the file path in Python is interpreted relative to the current working directory. By default, the current working directory is the directory from which you started the Python program. However, in order for this to work, the directory that contains the Python executable must be included in the system’s PATH environment variable, which specifies directories that are automatically searched for executables when a command is entered. Regardless, if your Python script and input file are located in different directories, you must either specify a relative path between them or use an absolute path for one of them.