WARNING: IPv4 forwarding is disabled. Networking will not work.エラーについて

DockerでCentos7を起動するとこのエラーが出た場合の対処方法を残します。

[root@localhost ~]# docker run --privileged -d --name TEST centos:centos7 /sbin/init
WARNING: IPv4 forwarding is disabled. Networking will not work.
f6510d629c0b7fe10f0ef4ac2651c300b1810bf2eed10ae24716ecfb5a608574

原因

net.ipv4.conf.all.forwardingの値が0だとダメみたい。。
確認してみる。

[root@localhost ~]# sysctl net.ipv4.conf.all.forwarding
net.ipv4.conf.all.forwarding = 0
[root@localhost ~]#

0でした。。

対処方法

1に変更することで回避可能

[root@localhost ~]# 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.conf.all.forwarding = 1    ★←この行を追加

再度Centos7を起動してみる

下記のとおりエラーなく起動できた。

[root@localhost ~]# docker run --privileged -d --name TEST centos:centos7 /sbin/init
b9a73772116fc674fcf4a1d258294dcbe050a6e8586261e83ed1ab7f8245899b
[root@localhost ~]#

トラップ

素のCentos7で起動するとネットワーク周りの設定が入っていない。
なので下記のようにipコマンドが無い。。

[root@localhost ~]# docker exec -it TEST /bin/bash
[root@b9a73772116f /]# ip a
bash: ip: command not found
[root@b9a73772116f /]#

下記コマンドでネットワーク周りの設定を追加する。

[root@b9a73772116f /]# yum -y install iproute

もう一度ipコマンドを確認してみよう。
これでOK

[root@b9a73772116f /]# 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
74: eth0@if75: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@b9a73772116f /]#