说明:记录一次无法访问云服务器上部署的Docker容器的问题。
问题描述
某次,我在云服务器上,使用Docker运行了一个Nginx容器,用公网IP怎么也访问不到。这种情况博主也算有经验,可以从以下几个方面去排查:
(1)运行容器时,映射的端口是否正确;
如启动容器命令如下:
powershell
docker run --name nginx -p 80:80 -d nginx
其中,-p 为指定端口,左侧为主机端口,右侧为容器端口。用图表示如下:

如 -p 90:80
,表示需要用IP:90端口来访问Nginx;
如 -p 80:90
,表示需要进入Nginx容器,修改配置文件中使用的端口,改为90才行;
(2)云服务是否开放了相应的端口;
其次,查看云服务器上面的端口访问规则,看是否开放了80端口的访问;

(3)防火墙配置
最后,再看服务器上面的防火墙状态,是否开启了防火墙?开了防火墙,是否开放了80端口;
可输入下面的命令,查看防火墙状态
powershell
systemctl status firewalld

再输入下面的命令,查看防火墙的配置,看是否开放了80端口;
powershell
systemctl status firewalld

如果没有开放,可以输下面的命令开放80端口,开发完之后刷一下防火墙配置;
powershell
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
刷新防火墙配置;
powershell
sudo firewall-cmd --reload
问题解决
上面三种方法我都试过了,还是不行。最终参考了这篇文章:Docker容器启动成功浏览器却无法访问,使用了下面的这个方法:

输入下面的命令,更新了Linux版本内核,重启系统就可以了。
(更新Linux版本内容)
powershell
yum update

(重启系统)
powershell
reboot
更新前:

更新后:

顺便提一下,我云服务器使用的是阿里云的CentOS 7.0 64位
