从Windows访问WSL2 docker中具有“localhost”安全性的gRPC python服务器

zbdgwd5y  于 2023-06-05  发布在  Docker
关注(0)|答案(1)|浏览(267)

问题是这样的。我得到了一个由pyinstaller制作的zip文件。在该文件中有一个设置,使服务器只侦听localhost server.add_insecure_port(f'localhost:{port_num}')。我想从centos docker运行这个python文件。Docker在WSL2上运行。
到目前为止,我能够运行docker,但问题如下:我可以从WSL2(在我的例子中是ubunty)对localhost:port进行grpcUrl调用。但是当我尝试从windows cmd调用这些函数时,我得到以下错误:
Failed to dial target host "localhost:9111": dial tcp [::1]:9111: connectex: No connection could be made because the target machine actively refused it.或此Failed to dial target host "127.0.0.1:9111": dial tcp 127.0.0.1:9111: connectex: No connection could be made because the target machine actively refused it.
我已经使用--network="host"启动了docker,希望它能帮助我,但它只能帮助ubuntu进行grpcUrl调用,而不能帮助windows。
任何帮助将不胜感激。请注意,我不能将localhost更改为0.0.0.0...

6yoyoihd

6yoyoihd1#

SSH隧道是我的答案。
在WSL上安装SSH服务器:

  1. sudo apt install openssh-server
  2. sudo vi /etc/ssh/sshd_config->在末尾插入下一个块
Port 2222
ListenAddress 0.0.0.0
PubkeyAuthentication no
PasswordAuthentication yes

搜索文件中的任何其他PasswordAuthentication并将其设置为yes
1.创建ssh密钥

sudo ssh-keygen -A
sudo service ssh --full-restart

1.添加脚本以在启动vi ~/.bashrc时运行SSH->在文件末尾插入下一个块

#Start ssh automatically
RUNNING=`ps aux | grep sshd | grep -v grep`
if [ -z "$RUNNING" ]; then
    sudo service ssh start > /dev/null 2>&1 &
    disown
fi

1.让ssh服务器在没有密码的情况下运行sudo visudo->在文件末尾插入下一个代码

%sudo ALL=NOPASSWD: /usr/sbin/service ssh *

1.从CMD运行wsl --shutdown重新启动WSL ->并再次打开wsl(例如通过运行ubuntu)
从CMD wsl hostname -I获取wsl - run的ip
现在从CMD ssh -L 127.0.0.1:<port>:localhost:<port> <wsl username>@<ip from wsl hostname -I> -p 2222运行

相关问题