欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker问题:容器互连时访问宿主机出现“No route to host”的问题

程序员文章站 2024-03-13 23:43:40
...

Docker问题:容器互连时访问宿主机出现“No route to host”的问题


问题:

环境:CentOS、离线安装的docker环境
场景:博主在用离线镜像tar包安装mysql后,运行并在Windows上用Navicatjava项目都可以连接上,但是将项目打包成镜像部署到服务器上启动时,报错了,两条错误信息:

  • 1.The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  • 2.No route to host

个人估计是网络配置的原因,创建docker服务后,docker会有一个默认的内部网络,172.17.0.1
查看网络信息,命令:ip a

[root@localhost server]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:93:3b:ac:9f brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:93ff:fe3b:ac9f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

解决方法:依次执行以下命令即可

nmcli connection modify docker0 connection.zone trusted

systemctl stop NetworkManager.service

firewall-cmd --permanent --zone=trusted --change-interface=docker0

systemctl start NetworkManager.service

nmcli connection modify docker0 connection.zone trusted

systemctl restart docker.service

执行完上面的命令后,再启动前面创建的容器即可

参考博客:https://blog.****.net/iouczp/article/details/80300500


相关博客:离线安装docker服务.