我正在尝试运行一个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
时,没有响应并无限期地继续加载…
检查容器docker exec -it proxy bash
中的nginx服务:
root@******:/# service nginx status
[ ok ] nginx is running.
任何想法?
Pd。我是docker新手,使用ubuntu。
似乎没有响应是正常的,因为以下两点:
-
根据文档(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卷中。 -
然后,您的URL以
https://
开始,但通常,如果容器不涉及专用的TLS反向代理配置,它只接受HTTP请求(从http://
开始)是正常的;因此,您应该尝试浏览以下URL:
http://localhost:8080
但是如果你特别有兴趣为你的dockerized网站提供一些HTTPS支持,参见例如这个StackOverflow问题或浏览涉及这些标签的其他问题。