Docker: 8080 没有得到来自 nginx 容器的响应

Docker: 8080 not getting a response from an nginx container

我正在尝试 运行 nginx docker 容器使用以下命令将其 8080 暴露到我的 8080 端口:

docker run --name mycontainer -p 8080:8080 nginx

结果是这样的:

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/08/06 21:11:22 [notice] 1#1: using the "epoll" event method
2021/08/06 21:11:22 [notice] 1#1: nginx/1.21.1
2021/08/06 21:11:22 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/08/06 21:11:22 [notice] 1#1: OS: Linux 5.11.0-25-generic
2021/08/06 21:11:22 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/08/06 21:11:22 [notice] 1#1: start worker processes
2021/08/06 21:11:22 [notice] 1#1: start worker process 31
2021/08/06 21:11:22 [notice] 1#1: start worker process 32
2021/08/06 21:11:22 [notice] 1#1: start worker process 33
2021/08/06 21:11:22 [notice] 1#1: start worker process 34
2021/08/06 21:11:22 [notice] 1#1: start worker process 35
2021/08/06 21:11:22 [notice] 1#1: start worker process 36
2021/08/06 21:11:22 [notice] 1#1: start worker process 37
2021/08/06 21:11:22 [notice] 1#1: start worker process 38

如果我检查 docker ps:

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                               NAMES
7ccaaa84ftaf   nginx     "/docker-entrypoint.…"   7 minutes ago   Up 7 minutes   80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   proxy

但是当在我的浏览器中向 https://localhost:8080 发送请求时,没有响应并无限期地继续加载...

检查容器内的nginx服务docker exec -it proxy bash :

root@******:/# service nginx status
[ ok ] nginx is running.

有什么想法吗?

钯。我是 docker 的菜鸟并使用 ubuntu.

没有反应似乎很正常,原因有两点:

  • 根据文档(https://hub.docker.com/_/nginx),内部端口不是8080,而是80;这意味着您应该改为尝试以下命令:
    docker run --name mycontainer -p 8080:80 nginx
    顺便说一句,它可能比 运行:
    更可取 docker run --rm -p 8080:80 nginx
    考虑到 运行ning 容器是短暂的,并且由于 --rm 选项可以在停止时自动删除(因此将其命名为 --name … 是不必要的),然后在以后重新创建而不会丢失数据,因为持久数据通常存储在所谓的 Docker volumes.

  • 然后,你的URL以https://开头,但通常情况下,如果一个容器不涉及专用的TLS反向代理配置,它只接受HTTP请求是正常的(从 http://) 开始;所以你应该尝试浏览以下 URL:
    http://localhost:8080

但是如果您特别有兴趣为您的 dockerized 网站提供一些 HTTPS 支持,请参见例如this Whosebug question or browse other questions involving these tags.