简体   繁体   English

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

[英]How to connect local Mongo database to docker

I am working on golang project, recently I read about docker and try to use docker with my app. 我正在从事golang项目,最近我阅读了有关do​​cker的信息,并尝试在我的应用程序中使用docker。 I am using mongoDB for database. 我正在使用mongoDB数据库。 Now problem is that, I am creating Dockerfile to install all packages and compile and run the go project. 现在的问题是,我正在创建Dockerfile来安装所有软件包并编译和运行go项目。 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:: 我在本地运行mongo数据,如果我在没有docker的情况下运行go程序,它会给我输出,但是如果我在同一项目中使用docker(只需在此项目上安装依赖项并运行项目),则编译成功但不会给出任何输出,有错误:

CreateSession: no reachable servers 

my Dockerfile:: 我的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

When you run your application inside Docker, it's running in a virtual environment; 当您在Docker中运行应用程序时,它在虚拟环境中运行; 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 . 要将您的容器连接到主机,Docker给它提供了一个特殊的IP地址,并为该IP提供了一个值为host.docker.internal的url。

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: 因此,假设mongo在主机的每个接口上都运行绑定,则可以从容器中通过连接字符串访问它:

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

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

In your golang project, how do you specify connection to mongodb? 在您的golang项目中,如何指定与mongodb的连接? localhost:27017? 本地主机:27017?

If you are using localhost in your code, your docker container will be the localhost and since you don't have mongodb in the same container, you'll get the error. 如果您在代码中使用本地主机,则您的docker容器将为本地主机,并且由于同一容器中没有mongodb,因此会出现错误。

If you are starting your docker with command line docker run ... add --network="host" . 如果要使用命令行--network="host" docker run ... --network="host" docker run ...添加--network="host" If you are using docker-compose, add network_mode: "host" 如果您使用的是docker-compose,请添加network_mode: "host"

Ideally you would setup mongodo in it's own container and connect them from your docker-compose.yml -- but that's not what you are asking for. 理想情况下,您可以在自己的容器中设置mongodo,然后从docker-compose.yml连接它们-但这不是您想要的。 So, I won't go into that. 所以,我不会讨论这个问题。

In future questions, please include relevant Dockerfile, docker-compose.yml to the extent possible. 在以后的问题中,请尽可能包含相关的Dockerfile,docker-compose.yml。 It will help us give more specific answer. 这将帮助我们给出更具体的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM