首页 > 解决方案 > Docker 在构建期间记录网络流量

问题描述

在 docker 中,如何记录构建期间发生的所有网络流量。

我想我会修改我的 dockerfile 来监控和收集所有网络请求。例如从apt-get install和安装 node 或 ruby​​gem 包

我可以修改 Dockerfile 以收集日志或以其他方式输出它。

关于我如何做到这一点的任何建议?

标签: dockerdockerfile

解决方案


所有网络流量 = 所有接口(loopback包括)和所有协议上的流量(不仅http/https,您可以通过代理使用日志记录)。在所有接口的每个命令开始tcpdump时在后台启动 ( )。示例,它将所有数据包打印到:RUNeth0, lostdout

FROM alpine

RUN apk add tcpdump

# start tcpdumps in the background for each RUN
RUN sh -c 'tcpdump -nnXSs 0 -i eth0 &' \
    && sh -c 'tcpdump -nnXSs 0 -i lo &' \
    && ping -c 5 google.com

RUN sh -c 'tcpdump -nnXSs 0 -i eth0 &' \
    && sh -c 'tcpdump -nnXSs 0 -i lo &' \
    && apk add curl

构建输出:

...
Step 3/4 : RUN sh -c 'tcpdump -nnXSs 0 -i eth0 &'     && sh -c 'tcpdump -nnXSs 0 -i lo &'     && ping -c 5 google.com
 ---> Running in 63249712af4a
PING google.com (216.58.204.78): 56 data bytes
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 262144 bytes
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
64 bytes from 216.58.204.78: seq=0 ttl=127 time=17.529 ms
13:01:09.987047 IP 8.8.4.4.53 > 172.17.0.2.43264: 41194 1/0/0 A 216.58.204.46 (44)
        0x0000:  4500 0048 7096 0000 7f11 12f0 0808 0404  E..Hp...........
        0x0010:  ac11 0002 0035 a900 0034 0472 a0ea 8180  .....5...4.r....
        0x0020:  0001 0001 0000 0000 0667 6f6f 676c 6503  .........google.
        0x0030:  636f 6d00 0001 0001 c00c 0001 0001 0000  com.............
        0x0040:  001e 0004 d83a cc2e 
...

当然,您可以将这些日志发送到 Elasticsearch/Splunk/...,但您需要安装更多工具。并且可能最好将这些流量排除在tcpdump.


推荐阅读