不能像平常的用户一样在Docker的centos映像中ping

我的Dockerfile

FROM centos RUN useradd me CMD su -c "ping localhost" me 

我的testing命令:

 $ docker build -t test . $ docker run --rm -it test ping: icmp open socket: Operation not permitted $ docker run --rm -it test /bin/bash [root@153c87b53b53 /]# ping localhost PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.126 ms 

我的临时解决scheme是https://www.centos.org/forums/viewtopic.php?t=39341

 chmod 4755 /usr/bin/ping 

这不是一个“临时解决scheme”,而是允许用户级ping的实际解决scheme – 基本上ping需要root级别访问才能在原始模式下打开套接字。 所以当它试图做到这一点,但不是以root身份运行,那么你会得到上面的错误。

所以为了这个起作用,ping必须是setuid root,这就是你在chmod 4755 /bin/ping时所做的事情 – 这意味着当你以普通用户的身份运行ping时,你可以将权限提升到root,但ping足够聪明,可以在打开套接字后直接将您放回给用户。

所以你的Dockerfile可能看起来像这样:

 FROM centos RUN chmod 4755 /bin/ping RUN useradd me CMD su -c "ping localhost" me