node.jsdockerdocker-composeaxiosphpbb

ECONNREFUSED Inside of Docker Container; Accessing Remote API


Recently, I've been building a nodejs bot to run games on a phpbb forum. I have the bot running locally on my machine (logging in, posting, scraping threads, ect.), so, naturally, I've started to dockerize it.

However, even when running through the login workflow from inside of my docker container, I'm receiving ECONNREFUSED errors from inside of the container when trying to hit the forum's api. There's no official docs for the aforementioned api, so a lot of this has been self-investigation.

I'm running node v14.4.0 with axios v0.19.2 + toughcookie v4.0.0 for requests. I'm able to successfully curl the endpoint with the same payload from inside of the container, so I suspect there's something that's going over my head with axios/node.js. I've tried to look at other issues with docker and ECONNREFUSED, but most of them on stackoverflow relate to inter-container communication instead of issues with external apis/access.

The container is running as a root user, and I don't have any sort of proxies or wonky networking set up.

Does anyone have any advice or inklings? My docker-compose is pretty bare bones, but I've included it below for reference.

version: '3.2'
services:
  bot:
    build: .
    env_file:
      - .env
    ports:
      - '80:80'

Any tips or theories would be much appreciated; I've hit the end of my list!

Cheers!


Solution

  • I assume you try to reach the host from the container. A simple way of accomplishing this is to use host networking instead of container networking. Try

    version: '3.2'
    services:
      network_mode: host
      bot:
        build: .
        env_file:
          - .env
    

    A better way is probably to put the rest of your application in container as well and use the service discovery in Compose, e.g.

    version: '3.2'
    services:
      bot:
        build: .
        env_file:
          - .env
      webapp:
        ...
    

    Then you can reach the PHP app by connecting to the host name "webapp" in the above example.