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

can not ping as normal user in Docker centos image

我的 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

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

chmod 4755 /usr/bin/ping

这不是 "temp solution" 而是允许用户级别 ping 的实际解决方案 - 基本上 ping 需要根级别访问权限才能在原始模式下打开套接字。因此,当它尝试执行此操作但不是 运行 作为 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