今天碰到了个很奇怪的事情,在docker里面运行nginx的时候,成功启动,监听centos的80端口

结果访问一直被拒,网页也打不开

记录一下我的解决方法以及各种尝试

我的docker启动参数

docker run -itd --name nginx -p 80:80 nginx bash

先检查一下端口情况

netstat -tnlp

发现80端口正常被docker-proxy使用

之后又检查防火墙有没有给我拦住

firewall-cmd --list-all #检查防火墙的配置

刚开始是没有80端口的,我手动开启后网页依旧打不开

firewall-cmd --zone=public --add-port=80/tcp --permanent。#开启80端口

之后又尝试关闭防火墙,使用

systemctl stop firewalld.service 关闭防火墙

此时telnet可以连接80,网页依旧访问不到

我意识到可能是正在运行的nginx容器需要进入内部启动nginx

[root@localhost ~]# docker exec -it nginx(此处是你的容器name) bash #进入容器内部

root@c431554b3059:/# service nginx start #开启nginx

2022/05/09 03:35:02 [notice] 21#21: using the "epoll" event method

2022/05/09 03:35:02 [notice] 21#21: nginx/1.21.6

2022/05/09 03:35:02 [notice] 21#21: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)

2022/05/09 03:35:02 [notice] 21#21: OS: Linux 3.10.0-1160.62.1.el7.x86_64

2022/05/09 03:35:02 [notice] 21#21: getrlimit(RLIMIT_NOFILE): 1048576:1048576

root@c431554b3059:/# 2022/05/09 03:35:02 [notice] 22#22: start worker processes

2022/05/09 03:35:02 [notice] 22#22: start worker process 23

root@c431554b3059:/# exit #退出容器

此时我可以打开网页了

 另外,我是个刚入行的菜鸟,错误的地方希望指正

查看原文