skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    (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.


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search