Back

[SOLVED] Function throws ECONNREFUSED when running local container

  • 0
  • Self Hosted
  • Functions
  • Locale
Petteri
20 May, 2023, 05:41

Hosting Appwrite on local docker container (http://localhost:80). Mocha test calls to Appwrite storage works correctly. Deployed function anyway does not connect to storage. Causes Exception: connect ECONNREFUSED 127.0.0.1:80 when running row "const fileInfo = await storage.getFile(bucketId, fileId);". Seems like function instance do not see appwrite container instance althought should be in same docker-compose network I suppose. Like said same code runs and connect to appwrite without any problems when used in mocha test. I assume this to be some env setup problem. Below envs related to functions. _APP_FUNCTIONS_SIZE_LIMIT=30000000 _APP_FUNCTIONS_TIMEOUT=900 _APP_FUNCTIONS_BUILD_TIMEOUT=900 _APP_FUNCTIONS_CONTAINERS=10 _APP_FUNCTIONS_CPUS=0 _APP_FUNCTIONS_MEMORY=0 _APP_FUNCTIONS_MEMORY_SWAP=0 _APP_FUNCTIONS_RUNTIMES=node-18.0,dotnet-6.0,python-3.9 _APP_EXECUTOR_SECRET=your-secret-key _APP_EXECUTOR_HOST=http://appwrite-executor/v1 _APP_EXECUTOR_RUNTIME_NETWORK=appwrite_runtimes _APP_FUNCTIONS_ENVS=node-18.0,dotnet-6.0,python-3.9 _APP_FUNCTIONS_INACTIVE_THRESHOLD=60

TL;DR
The user was experiencing an ECONNREFUSED error when running a local container. It turned out that the issue was caused by using 'localhost' instead of the LAN IP in the APPWRITE_ENDPOINT environment variable. The user was able to solve the problem by using the LAN IP instead. Additionally, there was a suggestion to use backticks to format the code. There was also a question about the use of .setSelfSigned(). The user shared their code and confirmed that the endpoint was set correctly. The issue seemed to be related to the function not seeing the appwrite container instance. The user speculated that it might be an environment setup problem
Petteri
20 May, 2023, 06:12

Should I set here some env like function endpoint?

safwan
20 May, 2023, 06:48

The function endpoint is basically the endpoint of your appwrite instance. Would you be able to share some code to see if maybe there was a human-error?

Petteri
20 May, 2023, 06:58

import * as sdk from 'node-appwrite' import { createWriteStream, existsSync, mkdir } from 'fs' import { Readable } from 'stream'

let client: any; let storage: any;

const DOWNLOADS_FOLDER = ./downloads;

function setup(){ try{ if(typeof process.env.APPWRITE_PROJECT_ID === 'undefined') throw Error(process.env.APPWRITE_PROJECT_ID not defined...) if(typeof process.env.APPWRITE_ENDPOINT === 'undefined') throw Error (process.env.APPWRITE_ENDPOINT not defined...); if(typeof process.env.APPWRITE_API_KEY === 'undefined') throw Error (process.env.APPWRITE_API_KEY not defined...);

TypeScript
client = new sdk.Client()
client
  .setEndpoint(process.env.APPWRITE_ENDPOINT)
  .setProject(process.env.APPWRITE_PROJECT_ID)
  .setKey(process.env.APPWRITE_API_KEY)
  // .setSelfSigned() // Use only on dev mo      
      
// databases = new sdk.Databases(client);
storage = new sdk.Storage(client);

if(typeof client === 'undefined') throw Error (`client not defined...`);
// if(typeof databases === 'undefined') throw Error (`databases not defined...`);
if(typeof storage === 'undefined') throw Error (`storage not defined...`);

console.log(`Appwrite setup done...`);

}catch(e: any){ console.log(e.message); } }

Petteri
20 May, 2023, 07:01

Calling storage fails ....const fileInfo = await storage.getFile(bucketId, fileId);

Petteri
20 May, 2023, 07:03

I was thinking this // .setSelfSigned() // Use only on dev mo...will that cause an issue?

safwan
20 May, 2023, 07:10

Just a tip - use 3 backticks ( ` ) at the start and end of your code to format it. Makes it much easier to read <:appwritepeepo:902865250427215882>

safwan
20 May, 2023, 07:11

APPWRITE_ENDPOINT what's the value for this env variable?

Petteri
20 May, 2023, 07:12

APPWRITE_ENDPOINT=http://localhost/v1

Petteri
20 May, 2023, 07:15

Running mocha tests locally works without any problems. I have these connection issue only when running deployed function.

Petteri
20 May, 2023, 07:20

This is log from deployed function

Drake
20 May, 2023, 14:27

You can't use localhost in a function because the function will try to connect to itself rather than Appwrite. Use your LAN IP instead.

Petteri
21 May, 2023, 07:40

Thanks, this solved the problem. A little bit confusing forced to use IP when using docker-compose.yaml file for deployment. Normally different instances uses network names to find each others. Anyway works now ...I appreciate your help!

safwan
21 May, 2023, 08:27

[SOLVED] Function throws ECONNREFUSED when running local container

Drake
21 May, 2023, 14:29

Yes, but for security reasons, the runtime container is on a separate docker network so it doesn't have access to the appwrite container

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more