简体   繁体   English

Subprocess.Popen在Docker容器中运行时给出“没有这样的文件”

[英]Subprocess.Popen gives “No such file” when run inside Docker container

I am running a python application from inside a docker container. 我正在从docker容器内运行python应用程序。 The application is a script that calls a set of executables sequentially using subprocess. 该应用程序是一个脚本,使用子进程顺序调用一组可执行文件。

It script ran fine when I tested it as such on my Centos machine but fails with a "file not found" (presumably for the executable) when a subprocess calling an executable is invoked inside docker container 当我在我的Centos机器上测试它时,脚本运行正常,但是当在docker容器内调用调用可执行文件的子进程时,失败并显示“找不到文件”(大概是可执行文件)

I have tried this by using both Python 2.7 and Centos7 as the base container but the problem persists. 我已经尝试过使用Python 2.7和Centos7作为基本容器,但问题仍然存在。

The python code that gives the error is: 给出错误的python代码是:

def __CallCommand(self, program, command):
        """ Allows execution of a simple command. """
        out = ""
        err = ""
        p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out,err = p.communicate()

the error is: OSError: [Errno 2] No such file or directory 错误是:OSError:[Errno 2]没有这样的文件或目录

here is my dockerfile 这是我的dockerfile

FROM python:2.7-alpine


RUN mkdir -p /input
RUN mkdir -p /output
RUN mkdir -p /executables

COPY config.yml        .
COPY executables       /executables
COPY pipeline.py       .
COPY input             /input

ENTRYPOINT ["python", "pipeline.py", "-i", "/input/inputFile.txt", "-o", "output"]

It's not entirely clear from you summary how your solution works, but assuming that you iterate over the /executables and push these to your __CallCommand , it's possible that your executables won't work on Alpine if built (linked) on CentOS. 您并不完全清楚自己的解决方案是如何工作的,但假设您遍历/executables并将这些/executables推送到__CallCommand ,如果在CentOS上构建(链接),则可执行文件可能无法在Alpine上运行。

I'm able to build and run a repro of your code but using Alpine's (busybox) executables and not trying to copy binaries in. I then copied my local distro's echo , this fails (as expected) 我能够构建和运行你的代码的repro,但使用Alpine的(busybox)可执行文件,而不是尝试复制二进制文件。然后我复制了我的本地发行版的echo ,这失败了(正如预期的那样)

You may wish to try running the executables you've copied on the container: 您可能希望尝试运行您在容器上复制的可执行文件:

docker run --interactive --tty --entrypoint ash [[your-image]]`
/ # cd executables/
/executables # ls -l
total 32
-rw-r--r--    1 root     root             0 Apr  1 23:13 1
-rw-r--r--    1 root     root             0 Apr  1 23:13 2
-rwxr-xr-x    1 root     root         31464 Apr  1 23:38 echo
/executables # ldd echo
        /lib64/ld-linux-x86-64.so.2 (0x7f405d498000)
        libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f405d498000)
Error relocating echo: __printf_chk: symbol not found
Error relocating echo: error: symbol not found
Error relocating echo: __fprintf_chk: symbol not found
/executables # ./echo Hello
ash: ./echo: not found

You should either: 你应该:

  • use a non-Alpine FROM image 使用非Alpine FROM图像
  • build binaries for Alpine 为Alpine建立二进制文件
  • install glibc on Alpine 在Alpine上安装glibc

Additional detail 其他细节

On Alpine (using its native|busybox) echo command: 在Alpine(使用其native | busybox) echo命令:

ldd /bin/echo
        /lib/ld-musl-x86_64.so.1 (0x7ff4f8848000)
        libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7ff4f8848000)

You can see ' musl ` is used (Alpine uses this version of libc) 你可以看到' musl`被使用(Alpine使用这个版本的libc)

My local host has Debian and it uses GNU C Library ( glibc ): 我的本地主机有Debian,它使用GNU C Library( glibc ):

ldd /bin/echo
        linux-vdso.so.1 (0x00007ffc88ff6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6933af1000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f6934098000)

You can see libc.so links to gnu/libc 你可以看到libc.so链接到gnu/libc

Thanks @DazWilkin! 谢谢@DazWilkin! The interactive shell into the container helped immensely! 进入容器的交互式shell帮助极大!

The first executable was a JRE file and my container did not have java (wonder what was I thinking :) running it without Java). 第一个可执行文件是一个JRE文件,我的容器没有java(想知道我在想什么:)没有Java运行它。 Added Java and Python to a Centos container and I am able to run it now. 将Java和Python添加到Centos容器中,我现在可以运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM