同一网络上的容器没有相互通信



我有一个mongodb容器,我命名为电子学习我有一个docker映像,应该连接到mongodb容器来更新我的数据库,但它不工作,我得到这个错误:
未知,最后一个错误:连接()错误发生在连接握手:拨号tcp 127.0.0.1:27017:连接:连接拒绝}

这是我的docker构建文件

# syntax=docker/dockerfile:1 
FROM golang:1.18 
WORKDIR /go/src/github.com/worker 
COPY go.mod go.sum main.go  ./ 
RUN go mod download 
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . 
FROM jrottenberg/ffmpeg:4-alpine   
FROM alpine:latest 
RUN apk --no-cache add ca-certificates 
WORKDIR /root/ 
ENV LD_LIBRARY_PATH=/usr/local/lib 
COPY --from=jrottenberg/ffmpeg / / 
COPY app.env /root 
COPY --from=0 /go/src/github.com/worker/app . 
CMD ["./app"] 

my docker compose file

version: "3.9"
services:
worker:
image: worker
environment:
- MONGO_URI="mongodb://localhost:27017/"
- MONGO_DATABASE=e-learning 
- RABBITMQ_URI=amqp://user:password@rabbitmq:5672/
- RABBITMQ_QUEUE=upload
networks:
- app_network
external_links:
- e-learning
- rabbitmq
volumes:
- worker:/go/src/github.com/worker:rw
networks:
app_network:
external: true
volumes:
worker:

my docker inspect network

[
{
"Name": "app_network",
"Id": "f688edf02a194fd3b8a2a66076f834a23fa26cead20e163cde71ef32fc1ab598",
"Created": "2022-06-27T12:18:00.283531947+03:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2907482267e1f6e42544e5e8d852c0aac109ec523c6461e003572963e299e9b0": {
"Name": "rabbitmq",
"EndpointID": "4b46e091e4d5a79782185dce12cb2b3d79131b92d2179ea294a639fe82a1e79a",
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
},
"8afd004a981715b8088af53658812215357e156ede03905fe8fdbe4170e8b13f": {
"Name": "e-learning",
"EndpointID": "1c12d592a0ef6866d92e9989f2e5bc3d143602fc1e7ad3d980efffcb87b7e076",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"ad026f7e10c9c1c41071929239363031ff72ad1b9c6765ef5c977da76f24ea31": {
"Name": "video-transformation-worker-1",
"EndpointID": "ce3547014a6856725b6e815181a2c3383d307ae7cf7132e125c58423f335b73f",
"MacAddress": "02:42:ac:14:00:04",
"IPv4Address": "172.20.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]

MONGO_URI="mongodb://localhost:27017/"更改为MONGO_URI="mongodb://e-learning:27017/"(假设e-learning为mongo容器)

在连接到桥接网络(默认)的容器中,localhost(127.0.0.1)是容器本身。因此,您的应用程序容器正在尝试访问自身端口27017的数据库(而不是在主机或db容器上)。最简单的解决方案是使用docker提供的容器之间的自动DNS解析。

我添加了额外的主机并将mongo uri更改为host.docker.internal它解决了我的问题

version: "3.9"
services:
worker:
image: worker
environment:
- MONGO_URI="mongodb://host.docker.internal:27017/"
- MONGO_DATABASE=e-learning 
- RABBITMQ_URI=amqp://user:password@rabbitmq:5672/
- RABBITMQ_QUEUE=upload
networks:
- app_network
external_links:
- e-learning
- rabbitmq
volumes:
- worker:/go/src/github.com/worker:rw
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
app_network:
external: true
volumes:
worker:

相关内容

  • 没有找到相关文章

最新更新