[英]django running on local machine cannot connect to docker container running MYSQL
[英]Cannot connect to a gRPC service running in local Docker container
我確實閱讀了 [this similar question][1] 的答案,但這對我解決問題沒有幫助。
我的設置:
.py
客戶端。 在該配置中,一切正常。 但是,如果我在本地 Docker 容器中啟動該remote gRPC service
( .py
客戶端仍然在本地運行):
08:49:00.434005 7 server.cc:53] Server starting
08:49:00.435603 7 server.cc:59] Server listening on 0.0.0.0:9000
我用來運行 gRPC 服務的
sudo docker run --rm -it -u dud --net=host --entrypoint=/usr/local/bin/application COOL_APP
:sudo docker run --rm -it -u dud --net=host --entrypoint=/usr/local/bin/application COOL_APP
這是我的
.py
客戶端的一段代碼:HOST = 'localhost' PORT = '9000' with grpc.insecure_channel('{}:{}'.format(HOST, PORT)) as channel:
我收到以下錯誤(AFAIK 這意味着我的.py
客戶端無法連接到我的 Docker 服務的host:port
):
Traceback (most recent call last):
File "client.py", line 31, in <module>
for record in reader:
File "/usr/local/lib/python2.7/site-packages/grpc/_channel.py", line 367, in next
return self._next()
File "/usr/local/lib/python2.7/site-packages/grpc/_channel.py", line 358, in _next
raise self
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Connect Failed"
debug_error_string = "{"created":"@1550567018.554830000","description":"Failed to create subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":2261,"referenced_errors":[{"created":"@1550567018.554828000","description":"Pick Cancelled","file":"src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc","file_line":245,"referenced_errors":[{"created":"@1550567018.554798000","description":"Connect Failed","file":"src/core/ext/filters/client_channel/subchannel.cc","file_line":867,"grpc_status":14,"referenced_errors":[{"created":"@1550567018.554789000","description":"Failed to connect to remote host: OS Error","errno":61,"file":"src/core/lib/iomgr/tcp_client_posix.cc","file_line":207,"os_error":"Connection refused","syscall":"connect","target_address":"ipv6:[::1]:9000"}]}]}]}"
我已經嘗試在我的.py
客戶端中設置localhost:9000
、 0.0.0.0:9000
和:9000
,但沒有奏效。
我不確定這是否有意義,但是當我運行時:
user-dev:~ user$ lsof -i tcp:8080
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
myservice 53941 user 6u IPv4 0x40c50fcf1d04701d 0t0 TCP localhost:http-alt (LISTEN)
user-dev:~ user$ lsof -i tcp:9000
即,我的終端沒有顯示tcp:9000
任何內容(我運行上面的命令來檢查是否真的有東西在監聽localhost:9000
)。
更新:當我使用-p 9000:9000
運行hello-world
[container][2] 時,我收到一個不同的錯誤:
debug_error_string = "{"created":"@1550580085.678869000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Socket closed","grpc_status":14}"
``` so I assume something is wrong with my gRPC service image / Docker flags.
[1]: https://stackoverflow.com/questions/43911793/cannot-connect-to-go-grpc-server-running-in-local-docker-container
[2]: https://github.com/crccheck/docker-hello-world
您需要告訴 docker 在外部公開端口。
嘗試將-p 9000:9000
添加到您的 docker run 命令。
正確的命令是:
docker run -p 9000:9000 -it -u dud --entrypoint=/usr/local/bin/application COOL_APP
基本上使用-p 9000:9000
並刪除--net=host
。
您的主機 ip不是 localhost,而是docker's ip address 。 您可以通過“docker network inspect bridge | grep IPv4Address”找到docker ip
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.