douqiong8412 2018-04-30 10:37
浏览 275
已采纳

尝试连接到MongoDB容器时,GOLANG客户端Docker容器变得无法访问

I have a simple program written in golang which connect to a mongodb instance thus:

func main() {
    session, err := mgo.Dial("localhost:27017")    

    if err != nil {
        panic(err)
    }

    defer session.Close()
    session.SetMode(mgo.Monotonic, true)
    ensureIndex(session)
}

I use docker compose to spin up two containers one to spin up my GOLANG API and the other to spin up mongo:

version: "3.3"

services:
  api:
    image: golang:latest
    volumes:
      - .:/go
    working_dir: /go
    environment:
      - GOPATH=/go
    command:
      go run /go/src/main.go
    ports:
      - "8082:8000"
    depends_on: 
      - db
    networks:
      - api-net    

  db:
    build: .
    expose:
      - '27017'
    container_name: 'mongo'
    ports:
      - "27017:27017"
    networks:
      - api-net

networks:
  api-net:
    driver: bridge

I use a Dockerfile in order to spin up mongo with a conf file that sets the instance to bind to all IPV4 and IPV6 networks:

FROM mongo:latest

COPY mongod.conf /etc/mongod.conf
CMD mongod --config /etc/mongod.conf##

and this is the mongod.conf file:

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

When I run docker-compose.up this is what I get:

Building db
Step 1/3 : FROM mongo:latest
 ---> a0f922b3f0a1
Step 2/3 : COPY mongod.conf /etc/mongod.conf
 ---> Using cache
 ---> f270e718c11e
Step 3/3 : CMD mongod --config /etc/mongod.conf
 ---> Running in 89ffc2495a2a
Removing intermediate container 89ffc2495a2a
 ---> fe2677d53122

Successfully built fe2677d53122
Successfully tagged carsupermarket_db:latest
WARNING: Image for service db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating mongo ... done
Creating carsupermarket_api_1 ... done
Attaching to mongo, carsupermarket_api_1
api_1  | panic: no reachable servers
api_1  |
api_1  | goroutine 1 [running]:
api_1  | main.main()
api_1  |        /go/src/main.go:38 +0x291
api_1  | exit status 2

I've trawled Google and stackoverflow and the only thing I could manage to find which is vaguely related to my issue is:

mongod --bind_ip using docker-compose version 2

however, my docker-compose.yml file as it stands 'Should' on paper work.

Can someone please point me in the correct direction as to why my GOLANG code cannot find my mongodb instance.

  • 写回答

2条回答 默认 最新

  • dongye6377 2018-04-30 10:45
    关注

    I think that when you call mgo.Dial("localhost:27017") the localhost part refers to the localhost within the golang container, and not the host the the containers are running on.

    Docker will resolved container names as host names, change the dial to mgo.Dial("mongo:27017") and it should work.

    From the docker container networking bridge docs

    User-defined bridges provide automatic DNS resolution between containers.

    Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.

    https://docs.docker.com/network/bridge/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记