构建期间的Docker日志网络流量

时间:2019-02-16 10:54:45

标签: docker dockerfile

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

我想我将修改我的dockerfile来监视和收集所有网络请求。例如,从apt-get install开始并安装节点或rubygem软件包

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

关于我该怎么做的任何建议?

1 个答案:

答案 0 :(得分:1)

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

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中排除此流量。

相关问题