今天做实验的时候,出现了一个低级失误,反复折腾了好多遍才排查出来,记录一下。
在做docker run -P
实验时,想暴露端口,于是按下列步骤操作了一番
#创建httpd容器
[root@localhost ~]# docker container run -it -d -P --name http httpd /bin/bash
af547d0dff31339222a8c7cfc52e3ef5dff32e15bca48f307c481a79d0e47bf6
#查看端口映射
[root@localhost ~]# docker container port http
80/tcp -> 0.0.0.0:49158
#测试访问,连接被拒绝
[root@localhost ~]# curl localhost:49158
curl: (56) Recv failure: Connection reset by peer
在网上搜索了大量资料,都无法解决我的问题,但是对我排查思路有一定的帮助,但是这个地方我记得我之前学习的时候肯定是可以的,于是我不服气,自己又创建了一个centos
容器,然后映射出80
端口,再自行安装httpd
服务测试了一遍,结果可想而知,思路乱了。
[root@localhost ~]# docker container run -it -p 8081:80 --name http2 --privileged centos:7 /usr/sbin/init
[root@localhost ~]# docker container exec -t http2 yum -y install httpd
[root@localhost ~]# docker container exec -t http2 systemctl start httpd
[root@localhost ~]# docker container port http2
80/tcp -> 0.0.0.0:8081
[root@localhost ~]# curl localhost:8081
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
通过这个实验首先排除了不是因为docker0
网卡的问题,有博文说重装docker0
网卡,没啥用。
其次其它博文中大多数的错误是因为dockerfile
中的network
写得有问题,要么就要去改--net=host
,还有哥们是在windows
装的docker
得使用docker machine default ip
来访问,踩了很多坑。
最终对比之前做的实验发现是创建容器时出现了错误,再来看下创建文件时使用的命令
[root@localhost ~]# docker container run -d -P --name http httpd /bin/bash
再去掉最后面的/bin/bash
[root@localhost ~]# docker container run -it -d -P --name http3 httpd
91d233bf2b28c697437c2d22874e024d7ca2d0d76a6a93107c96ac66036ede5d
[root@localhost ~]# docker container port http3
80/tcp -> 0.0.0.0:49159
[root@localhost ~]# curl localhost:49159
<html><body><h1>It works!</h1></body></html>
神奇的好了,在和小伙伴分享这个问题的时候想到,应该是默认的CMD
参数被/bin/bash
给代替了,导致容器中的80
端口没起,通过docker iamge inspect httpd
可以看到具体参数。
查看容器时也有提示
进容器中证实一下,在bin
目录下找到了apachectl
的命令,不要问我为啥不用其它命令,为啥你心里难道不明白的嘛。
root@af547d0dff31:/usr/local/apache2/bin# cat apachectl
...
case $ACMD in
start|stop|restart|graceful|graceful-stop)
$HTTPD -k $ARGV
ERROR=$?
;;
有这么一段,但是没有status
,于是只能用stop
和start
来完成测试。
#开启apache
root@af547d0dff31:/usr/local/apache2/bin# apachectl start
AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
httpd (pid 20) already running
[root@localhost ~]# curl localhost:49160
<html><body><h1>It works!</h1></body></html>
#关闭apahce
[root@localhost ~]# docker container exec -t http /usr/local/apache2/bin/apachectl stop
AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[root@localhost ~]# curl localhost:49160
curl: (56) Recv failure: Connection reset by peer
应该就是最后的/bin/bash
将httpd
的开启服务给替代了导致服务没有开启。
低级问题,但是,略坑!