skip to Main Content

I have my Node/Express app that connects to InfluxDB 2.0 at http://localhost:8086 or http://127.0.0.1/8086.

InfluxDB docker container is created by someone over which I don’t have any control.

In my node app, I connect to InfluxDB for local development as below,

env.js

{
    influxDB: {
        url: 'http://localhost:8086',  // OR http://127.0.0.1/8086
        token: 'xxxx==',
        org: "xxx",
        bucket: "xxx",
    }
    ...
    ...
}

and it connects to InfluxDB to get/fetch data locally. It works fine.

Now, I create a docker image and container in which my node app works but It can’t connect to Influxdb.

and it throws error.

connect ECONNREFUSED 127.0.0.1:8086

docker-compose.yml (for node app)

version: '3.8'
services:
  my-node-app:
    build:
      context: ../
      dockerfile: Dockerfile
    image: "mynodeapp:1.1.0"
    container_name: "mynodeapp-container"
    ports:
      - 5000:5000               <------ port at which my node app works

What is missing ?

Is there any change required in influxDB container or in my docker-compose file ?

3

Answers


  1. You are misunderstanding how containers work. You can think of a container as it’s own Linux system, so unless InfluxDB is inside the node app container, both localhost and 127.0.0.1 (which is also localhost) will not work.

    Typically in a deployment like yours you would want to connect to a separate InfluxDB container so the url will end up being something like http://mynodeapp-db:8086 if mynodeapp-db is the name of the database container. See Docker Hub for more details.

    If you are running the database on the host machine and your app in a container, you will want to look at this answer.

    You need to read up on Docker networks if you plan on developing with Docker. It can be slightly confusing at first but the documentation is robust.

    Login or Signup to reply.
  2. As noted, when your application inside a Docker container attempts to connect to localhost or 127.0.0.1, it is trying to connect to itself, not the host machine or any other container. That is why your attempts to connect to InfluxDB using localhost or 127.0.0.1 are failing.

    And Docker Compose provides automatic service discovery within the same docker-compose.yml file, allowing containers to communicate with each other using the service name as the hostname.

    If InfluxDB is in a separate docker compose file or managed independently, your Node.js application container and the InfluxDB container must be on the same Docker network to communicate. You can create a network and attach both containers to it. If you do not have access to modify the InfluxDB container’s configuration, you can still connect to an existing network if you know its name.

    • Create a network (if you can influence the InfluxDB container setup): docker network create <network-name>

    • Attach your container to the existing network by modifying your docker-compose.yml:

      version: '3.8'
      services:
        my-node-app:
          ...
          networks:
            - default
            - existing-network
      
      networks:
        existing-network:
          external: true
      

      A docker network ls can list the existing networks, and you will have to find the one that includes the InfluxDB container (docker inspect <container-name-or-id>).

    If InfluxDB is on your host machine, you can also try using Docker’s special DNS name host.docker.internal (for development purposes): that DNS name allows you to connect from a container back to the host machine. Update your Node.js application’s configuration to use http://host.docker.internal:8086 as the InfluxDB URL.

    Login or Signup to reply.
  3. Your issue arises from the fact that when you’re running your Node/Express app in a Docker container, localhost or 127.0.0.1 is being interpreted as the container itself, not the host machine where your InfluxDB is running. Therefore, you are getting a ECONNREFUSED error.

    Solution for

    Windows/MacOS

    {
        influxDB: {
            url: 'http://host.docker.internal:8086',  // Replace localhost/127.0.0.1 with host.docker.internal
            token: 'xxxx==',
            org: "xxx",
            bucket: "xxx",
        }
        ...
        ...
    }
    

    Linux

    {
        influxDB: {
            url: 'http://172.17.0.1:8086', // Replace localhost/127.0.0.1 with 172.17.0.1
            token: 'xxxx==',
            org: "xxx",
            bucket: "xxx",
        }
        ...
        ...
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search