简体   繁体   中英

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.

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 . Upon startup the .net application pings mongodb://user:p@$$w0rd@localhost:27018/Students to perform a health check. 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.

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
  2. Changed the connection string to mongodb://user:p@$$w0rd@docker.for.win.localhost:27018/Students : Error response from daemon:.network host not found
  3. Changed connection string to mongodb://user:abc123@172.17.0.1:27017/Students : Timeout
  4. Executed docker run... -p 27018:27018 : Error response from daemon: Failure on Windows System Call.
  5. Executed docker run.. -.net=host but found out that this is only available in Linux containers.

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. 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. If that's the case, the container gets an IP address that is not visible outside of the 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. 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. 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? If the DB runs on the host itself, you could try to use the host IP address and see if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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