Home > Mobile >  Container on same network not communicating with each other
Container on same network not communicating with each other

Time:06-28

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

CodePudding user response:

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.

CodePudding user response:

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:

  • Related