
使用
rust、libpcap和tui-rs, 写了一个监控网络流量的命令行 UI, 因为想简化使用的方式, 又了解到--net=host可以访问到宿主机的网络, 所以想放入docker容器.
顺利成章的写出了如下的 dockerfile
本来是分阶段构建的, 为了测试方便改成直接启动了
FROM rust:latest as builder WORKDIR /usr/src/netop RUN apt-get update && apt-get install -y git libpcap-dev build-essential RUN git clone https://github.com/ZingerLittleBee/netop.git RUN mv netop/* . RUN cargo install --path . ENTRYPOINT [ "netop" ] 镜像构建好, 运行容器
docker run --privileged --net=host netop 然后就出现如下错误, 意思是说 eth0 这块网卡不存在, 在容器内使用 ifconfig 是可以看到 eth0
Error: Os { code: 6, kind: Uncategorized, message: "No such device or address" } docker run -i --privileged --net=host --name=netop rust:latest 手动执行了 Dockerfile 中的命令
apt-get update && apt-get install -y git libpcap-dev build-essential git clone https://github.com/ZingerLittleBee/netop.git mv netop/* . cargo install --path . 运行依旧是之前的错误
我保持了交互式容器的会话, 然后新打开一个终端标签使用 docker exec
docker exec -it netop bash 进入容器, 运行 netop 就可以跑起来, 运行起来是这样的 
排除下
我怀疑是权限问题, 但是容器使用了特权模式和 host 网络
为什么 docker run dockerfile 会出错, 但是 docker exec 可以执行呢
非常感谢大佬提点, 加上 -it 参数, 就没问题了
docker run -it --net=host netop 1 choury 2022-06-04 22:46:05 +08:00 docker run -it xxx 试下,这个设备说的不清不楚,说不定是 tty 设备 |