无法通过curl或浏览器访问Docker容器

9avjhtql  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(326)

我使用以下命令创建了一个nginx容器。
$ docker run -it -d --name test-nginx -p 8090:8091 nginx
看起来一切正常

$ docker ps
CONTAINER ID   IMAGE                               COMMAND                  CREATED             STATUS             PORTS                                                                                                                                                                                                                                              NAMES
e36f30ce7854   nginx                               "/docker-entrypoint.…"   About an hour ago   Up About an hour   80/tcp, 0.0.0.0:8090->8091/tcp, :::8090->8091/tcp                                                                                                                                                                        test-nginx

但是我不能通过浏览器或curl命令访问容器。我得到以下错误。

$ curl localhost
curl: (7) Failed connect to localhost:80; Connection refused
$ curl localhost:8090
curl: (56) Recv failure: Connection reset by peer
curl localhost:8091
curl: (7) Failed connect to localhost:8091; Connection refused
$ curl 10.57.8.115
curl: (7) Failed connect to 10.57.8.115:80; Connection refused
$ curl 10.57.8.115:8090
curl: (7) Failed connect to 10.57.8.115:8090; Connection refused
$ curl 10.57.8.115:8091
curl: (7) Failed connect to 10.57.8.115:8091; Connection refused

有谁能帮我解决问题吗?什么时候解决?

$ curl localhost:8090

它给出以下错误?

curl: (56) Recv failure: Connection reset by peer
7hiiyaii

7hiiyaii1#

nginx监听容器中的端口80,因此这是您需要在主机上公开的端口。您公开的是端口8091。因此您的命令应该是:

docker run -it -d --name test-nginx -p 8091:80 nginx

以公开本地主机端口8091上的容器端口80。然后,您的curl命令应该会成功运行:

$ curl -I localhost:8091
HTTP/1.1 200 OK
Server: nginx/1.23.1
Date: Fri, 07 Oct 2022 07:29:43 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 19 Jul 2022 14:05:27 GMT
Connection: keep-alive
ETag: "62d6ba27-267"
Accept-Ranges: bytes

相关问题