Docker Python脚本找不到文件 [英] Docker Python script can't find file

查看:341
本文介绍了Docker Python脚本找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功构建了一个Docker容器并将我的应用程序文件复制到Dockerfile中的容器中。但是,我试图执行一个引用输入文件的Python脚本(该文件在Docker构建期间复制到了容器中)。我似乎无法弄清楚为什么我的脚本告诉我它无法找到输入文件。我包括用于在下面构建容器的Dockerfile,以及正在寻找其找不到的输入文件的Python脚本的相关部分。

I've successfully built a Docker container and copied my application's files into the container in the Dockerfile. However, I am trying to execute a Python script that references an input file (that was copied into the container during the Docker build). I can't seem to figure out why my script is telling me it cannot locate the input file. I am including the Dockerfile I used to build the container below, and the relevant portion of the Python script that is looking for the input file it cannot find.

Dockerfile:

Dockerfile:

FROM alpine:latest

RUN mkdir myapplication

COPY . /myapplication

RUN apk add --update \
    python \
    py2-pip && \
    adduser -D aws

WORKDIR /home/aws

RUN mkdir aws && \
    pip install --upgrade pip && \
    pip install awscli && \
    pip install -q --upgrade pip && \
    pip install -q --upgrade setuptools && \
    pip install -q -r /myapplication/requirements.txt

CMD ["python", "/myapplication/script.py", "/myapplication/inputfile.txt"]

Python脚本的相关部分:

Relevant portion of the Python script:

if len(sys.argv) >= 2:
    sys.exit('ERROR: Received 2 or more arguments. Expected 1: Input file name')

elif len(sys.argv) == 2:
    try:
        with open(sys.argv[1]) as f:
            topics = f.readlines()
    except Exception:
        sys.exit('ERROR: Expected input file %s not found' % sys.argv[1])
else:
    try:
        with open('inputfile.txt') as f:
            topics = f.readlines()
    except:
        sys.exit('ERROR: Default inputfile.txt not found. No alternate input file was provided')

主机上的Docker命令导致错误:

Docker command on host resulting in error:

sudo docker run -it -v $HOME/.aws:/home/aws/.aws discursive python \
    /discursive/index_twitter_stream.py

上面命令中的错误:


错误:找不到默认的inputfile.txt。没有提供替代输入文件

ERROR: Default inputfile.txt not found. No alternate input file was provided

AWS内容来自于如何将主机的AWS凭证传递给Docker容器的教程用于与AWS服务交互。我从这里使用了元素: https://github.com/jdrago999/aws-cli- on-CoreOS

The AWS stuff is drawn from a tutorial on how to pass your host's AWS credentials into the Docker container for use in interacting with AWS services. I used elements from here: https://github.com/jdrago999/aws-cli-on-CoreOS

推荐答案

到目前为止,我已经确定了两个问题。 Maya G在下面的注释中指出了三分之一。

There are two issues I've identified so far. Maya G points out a third in the comments below.

条件逻辑不正确

您需要替换:

if len(sys.argv) >= 2:
    sys.exit('ERROR: Received 2 or more arguments. Expected 1: Input file name')

使用:

if len(sys.argv) > 2:
    sys.exit('ERROR: Received more than two arguments. Expected 1: Input file name')

请记住,给脚本的第一个参数始终是其自己的名称。这意味着您应该在 sys.argv 中期望1个或2个参数。

Bear in mind that the first argument given to the script is always its own name. This means you should be expecting either 1 or 2 arguments in sys.argv.

与定位有关的问题默认文件

另一个问题是您的Docker容器的工作目录为 / home / aws ,因此,当您执行Python脚本时,它将尝试解析与此相对的路径。

Another problem is that your docker container's working directory is /home/aws, so when you execute your Python script it will try to resolve paths relative to this.

这意味着:

with open('inputfile.txt') as f:

将解析为 /home/aws/inputfile.txt ,而不是 /home/aws/myapplication/inputfile.txt

您可以通过以下代码来解决此问题:

You can fix this by either changing the code to:

with open('myapplication/inputfile.txt') as f:

或(首选):

with open(os.path.join(os.path.dirname(__file__), 'inputfile.txt')) as f:

ab的来源 ove变体)

使用 CMD ENTRYPOINT

Using CMD vs. ENTRYPOINT

似乎您的脚本显然不是 正在接收 myapplication / inputfile.txt 作为参数。这可能是一个带有 CMD 的怪癖。

It also seems like your script apparently isn't receiving myapplication/inputfile.txt as an argument. This might be a quirk with CMD.

我对这两个操作之间的区别还不是100%清楚,但我总是在Dockerfile中使用 ENTRYPOINT ,这让我不难过。请参见此答案并尝试替换:

I'm not 100% clear on the distinction between these two operations, but I always use ENTRYPOINT in my Dockerfiles and it's given me no grief. See this answer and try replacing:

CMD ["python", "/myapplication/script.py", "/myapplication/inputfile.txt"]

使用:

ENTRYPOINT ["python", "/myapplication/script.py", "/myapplication/inputfile.txt"]

(感谢Maya G)

这篇关于Docker Python脚本找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆