【问题标题】:iptables command not found in docker-compose container在 docker-compose 容器中找不到 iptables 命令
【发布时间】:2022-01-07 13:50:52
【问题描述】:

我们有 docker-compose 文件

version: '3'
services:
    prometheus-server:
        image: prom/prometheus
        ports:
            - 9090:9090
        volumes:
            - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml

    grafana-ui:
        privileged: true
        image: grafana/grafana-oss:latest
        ports:
            - 3000:3000
        environment:
            - GF_SECURITY_ADMIN_PASSWORD=secret
        links:
            - prometheus-server:prometheus
        cap_add:
            - NET_RAW
            - NET_ADMIN

但是,当我们尝试通过docker exec -it <grafana-ui container id> /bin/sh 运行iptables -nvL -t nat 时,/bin/sh: iptables: not found。我有什么遗漏吗?如何在docker容器中运行iptables

根据这个问题Installing iptables in docker container based on alpinelinux,添加了参数--cap-add=NET_ADMIN--cap-add=NET_RAW,但是我也没有运行iptables

【问题讨论】:

  • 通常你不会这样做:每个容器都有自己的网络接口,完全由 Docker 管理,Linux 权限进一步阻止你这样做。您描述的工作流程也会进行临时更改,当您重新创建容器时该更改将丢失。为什么要在调试 shell 中运行 iptables
  • 其实,根据这个question,我想知道嵌入式DNS服务器在docker中是如何工作的@DavidMaze

标签: docker docker-compose iptables


【解决方案1】:

/bin/sh: iptables: 未找到

这意味着grafana/grafana-oss:latest 默认不包括iptables 命令。

您可以使用apk add --no-cache iptables ip6tables 安装它,请参阅Running (and debugging) iptables inside a Docker container

接下来的快速实验:

nxa13855@shlava:~$ docker run --entrypoint /bin/bash -idt --cap-add=NET_ADMIN --cap-add=NET_RAW grafana/grafana-oss:latest
21296933a1d59c45c68c8ab1120b4324b717aea8d220ca070c2c8f21c449e6a5
nxa13855@shlava:~$ docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS         PORTS                    NAMES
21296933a1d5   grafana/grafana-oss:latest   "/bin/bash"              8 seconds ago   Up 7 seconds   3000/tcp                 awesome_villani
nxa13855@shlava:~$ docker exec -uroot -it 21296933a1d5 /bin/bash
bash-5.1# apk add --no-cache iptables ip6tables
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/4) Installing libmnl (1.0.4-r1)
(2/4) Installing libnftnl-libs (1.2.0-r0)
(3/4) Installing iptables (1.8.7-r1)
(4/4) Installing ip6tables (1.8.7-r1)
Executing busybox-1.33.1-r6.trigger
Executing glibc-bin-2.30-r0.trigger
/usr/glibc-compat/sbin/ldconfig: /usr/glibc-compat/lib/ld-linux-x86-64.so.2 is not a symbolic link

OK: 29 MiB in 38 packages
bash-5.1# iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
bash-5.1#

对于您,您可能需要编写您的所有者 dockerfile extends from grafana/grafana-oss:latest,在该 dockerfile 中,添加 apk add --no-cache iptables ip6tables 以使您的图像默认具有 iptables 命令。

如果只是为了调试,您可以像我上面所做的那样直接执行到该容器中安装命令,但请记住在exec 时添加-uroot,因为该图像默认不使用root

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 2021-06-12
    • 2016-10-23
    • 2017-10-01
    • 1970-01-01
    • 2018-11-13
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多