docker中python3.10.5的subprocess.run子进程运行可执行文件报FileNotFoundError

现象

执行subprocess.run(['./xray_linux_amd64', 'webscan', '--basic-crawler', f'{url}', '--json-output', f'./reports/xray-{target_id}.json'], cwd=XRAY_PATH)

在WSL(ubuntu-py3.10.12)上运行的没问题

在容器alpine-py3.10.5中报错:

FileNotFoundError: [Errno 2] No such file or directory: './xray_linux_amd64'

莫非py版本差异导致的cwd路径失效?

不是的,看了下源码问题不在这,所以这个报错很有误导性!

手动执行下,找到这个文件,居然还报这个错,不可思议,瞪眼说瞎话。

依赖包问题

执行 ldd xray_linux_amd64

Error loading shared library libpcap.so.0.8: No such file or directory (needed by xray_linux_amd64)
        libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f8e2e0a7000)
Error relocating xray_linux_amd64: pcap_setnonblock: symbol not found
Error relocating xray_linux_amd64: pcap_open_offline_with_tstamp_precision: symbol not found
Error relocating xray_linux_amd64: pcap_set_immediate_mode: symbol not found
Error relocating xray_linux_amd64: pcap_get_tstamp_precision: symbol not found
Error relocating xray_linux_amd64: __vfprintf_chk: symbol not found
Error relocating xray_linux_amd64: pcap_activate: symbol not found
Error relocating xray_linux_amd64: pcap_list_tstamp_types: symbol not found
Error relocating xray_linux_amd64: pcap_setfilter: symbol not found
Error relocating xray_linux_amd64: pcap_datalink_val_to_name: symbol not found
Error relocating xray_linux_amd64: pcap_freealldevs: symbol not found
Error relocating xray_linux_amd64: pcap_can_set_rfmon: symbol not found
Error relocating xray_linux_amd64: pcap_set_timeout: symbol not found
Error relocating xray_linux_amd64: pcap_set_tstamp_precision: symbol not found
Error relocating xray_linux_amd64: pcap_get_selectable_fd: symbol not found
Error relocating xray_linux_amd64: pcap_findalldevs: symbol not found
Error relocating xray_linux_amd64: pcap_create: symbol not found
Error relocating xray_linux_amd64: __fdelt_chk: symbol not found
Error relocating xray_linux_amd64: pcap_close: symbol not found
Error relocating xray_linux_amd64: pcap_stats: symbol not found
Error relocating xray_linux_amd64: pcap_tstamp_type_val_to_name: symbol not found
Error relocating xray_linux_amd64: pcap_open_dead: symbol not found
Error relocating xray_linux_amd64: pcap_set_snaplen: symbol not found
Error relocating xray_linux_amd64: pcap_next_ex: symbol not found
Error relocating xray_linux_amd64: pcap_fopen_offline_with_tstamp_precision: symbol not found
Error relocating xray_linux_amd64: pcap_geterr: symbol not found
Error relocating xray_linux_amd64: pcap_free_tstamp_types: symbol not found
Error relocating xray_linux_amd64: pcap_set_promisc: symbol not found
Error relocating xray_linux_amd64: pcap_tstamp_type_name_to_val: symbol not found
Error relocating xray_linux_amd64: pcap_set_tstamp_type: symbol not found
Error relocating xray_linux_amd64: pcap_sendpacket: symbol not found
Error relocating xray_linux_amd64: pcap_offline_filter: symbol not found
Error relocating xray_linux_amd64: pcap_set_rfmon: symbol not found
Error relocating xray_linux_amd64: pcap_compile: symbol not found
Error relocating xray_linux_amd64: pcap_snapshot: symbol not found
Error relocating xray_linux_amd64: pcap_setdirection: symbol not found
Error relocating xray_linux_amd64: pcap_freecode: symbol not found
Error relocating xray_linux_amd64: pcap_datalink_val_to_description: symbol not found
Error relocating xray_linux_amd64: pcap_datalink_name_to_val: symbol not found
Error relocating xray_linux_amd64: pcap_statustostr: symbol not found
Error relocating xray_linux_amd64: pcap_lib_version: symbol not found
Error relocating xray_linux_amd64: pcap_open_live: symbol not found
Error relocating xray_linux_amd64: pcap_set_datalink: symbol not found
Error relocating xray_linux_amd64: __fprintf_chk: symbol not found
Error relocating xray_linux_amd64: pcap_datalink: symbol not found
Error relocating xray_linux_amd64: pcap_lookupnet: symbol not found
Error relocating xray_linux_amd64: pcap_list_datalinks: symbol not found
Error relocating xray_linux_amd64: pcap_free_datalinks: symbol not found
Error relocating xray_linux_amd64: pcap_set_buffer_size: symbol not found

原来是缺少libpcap库

装!

这些错误信息表明 xray_linux_amd64 二进制文件依赖于 libpcap 库,但在您的系统或容器中找不到这个库,或者库的版本与二进制文件需要的版本不兼容。这些 symbol not found 错误是因为二进制文件试图访问 libpcap 库中的特定函数或特性,但无法找到。

要解决这个问题,您需要在您的系统或 Docker 容器中安装 libpcap 库。根据您的 Linux 发行版,这可以通过相应的包管理器来完成:

对于基于 Debian 或 Ubuntu 的系统:
apt-get update
apt-get install libpcap-dev

对于基于 Alpine 的系统:
apk add libpcap-dev


对于基于 CentOS 或 Fedora 的系统:
yum install libpcap-devel

然并卵,还是一样的报错!

镜像问题

干脆重回ubuntu,于是基础镜像改造为

# FROM amopromo/python3.10-nginx
# 使用 Ubuntu 最新 LTS 版本
FROM ubuntu:22.04

# 设置环境变量,以避免安装过程中的交互式提示
ENV DEBIAN_FRONTEND=noninteractive

# 安装 Python 3.10 和 Nginx
RUN apt-get update && 
    apt-get install -y --no-install-recommends python3.10 python3.10-venv python3-pip nginx libpcap0.8 && 
    # 清理缓存和临时文件以减少镜像体积
    apt-get clean && 
    rm -rf /var/lib/apt/lists/*

# 暴露 Nginx 端口
# EXPOSE 80

# 设置容器启动时运行的命令
# CMD ["nginx", "-g", "daemon off;"]

COPY ./requirements.txt /Boboji/requirements.txt
WORKDIR /Boboji
RUN pip install -r requirements.txt
# CMD uvicorn srv2:app --host 0.0.0.0 --port 5777 --workers 2

运行,然后果然成功!

原来是坑爹的精简版系统引发的奇怪报错!