docker - 无法连接到转发任何端口的 docker 端口

标签 docker ubuntu nginx portforwarding docker-proxy

操作系统:Ubuntu 20.04 LTS

Docker版本20.10.7,内部版本20.10.7-0ubuntu1~20.04.2

尝试过这个(最小的重现方式),前一天它工作正常:

docker run --name nginx1 -p 8089:80 -d nginx:alpine

工作正常:

docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED         
74faabf44b8e   nginx:alpine   "/docker-entrypoint.…"   5 minutes ago   

STATUS         PORTS                                       NAMES
Up 5 minutes   0.0.0.0:8089->80/tcp, :::8089->80/tcp       nginx1

curl 在 docker 中工作:

docker exec -it 74faabf44b8e sh

/ # curl localhost
<!DOCTYPE html>
<html>
<head>
....

但是当从主机访问时:

netstat -antpel | grep 8089
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:8089            0.0.0.0:*               LISTEN      0          82748695   -                   
tcp6       0      0 :::8089                 :::*                    LISTEN      0          82751546   - 

curl -v localhost:8089
*   Trying 127.0.0.1:8089...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8089 (#0)
> GET / HTTP/1.1
> Host: localhost:8089
> User-Agent: curl/7.68.0
> Accept: */*
> 
--> stuck without a response, after a while:
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer

可能的原因是什么?
这也不适用于另一个容器(PostgreSQL),似乎 docker-proxy 不会转发来自 docker 的响应?

已经尝试过:

  • 重新安装 docker.io
  • 删除所有容器并重新创建
  • docker 网络修剪

最佳答案

Ubuntu 版本:22.04 LTS

Docker 版本:20.10.17,内部版本 100c701

安装流程:https://docs.docker.com/engine/install/ubuntu/

我在最新版本的 Ubuntu 和 Docker 上遇到了与您相同的问题:一切似乎都工作正常,只是主机无法访问端口上的容器,如 -p <docker-host-port>:<docker-container-port> 所定义的那样。选项。

curl http://localhost:<port>尽管端口绑定(bind),但可以在容器内部工作,但不能从外部工作...

我尝试重新启动docker守护进程,重新启动机器,删除容器,重新创建容器,删除镜像,重新创建镜像,完全卸载docker,从头开始重新安装docker。我只是不明白为什么 Docker 在这个特定实例(Ubuntu 22.04 + Docker)上表现得如此时髦。

关于docker - 无法连接到转发任何端口的 docker 端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69455895/

相关文章:

git - 如何为目录和目录外的文件创建 git 存储库?

linux - 重命名以文件夹名称为前缀的文件夹中的所有文件

unicorn 的示例 nginx.conf 中不允许使用 nginx 指令

同一网络中的 Docker 容器不通信

node.js - 多语言微服务方法

docker - 如何按订单向docker swarm用户docker堆栈部署服务

shell - 如何在Dockerfile中将环境变量作为Shell脚本的参数传递?

docker - 无法写入kubernetes中部署的dockerized flask应用程序的静态目录中的文件

linux - 使用 dup2 的 Ubuntu Linux C 编程中的多个管道

php - 在不同的机器上配置 php 和 nginx