dongzan9069 2018-09-13 13:35
浏览 294

如何将本地Mongo数据库连接到Docker

I am working on golang project, recently I read about docker and try to use docker with my app. I am using mongoDB for database. Now problem is that, I am creating Dockerfile to install all packages and compile and run the go project. I am running mongo data as locally, if I am running go program without docker it gives me output, but if I am using docker for same project (just installing dependencies with this and running project), it compile successfully but not gives any output, having error::

CreateSession: no reachable servers 

my Dockerfile::

# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang
WORKDIR $GOPATH/src/myapp

# Copy the local package files to the container's workspace.
ADD . /go/src/myapp

#Install dependencies
RUN go get ./...

# Build the installation command inside the container.
RUN go install myapp

# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/myapp

# Document that the service listens on port 8080.
EXPOSE 8080
EXPOSE 27017
  • 写回答

2条回答 默认 最新

  • dpnzf48660 2018-09-13 13:44
    关注

    When you run your application inside Docker, it's running in a virtual environment; It's just like another computer but everything is virtual, including the network.

    To connect your container to the host, Docker gives it an special ip address and give this ip an url with the value host.docker.internal.

    So, assuming that mongo is running with binding on every interface on the host machine, from the container it could be reached with the connection string:

    mongodb://host.docker.internal:21017/database

    Simplifying, Just use host.docker.internal as your mongodb hostname.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!