[英]Request between Docker containers failing dial tcp 172.18.0.6:3050: connect: connection refused
I am struggling with Go requests between containers.我正在努力处理容器之间的 Go 请求。 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".
我遇到的问题是我的容器的 rest 可以向节点 Container 发送请求以提供响应,但是当我从 GoLang 应用程序向节点发送请求时,我收到拒绝错误“拨打 tcp 172.18.0.6:3050:连接连接:拒绝”。 So my whole docker set up is:
所以我的整个 docker 设置是:
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
所以我的 React 应用程序可以将请求发送到“http://node:3050/api/greeting/name”,即使 React 应用程序没有链接到
node
应用程序但是当 Golang 应用程序向节点 docker 容器发送请求时它也会得到响应它得到连接拒绝消息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'?那么,当 docker 没有将主机视为我的容器“节点”的名称时,如何解决对另一个 Docker 节点容器的 Golang 请求?
Update: By accident i put Golang port, which it doenst run on any port since it is application that checks on database records.更新:偶然我放了 Golang 端口,它不会在任何端口上运行,因为它是检查数据库记录的应用程序。 So it hasnt got any api, therefore it is not running on any port.
所以它没有任何 api,因此它没有在任何端口上运行。 Is that could be the problem why my golang application cannot communication to other containers?
这可能是我的 golang 应用程序无法与其他容器通信的问题吗?
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?因为我还有另一个 golang 应用程序,它是 api 应用程序,它在 5000 端口上运行,它与我的节点应用程序通信良好?
Network info: After checking the network if node and golang share the same network and the answer is yes.网络信息:检查网络后node和golang是否共享同一个网络,答案是肯定的。 All containers share the same network
所有容器共享同一个网络
(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/ . (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-互相/ 。 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
对于那些本指南无法帮助您阅读下文的人,也许您在构建容器后尝试请求容器 api: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.
这就是为什么主机抛出“拨号 tcp 172.18.0.6:3050:连接:连接被拒绝”的原因我已经在其他可能向该节点容器发送请求的容器上运行了该测试,并且它们在构建时间之后都失败了。 But when re-requesting after few seconds all worked out.
但是当几秒钟后重新请求时,一切都解决了。
Sorry to bother you guys.抱歉打扰各位了。 I really spent 3 days into this issue.
我真的花了 3 天时间研究这个问题。 And I was looking into completely wrong direction.
我正在寻找完全错误的方向。 Never thought that the issue is that silly:D
从来没有想过这个问题是那么愚蠢:D
Thanks for you time.谢谢你的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.