【问题标题】:Docker: 8080 not getting a response from an nginx containerDocker:8080 没有从 nginx 容器中得到响应
【发布时间】:2021-10-11 16:54:55
【问题描述】:

我正在尝试使用以下命令运行 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。

【问题讨论】:

    标签: docker ubuntu nginx devops


    【解决方案1】:

    没有反应似乎很正常,因为以下两点:

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

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

    但是,如果您特别有兴趣为您的 dockerized 网站提供一些 HTTPS 支持,请参阅例如this StackOverflow questionbrowse other questions involving these tags

    【讨论】:

    • 顺便问一下你的操作系统是什么?你在 Windows 下运行 Docker 吗?
    • 因为我在回答中建议的两个步骤对我有用(在 GNU/Linux Debian 下)。
    • 顺便说一句,请注意,在多次尝试之前,您需要删除初始容器,通过运行docker rm -f mycontainer,或者更好的是,每次运行docker run --rm -p 8080:80 nginx,以便在停止时删除临时容器时间(将相应地更新我的答案)。
    • 如果您正在运行适用于 Windows 或 macOS 的 Docker Toolbox,docker-machine ip 命令会返回什么?如果你得到了192.168.99.100这样的IP,你可以尝试浏览http://192.168.99.100:8080
    • @xavier “不起作用”不是对问题的描述。请具体说明(使用的命令、错误消息、已采取的调试步骤……编辑您的问题以添加)。同时,使用完全相同的图像和上述答案中建议的命令,我花了 200 万才得到它perfectly working as expected
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多