docker run 报错——WARNING: IPv4 forwarding is disabled. Networking will not work.

时间:2023-11-25 14:01:56

执行  docker run  时遇到如下WARNING:

[root@etcd1 volumes]# docker run -d -p 8080:80 -v /tmp/test_mount httpd
Unable to find image 'httpd:latest' locally
Trying to pull repository docker.io/library/httpd ...
sha256:72f2b4aa99235509146bd12054d1a93c1c869ba60212d21729118c93ca4305d3: Pulling from docker.io/library/httpd
3d77ce4481b1: Pull complete
73674f4d9403: Pull complete
d266646f40bd: Pull complete
ce7b0dda0c9f: Pull complete
01729050d692: Pull complete
014246127c67: Pull complete
7cd2e04cf570: Pull complete
Digest: sha256:72f2b4aa99235509146bd12054d1a93c1c869ba60212d21729118c93ca4305d3
Status: Downloaded newer image for docker.io/httpd:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
a281f3580d29863a472a3d8c5945a07c0b9d485dcd49f50b3816ff6406e44513

查看 IPv4 forwarding 启用情况

[root@etcd1 volumes]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

  

解决方案:

方案1

(1)查看forward转发是否开启:

## 内核中的forward功能开启
[root@etcd1 volumes]# cat /proc/sys/net/ipv4/ip_forward
0
## 或者
[root@etcd1 volumes]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

  

(2)设置生效

[root@etcd1 volumes]# echo "1" > /proc/sys/net/ipv4/ip_forward

## 或者
[root@etcd1 volumes]# sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

 

(3)查看是否生效:

[root@etcd1 volumes]# cat /proc/sys/net/ipv4/ip_forward
1
## 或者
[root@etcd1 volumes]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1

 

上述方式,可以达到立即生效的效果,但是重启网络服务之后,就会失效,所以,这种情况可以用于调试。

方案2: 

(1)设置生效:

[root@etcd1 volumes]# echo net.ipv4.ip_forward=1 >> /usr/lib/sysctl.d/00-system.conf  ## 或者 /etc/sysctl.conf
[root@etcd1 volumes]# systemctl restart network

(2)查看是否修改成功:

[root@etcd1 volumes]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1 

重启docker服务:

[root@etcd1 volumes]# systemctl restart docker

  

完成!