1

I am struggling with Go requests between containers. The issue that I have that the rest of my containers can send request to the node Container that give response, but when I send request from my GoLang application to node I get that refuse error "dial tcp 172.18.0.6:3050: connect: connection refused". So my whole docker set up is:

version: "3.3"

services:
  ##########################
  ### SETUP SERVER CONTAINER
  ##########################
  node:
    # Tell docker what file to build the server from
    image: myUserName/mernjs:node-dev
    build:
      context: ./nodeMyApp
      dockerfile: Dockerfile.dev
    # The ports to expose
    expose:
      - 3050
    # Port mapping
    ports:
      - 3050:3050
    # Volumes to mount
    volumes:
      - ./nodeMyApp/src:/app/server/src
    # Run command
    # Nodemon for hot reloading (-L flag required for polling in Docker)
    command: nodemon -L src/app.js

    # Connect to other containers
    links:
      - mongo
    # Restart action
    restart: always

  react:
    ports:
      - 8000:8000
    build:
      context: ../reactMyApp
      dockerfile: Dockerfile.dev
    volumes:
      - ../reactMyApp:/usr/src/app
      - /usr/src/app/node_modules
      - /usr/src/app/.next
    restart: always
    environment:
      - NODE_ENV=development

  golang:
    build:
      context: ../goMyApp
    environment:
      - MONGO_URI=mongodb://mongo:27017
    # Volumes to mount
    volumes:
      - ../goMyApp:/app/server
    links:
      - mongo
      - node
    restart: always

So my React app can send the request to "http://node:3050/api/greeting/name" and it get the response even that react app is not linked to the node app but when Golang app sends request to node docker container it gets connection refuse message GetJson err: Get "http://node:3050/api/greeting/name": dial tcp 172.18.0.6:3050: connect: connection refused

func GetJson(url string, target interface{}) error {
    r, err := myClient.Get(url)
    if err != nil {
        fmt.Println("GetJson err: ", err)
        return err
    }
    defer r.Body.Close()
    return json.NewDecoder(r.Body).Decode(target)
}

type ResultsDetails struct {
    Greeting       string `bson:"greatingMessage" json:"greatingMessage"`
    Message       string `bson:"message" json:"message"`
}

func GetGreetingDetails(name string) ResultsDetails {
    var resp ResultsDetails
    GetJson("http://node:3050/api/greeting/"+name, &resp)
    return resp
}

So how do I solve the Golang request to another Docker Node Container when docker doesnt see the host as the name of my container 'node'?

Update: By accident i put Golang port, which it doenst run on any port since it is application that checks on database records. So it hasnt got any api, therefore it is not running on any port. Is that could be the problem why my golang application cannot communication to other containers?

Since i have also another golang application which is api application and it is running on 5000 port and it is well communicating to my node application?

Network info: After checking the network if node and golang share the same network and the answer is yes. All containers share the same network

  • Please reduce this to a [mcve], or is the MongoDB in there relevant? – Ulrich Eckhardt Dec 27 '21 at 09:01
  • 1
    Your question contains conflicting information. You claim that the message says that it fails to connect to `172.18.0.5:3050` while the message you actually cite is about `172.18.0.7:4000`, i.e. both different IP and different port - and notably a port which is not made accessible. – Steffen Ullrich Dec 27 '21 at 09:17
  • @UlrichEckhardt Hi! Sorry it was not related. I removed it for your clarification. Thx for such quick reply – George Shevchenko Dec 27 '21 at 10:02
  • @SteffenUllrich Hi! Sorry bad copy paste thing. Yeah the node is on the 3050 port and the request is for 3050. Also thx for such quick reply. – George Shevchenko Dec 27 '21 at 10:03
  • 1
    @GeorgeShevchenko: The IP address is still different. Also, errors don't happen magically with copy + paste. This suggests more that your are mixing things from several tries together which makes me wonder if what you ask really happened the way you described it. – Steffen Ullrich Dec 27 '21 at 10:12
  • @SteffenUllrich yeah i just copied them on two different docker build :D Maybe the IP changes over the builds. But yeah the IP is changing over each build no idea why. – George Shevchenko Dec 27 '21 at 10:23
  • @SteffenUllrich I also just emailed you on the email that you have left on your webpage "http://maulwuff.de/" – George Shevchenko Dec 27 '21 at 10:24
  • @SteffenUllrich any solution? I am really lost on this, need someone's help :( – George Shevchenko Dec 27 '21 at 11:26
  • 1
    See my initial response. Read that link and reproduce the problem in isolation. Remove everything not necessary. – Ulrich Eckhardt Dec 27 '21 at 12:00
  • How does the Node application set up its network listener; is the process inside the container actually listening on port 3050, or the default Express port 3000? Your statement that the React application can resolve the `node` host name is very surprising because it usually runs in a browser, outside of Docker, and I wonder if you're actually reaching something else. – David Maze Dec 27 '21 at 12:18
  • @DavidMaze it is not purely react in face Nextjs which has server side rendering and I send request through SSR. This is how it reaches the Node server – George Shevchenko Dec 27 '21 at 15:11
  • @UlrichEckhardt yes sure I will do a isolation version of it. But my gut tells me that it will hit the same issue – George Shevchenko Dec 27 '21 at 15:13
  • 1
    I will say this: it should work and I see nothing wrong with what you posted. Make sure all your assertions are true at the same time and state isn't cheng unexpectedly. – erik258 Dec 27 '21 at 16:39
  • @DanielFarrell thats what I think as well. But i got to try first a isolated version of it just to try – George Shevchenko Dec 27 '21 at 18:39
  • @SteffenUllrich Could it be there reason that my golang application is not exposed to any port? I just relised that it is not. and not running on any port – George Shevchenko Dec 28 '21 at 13:09
  • @DanielFarrell Could it be there reason that my golang application is not exposed to any port? I just relised that it is not. and not running on any port – George Shevchenko Dec 28 '21 at 13:09
  • 1
    @GeorgeShevchenko: Your client application does not need to be exposed to any port. Exposing ports is for listeners, i.e. servers which accept connections. Clients instead initiate connections. – Steffen Ullrich Dec 28 '21 at 14:09

2 Answers2

0

(Unrelated to my issue) To anyone who has "dial tcp connection refused" issue I suggest to go though that guide https://maximorlov.com/4-reasons-why-your-docker-containers-cant-talk-to-each-other/. Really helpful. To those who this guide wont help prob read bellow this, maybe you trying to request the container api after just containers were built :D

For those who was interested what was wrong: Technically reason why I was getting this error is because of the request that I was trying to run, was just when all containers were built. I believe there is some delay to the network after containers are built. Thats why there host was throwing "dial tcp 172.18.0.6:3050: connect: connection refused" I've run that test on other containers that could possibly send request to that node container and they were all failing after the build time. But when re-requesting after few seconds all worked out.

Sorry to bother you guys. I really spent 3 days into this issue. And I was looking into completely wrong direction. Never thought that the issue is that silly :D

Thanks for you time.

0

I've met the same error in my harbor registry service. After I docker exec -it into the container, and check if the service is available, and finally I found that http_proxy has been set.

Remove the http_proxy settings for docker service, then it works like a charm.

Failed on load rest config err:Get "http://core:8080/api/internal/configurations": dial tcp 172.22.0.8:8080: connect: connection refused

$docker exec -it harbor-jobservice  /bin/bash
$echo $http_proxy $https_proxy
david euler
  • 714
  • 8
  • 12