dousi6701 2018-07-24 18:35
浏览 453

从容器内连接到mongoDB会话

I'm new to learning how to use goLang to build microservices. I had a whole project up and running locally, but when I tried deploying it I ran into a problem. The session I was working with (mgo.Dial("localhost")) was no longer working. When I put this into a docker image, it failed to connect to the local host, which makes sense, since the docker image builds it over a new OS (alpine in my case). I was wondering what I should do to get it to connect.

To be clear, when I was researching this, most people wanted to connect to a mongoDB session that is a docker container, I want to connect to a mongoDB session from within a docker container. Also once I'm ready for deployment I'll be using StatefulSet with kubernetes if that changes anything.

For example, this is what I want my program to be like:

sess, err := mgo.Dial("localhost") //or whatever
if err != nil {
   fmt.Println("failed to connect")
else {
   fmt.Println("connected")

What I tried doing: Dockerfile:

FROM alpine:3.6

COPY /build/app /bin/
EXPOSE 8080

ENTRYPOINT ["/bin/app"]

In terminal:

docker build -t hell:4 .
docker run -d -p 8080:8080 hell:4

And as you can expect, it says not connected. Also the port mapping is for the rest of the project, not this part.

Thanks for your help!

  • 写回答

2条回答 默认 最新

  • dougong9987 2018-07-24 19:04
    关注

    If your mongoDB server is running in your host machine, replace localhost by you host IP.

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大