【问题标题】:Cant run iptables in Dockerfile无法在 Dockerfile 中运行 iptables
【发布时间】:2022-02-14 16:50:23
【问题描述】:

我已经为此奋斗了一段时间!给定以下 Dockerfile,如果我不使用 RUN iptables... 行,然后在正在运行的 docker 容器中手动执行它们,它们就可以正常工作。但如果我将它们留在 Dockerfile 中,我会收到权限错误。

FROM ubuntu
RUN apt-get update
RUN apt-get install -y iptables
RUN iptables -I INPUT -p tcp --dport 27015 -j ACCEPT
RUN iptables -A INPUT -i eth0 -j QUEUE

docker build 的输出给出:

[+] Building 0.4s (7/8)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 199B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                   0.0s
 => [1/5] FROM docker.io/library/ubuntu                                                                            0.0s
 => CACHED [2/5] RUN apt-get update                                                                                0.0s
 => CACHED [3/5] RUN apt-get install -y iptables                                                                   0.0s
 => ERROR [4/5] RUN iptables -I INPUT -p tcp --dport 27015 -j ACCEPT                                               0.3s
------
 > [4/5] RUN iptables -I INPUT -p tcp --dport 27015 -j ACCEPT:
#7 0.256 getsockopt failed strangely: Operation not permitted
------
executor failed running [/bin/sh -c iptables -I INPUT -p tcp --dport 27015 -j ACCEPT]: exit code: 1

但如果我使用:

FROM ubuntu
RUN apt-get update
RUN apt-get install -y iptables

Docker 构建完成,然后我运行映像:

docker run -i -t --cap-add NET_RAW --cap-add NET_ADMIN 094d0bb9befb

容器打开,在命令提示符下我可以输入上面的 iptables 规则。它们被接受并完全按照我的要求工作。

有什么想法可以直接从 Dockerfile 应用这些 iptable 规则吗?

【问题讨论】:

  • ?但是为什么要在 RUN 中设置 iptables?

标签: linux docker ubuntu iptables


【解决方案1】:

如何直接从 Dockerfile 应用这些 iptable 规则?

你不能。而且它们毫无意义 - 它们正在影响为构建图像而创建的临时容器。

每个容器都有一个单独的网络命名空间,其中包括单独的接口和防火墙。在容器启动时,会创建一个单独的网络空间。

创建一个将在CMDENTRYPOINT 上运行的脚本,或者在容器启动时手动运行,并在该脚本中添加应该影响当前容器环境的命令..

【讨论】:

  • 好的,感谢您澄清这一点。我已经制作了一个脚本,但希望 iptables 可能只是在图像中配置了一些文件或类似的简单的东西。我是第一次使用 docker,我的 linux 知识也有限!
猜你喜欢
  • 1970-01-01
  • 2019-07-17
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2019-01-28
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
相关资源
最近更新 更多