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.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值