将端口转发到本身已转发的docker容器端口

时间:2019-11-23 15:34:20

标签: docker networking tcp portforwarding docker-container

我正在从docker容器中运行Google Cloud Platform Datalab,并且在将端口转发到主机时遇到问题,该主机在运行datalab的docker容器中转发。我创建了一个Docker容器,如下所示

docker container run -p 127.0.0.1:8081:8081 -it gcp

其中gcp是我的docker映像。

在这里,我希望将主机127.0.0.1:8081的连接转发到容器的8081。但是容器的8081端口将自己转发。

因此,在容器的交互式终端中运行命令后,尝试从主机使用curl时,同级重置了连接。

我试图检查容器(容器的pid为342)中的端口为

root@C8308:~# nsenter -t 342 -n netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 C8308:48378             173.194.73.95:https     ESTABLISHED
tcp        0      0 C8308:43026             35.223.43.12:ssh        ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path

但是没有找到任何与端口转发有关的东西。 curl命令依次指定该端口上没有运行http

root@C8308:~# curl -v 127.0.0.1:8081
* Rebuilt URL to: 127.0.0.1:8081/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8081 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8081
> User-Agent: curl/7.58.0
> Accept: */*
> 
* Recv failure: Connection reset by peer
* stopped the pause stream!
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

但是curl从docker端口可以正常工作:

root@59e7c60d5a09:~# curl -v localhost:8081/tree/datalab                                                                                                                                                                               
*   Trying 127.0.0.1...                                                                                                                                                                                                                
* TCP_NODELAY set                                                                                                                                                                                                                      
* Connected to localhost (127.0.0.1) port 8081 (#0)                                                                                                                                                                                    
> GET /tree/datalab HTTP/1.1                                                                                                                                                                                                           
> Host: localhost:8081                                                                                                                                                                                                                 
> User-Agent: curl/7.58.0                                                                                                                                                                                                              
> Accept: */*                                                                                                                                                                                                                          
>                                                                                                                                                                                                                                      
< HTTP/1.1 200 OK                                                                                                                                                                                                                      
< Content-Type: text/html                                                                                                                                                                                                              
< Date: Sat, 23 Nov 2019 15:29:54 GMT                                                                                                                                                                                                  
< Connection: keep-alive                                                                                                                                                                                                               
< Transfer-Encoding: chunked                                                                                                                                                                                                           
<                                                                                                                                                                                                                                      
<!DOCTYPE html>                                                                                                                                                                                                                        
<html>                                                                                                                                                                                                                                 
<head>       
//etc...

如何解决此类问题?我犯了什么错误?

UPD:我在docker容器内的8081端口上运行了nginx,并且docker端口转发工作正常。

0 个答案:

没有答案