简体   繁体   English

无法在docker-compose文件中为argparse传递参数

[英]Unable to pass arguments in docker-compose file for argparse

I'm new to docker environment, I have a sample script which i accept cmd line args through argparse and print it. 我是docker环境的新手,我有一个示例脚本,我通过argparse接受cmd line args并打印它。 Python code below: Python代码如下:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-ol', '--old_file', type=str, help='path of a old file')

args = parser.parse_args()

print("Hello : " + str(args.old_file))

here is my dockerfile: 这是我的dockerfile:

FROM python:3

COPY arguments_sample.py /arguments_sample.py

ARG ol

RUN echo $ol

ENTRYPOINT ["python", "arguments_sample.py"]

and finally my docker-compose.yml: 最后我的docker-compose.yml:

 version: '3'

services:
    python-service:
        build: ./docker_stage
        environment:
        - OL='Hello'
        volumes:
        - ./sample_api:/usr/src/app_sample

usually i can run the python script as below: 通常我可以运行如下的python脚本:

python arguments_sample.py -ol Henry

and it would print it as Hello : Henry 它将打印为Hello : Henry

when i use docker-compose up it was printing Hello : None 当我使用docker-compose up它打印Hello : None

It may be wrong syntax which i'm not able to figure it out. 它可能是错误的语法,我无法弄明白。 Kindly help me! 请帮助我!

I don't know much about Python but I can see that you have a couple things wrong in the yml. 我对Python知之甚少,但我可以看到你在yml中有一些错误。

  1. I don't think you need the - in front of the env var like you have there. 我不认为你需要 - 就像你在那里的env var一样。
  2. You don't have the proper indentation for your env var. 您的env var没有正确的缩进。 It looks like it's the same indentation as "environment". 看起来它与“环境”的缩进相同。 Bump it in. 把它搞砸了。
  3. Those may be irrelevant because I don't think ARG and environment variables are synonymous. 这些可能无关紧要,因为我不认为ARG和环境变量是同义词。 So the OL declaration you're making in the yml doesn't apply to the ARG declaration in the Dockerfile or your usage of it in the Python. 因此,您在yml中创建的OL声明不适用于Dockerfile中的ARG声明或您在Python中的使用。 So your problem might lie there but again I don't know much about Python, just Docker. 所以你的问题可能就在那里,但我再也不太了解Python,只有Docker。

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

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