Docker 镜像 NGINX 未公开:无法访问站点

问题描述 投票:0回答:6

1.我正在使用this指南来获取要运行的nginx网络服务器映像并使用命令

docker运行-p 8888:80 nginx

docker 运行 -p 80:80 nginx

我猜有两个或更多容器已启动并运行,但当我打开 localhost:8888 时,它显示无法访问该站点。

我还使用 this 尝试在我的浏览器上公开某些内容。它也显示了同样的问题。

2. 还有一个问题,当我使用相同的图像文件运行更多容器时,终端不会显示任何内容作为控制台,甚至不会终止,即返回到美元符号。所以我会被卡住并被迫打开另一个终端。我在这里缺少一些技巧概念吗?

请注意我已经在Windows上安装了docker并使用了docker快速启动终端来完成上述操作。

nginx docker
6个回答
9
投票

问题是我在 Windows 上的虚拟机上运行它,这发生在 docker 上。

因此,在其中一篇初学者教程中提到,端口被转发到此虚拟机端口,而不是 Windows 端口。 (只需阅读 hello world! 浏览器图像下方的注释)

因此,您必须找到 VM 操作系统的 IP 地址并将其与端口号一起粘贴到浏览器中。


6
投票

对于 Windows 中的我来说,我没有指向 localhost,而是使用了运行命令的结果 ip:

docker-machine ip default

3
投票

你应该运行:

docker run -d -p 8888:80 nginx

docker run -d -p 80:80 nginx

-d
参数,后台运行容器并打印容器ID


1
投票

使用命令获取ip:

docker-machine ip default

然后使用

http:\\ip:port\
访问nginx


0
投票

任何读过《在 AWS 上部署容器》这本书的人 使用 EC2、ECS 和 EKS”

请注意,工作流程如下 -

PS C:\Users\nrajora> docker build --tag awsnginx .\gitouch\dockers\
[+] Building 0.2s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                        0.0s 
 => => transferring dockerfile: 31B                                                                                                                                                                                         0.0s 
 => [internal] load .dockerignore                                                                                                                                                                                           0.0s 
 => => transferring context: 2B                                                                                                                                                                                             0.0s 
 => [internal] load metadata for docker.io/library/amazonlinux:2                                                                                                                                                            0.0s 
 => [1/2] FROM docker.io/library/amazonlinux:2                                                                                                                                                                              0.0s 
 => CACHED [2/2] RUN amazon-linux-extras install nginx1                                                                                                                                                                     0.0s 
 => exporting to image                                                                                                                                                                                                      0.0s 
 => => exporting layers                                                                                                                                                                                                     0.0s 
 => => writing image sha256:fe1a3a2493bb8fdc6e1838d07ce450217cb9b6d95b8459b986c180c8bc4a53c7                                                                                                                                0.0s 
 => => naming to docker.io/library/awsnginx                                                                                                                                                                                 0.0s 
PS C:\Users\nrajora> docker run -d -p 8081:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e1acddbe380c: Pull complete
e21006f71c6f: Pull complete
f3341cc17e58: Pull complete
2a53fa598ee2: Pull complete
12455f71a9b5: Pull complete
b86f2ba62d17: Pull complete
Digest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
Status: Downloaded newer image for nginx:latest
9e8dc2373283770c7f6418e9377885cf3b1a3cb02858d939d0ddd09331458fc2

现在您的 Docker 将构建、部署并可通过下面显示的 URL 访问 - http://localhost:8081/

欢迎使用 Nginx!如果您看到此页面,则 nginx Web 服务器是 成功安装并运行。需要进一步配置。

有关在线文档和支持,请参阅 nginx.org。 nginx.com 提供商业支持。

感谢您使用 nginx。

如果您想了解 dockerfile - 在这里。

FROM amazonlinux:2
RUN amazon-linux-extras install nginx1
EXPOSE 8081
CMD ["nginx", "-g", "daemon off;"]

于 2021 年 8 月 20 日对最新 docker 版本进行了测试,如下 -

PS C:\Users\nrajora> docker version
Client:
 Cloud integration: 1.0.17
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.16.4
 Git commit:        f0df350
 Built:             Wed Jun  2 12:00:56 2021
 OS/Arch:           windows/amd64
 Context:           desktop-linux

0
投票

就我而言,我尝试使用此命令来调试错误所在

➜ docker exec -it <container_name> ps aux | grep nginx
➜ docker exec -it <container_name> cat /etc/nginx/nginx.conf
➜ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>

当我检查这个时

 docker exec -it crazy_bose netstat -tuln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
tcp        0      0 :::80                   :::*                    LISTEN      

netstat 输出表明 Nginx 正在侦听容器内的端口 80。 但我将 nginx 配置为使用 IntelliJ IDEA Docker 服务在端口 5000 上运行,并且容器可能未使用正确的端口绑定启动。要检查我是否使用了此命令,此处命令带有不正确的输出

docker inspect <container_name> | grep "5000/tcp"

                "5000/tcp": [
                "5000/tcp": {},
                "5000/tcp": [

然后我停止该容器并使用此命令启动新容器

docker run -it --rm -d -p 5000:80 --name crazy_bose nginx

终于成功了---欢迎使用nginx! 命令对于调试很有用,希望对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.