简体   繁体   English

从 windows 容器连接到主机上运行的 MongoDb

[英]Connecting to MongoDb running on host machine from a windows container

I am learning Docker and I apologize if this question has already been asked on here but my use case is as follows.我正在学习 Docker,如果这个问题已经被问到这里,我深表歉意,但我的用例如下。

I have a .net framework application that reads/writes to a MongoDb. The connection string of the MongoDb is mongodb://user:p@$$w0rd@localhost:27018/Students .我有一个 .net 框架应用程序,它读取/写入 MongoDb。MongoDb 的连接字符串是mongodb://user:p@$$w0rd@localhost:27018/Students Upon startup the .net application pings mongodb://user:p@$$w0rd@localhost:27018/Students to perform a health check.在启动时,.net 应用程序 ping mongodb://user:p@$$w0rd@localhost:27018/Students以执行健康检查。 I am trying to containerize the .net application and I have it mostly working except that the health check is failing which makes sense to me.我正在尝试对 .net 应用程序进行容器化处理,并且它大部分时间都在工作,只是运行状况检查失败,这对我来说很有意义。

Here are the things that I have tried so far这是我到目前为止尝试过的事情

  1. Changed the connection string to mongodb://user:p@$$w0rd@host.docker.internal:27018/Students : Timeout将连接字符串更改为mongodb://user:p@$$w0rd@host.docker.internal:27018/Students : Timeout
  2. Changed the connection string to mongodb://user:p@$$w0rd@docker.for.win.localhost:27018/Students : Error response from daemon:.network host not found将连接字符串更改为mongodb://user:p@$$w0rd@docker.for.win.localhost:27018/Students :来自守护进程的错误响应:.network host not found
  3. Changed connection string to mongodb://user:abc123@172.17.0.1:27017/Students : Timeout将连接字符串更改为mongodb://user:abc123@172.17.0.1:27017/Students :超时
  4. Executed docker run... -p 27018:27018 : Error response from daemon: Failure on Windows System Call.执行docker run... -p 27018:27018 :来自守护进程的错误响应:Windows 系统调用失败。
  5. Executed docker run.. -.net=host but found out that this is only available in Linux containers.执行docker run.. -.net=host但是发现这个只能在 Linux 容器中使用。

I do realize the best thing would be to have MongDb running in a container and have everything configured via a docker-compose but I'd like to understand how.networking works and what I am doing wrong here.我确实意识到最好的办法是让 MongDb 在容器中运行并通过docker-compose配置所有内容,但我想了解.networking 的工作原理以及我在这里做错了什么。 Thank you谢谢

What is the.network you're using?您使用的是什么网络? If you did not configure the.network, it means the Windows container on the Windows host is using NAT.如果没有配置.network,说明Windows主机上的Windows容器正在使用NAT。 If that's the case, the container gets an IP address that is not visible outside of the NAT.network.如果是这种情况,容器将获得一个 IP 地址,该地址在 NAT.network 之外是不可见的。 The container can access the inte.net and other IPs on the host.network (or it should at least) but to access the container, you have to map the ports you want to open.容器可以访问 host.network 上的 inte.net 和其他 IP(或者至少应该访问)但是要访问容器,您必须 map 要打开的端口。 For example, you this is a web application, you can use -p 8080:80 on the docker run command to map the port 8080 on the host to 80 on the container.比如你这是一个web的应用,你可以在docker run命令时使用-p 8080:80将map主机上的8080端口改为容器上的80端口。 However, it seems your problem is that the container can't access the database that lives somewhere else.但是,您的问题似乎是容器无法访问位于其他地方的数据库。 Have you tried to use the IP address of the DB instance?您是否尝试使用数据库实例的 IP 地址? If the DB runs on the host itself, you could try to use the host IP address and see if that works.如果数据库在主机本身上运行,您可以尝试使用主机 IP 地址,看看是否可行。

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

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