我的 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
最佳答案
这不是“临时解决方案”,而是允许用户级别 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
关于centos - 无法在 Docker centos 镜像中以普通用户身份 ping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553923/