简体   繁体   中英

C# How to Access local network (192.168.x.x) within Docker containers

This is my first post here, I've looked across the internet in order to solve this issue but have no idea how to do this, as i am new to docker and not that great at networking (saying kindly).

I need a way to access my host machines internal network IP which has been forwarded to a port that connects to a proxy server. so essentially 192.168.x.xx:5000 => someproxyserver.com now when setting up the SOCKS5 on my local machine or even in C# it is able to connect and it works. but this is not the case with docker, again i am complete noob at docker, i have tried many things, but i can't seem to get my docker container to connect to the socks5 server.

I even tried to run the docker container with --network=host but i get an error (no route to host) Ideally i would not want to use --network host as i have many other containers connecting to a external network.

var settings = new ProxySettings() {
  Host = localIp,
  Port = port
};
using (var proxyClientHandler = new ProxyClientHandler<Socks5>(settings)) {
  using (var httpClient = new HttpClient(proxyClientHandler)) {
    var response = await httpClient.GetStringAsync("https://api.ipify.org?format=json");
  }
}

this is the C# code which i am using to connect to the SOCKS5 server.

When using --network=host

在此处输入图片说明

without --network=host

在此处输入图片说明

我设法以某种方式完成了这项工作,没有包含任何运行命令,我相信这是由于我的防火墙阻止访问而失败的,这很奇怪,因为我确实允许访问它,但是在早些时候重新启动我的计算机后,它工作了:) 所以是的,如果有人偶然发现了这一点,请记住确保您的防火墙没有阻止对 docker 的访问

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