Centos7 Docker容器启动报错:WARNING: IPv4 forwarding is disabled. Networking will not work


问题

最近使用Docker容器部署Jar包的时候,数据库一直连不上:

2021-01-08 02:59:31.354 ERROR [eate-1123226989] com.alibaba.druid.pool.DruidDataSource   2507 - create connection SQLException, url: jdbc:mysql://***/***?autoReconnect=true&useUnicode=true&allowMultiQueries=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=CTT&useAffectedRows=true, errorCode 0, state 08001
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
	……
	com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1572)
	……
Caused by: java.net.ConnectException: Connection timed out (Connection timed out)
	……

被这个问题困扰了很久,解决问题的方向一直是容器的镜像。

直到今天,在大佬的帮助下,发现启动容器的时候有一行不起眼的提示:

WARNING: IPv4 forwarding is disabled. Networking will not work

再结合前面的:

java.net.ConnectException: Connection timed out (Connection timed out)

淦!

问题出在了网络上!

IPv4转发已禁用。网络将不起作用。


解决

添加IPv4转发配置

  • vi /etc/sysctl.conf 或者 vi /usr/lib/sysctl.d/00-system.conf

  • 添加一行配置 net.ipv4.ip_forward=1

  • 使用 systemctl restart network 重启 network服务

  • 使用sysctl net.ipv4.ip_forward查看是否修改成功 ,如果返回为 net.ipv4.ip_forward = 1 则表示修改成功

  • 重启Docker容器,问题解决


启动容器时指定IP

还可以在启动容器时通过 -p 参数指定IP(IP为IPv4 地址)的方式来解决,例如: docker run -p 192.168.216.1:8080



参考:

【1】:centos 7 Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work

猜你喜欢

转载自blog.csdn.net/sinat_40770656/article/details/112548627