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

启动容器报错

1
2
3
[root@szl ~]# docker run -d --name centos02 -p 81:80 centos
WARNING: IPv4 forwarding is disabled. Networking will not work.
fbee706df13e5f848857bfd2d520f94a3b50d1220c944f5c32909efc9a4a640c

在宿主机中/etc/sysctl.conf添加net.ipv4.ip_forward=1

1
2
3
4
5
6
7
8
9
10
11
12
[root@szl ~]# vi /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward=1

重启network和docker服务

1
2
[root@szl ~]# systemctl restart network
[root@szl ~]# systemctl restart docker

测试成功

1
2
[root@szl ~]# docker run -it --name centos01 -p 88:80 centos /bin/bash
[root@2c602ab17f22 /]#