docker curl: (56) Recv failure: Connection reset by peer问题解决方法

在进行Docker实验时,遇到一个关于端口映射的问题。尝试运行httpd容器并使用-P选项暴露端口,但无法成功访问。经过一系列排查,包括检查docker0网卡、查阅资料、对比实验,最终发现错误在于创建容器时指定了/bin/bash作为默认命令,这替代了httpd服务启动。去掉/bin/bash后,服务正常启动,问题得到解决。此问题虽小,但对理解Docker容器的运行机制有帮助。
摘要由CSDN通过智能技术生成

今天做实验的时候,出现了一个低级失误,反复折腾了好多遍才排查出来,记录一下。


在做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,于是只能用stopstart来完成测试。

#开启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/bashhttpd的开启服务给替代了导致服务没有开启。

低级问题,但是,略坑!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值