无法在Docker centos映像中作为普通用户ping

无法在Docker centos映像中作为普通用户ping,centos,docker,Centos,Docker,我的Dockerfile FROM centos RUN useradd me CMD su -c "ping localhost" me 我的测试命令: $ 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 l

我的Dockerfile

FROM centos
RUN useradd me
CMD su -c "ping localhost" me
我的测试命令:

$ 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
我的临时解决方案是

这不是一个“临时解决方案”,而是允许用户级ping的实际解决方案-基本上ping需要根级访问才能在原始模式下打开套接字。因此,当它试图这样做,但不是以root用户身份运行时,就会出现上述错误

因此,为了使其工作,ping必须设置为uid 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
FROM centos
RUN chmod 4755 /bin/ping
RUN useradd me
CMD su -c "ping localhost" me