容器在同一网络上无法相互通信

huangapple go评论88阅读模式
英文:

Container on same network not communicating with each other

问题

我有一个名为e-learning的MongoDB容器,还有一个应该连接到MongoDB容器以更新我的数据库的Docker镜像,但它无法工作,我得到以下错误:
Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }

这是我的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"] 

我的Docker Compose文件:

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:

我的Docker检查网络:

[
    {
        "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": {}
    }
]
英文:

I have a mongodb container which i name e-learning
and i have a docker image which should connect to the mongodb container to update my database but it's not working i get this error:
Unknown, Last error: connection() error occurred during connection handshake: dial tcp 127.0.0.1:27017: connect: connection refused }

here's my docker build file

# 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": {}
    }
]

答案1

得分: 2

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

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

英文:

Change MONGO_URI="mongodb://localhost:27017/" to MONGO_URI="mongodb://e-learning:27017/" (working on the assumption that e-learning is the mongo container).

Within a container attached to a bridge network (the default) localhost (127.0.0.1) is the container itself. So your app container is trying to access the database at port 27017 on itself (not on the host or on the db container). The easiest solution is to use the automatic DNS resolution between containers that docker provides.

答案2

得分: 1

我添加了额外的主机,并将我的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:
英文:

I added extra hosts and changed my mongo uri to host.docker.internal
and it solved my problems

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:

huangapple
  • 本文由 发表于 2022年6月27日 17:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/72770097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定