I am creating a wrapper script to execute my python programs. The logic is like:
This is my wrapper script:
import os
import sys
import argparse
parse = argparse.ArgumentParser()
parse.add_argument('command', help="give datacenter name")
parse.add_argument('args', nargs=argparse.REMAINDER)
parse_arguments = parse.parse_args()
'''
Co-relate to the command and corresponding scripts to trigger
'''
scripts = {
'verify' : '/path/verify.py'
}
if __name__ == '__main__':
if parse_arguments.command not in scripts:
print('These are the available scripts to run:')
print('\n'.join(sorted(scripts.keys())))
else:
os.execv(scripts.get(parse_arguments.command),
[scripts.get(parse_arguments.command)] + parse_arguments.args) .
I am running this like,
$ docker run -it --rm --net host run-script verify --listenv tpc1
Traceback (most recent call last):
File "/path/runme.py", line 28, in <module>
os.execv(scripts.get(parse_arguments.command),
[scripts.get(parse_arguments.command)] + parse_arguments.args)
FileNotFoundError: [Errno 2] No such file or directory
My Docker image is run-script
If I run the same code from my local machine, it does work. but inside container it shows this file not find error.
Can anyone help me on this?
The first argument passed to sys.argv is the path to the running file, here /path/runme.py. The file is found when run from your local machine, but probably not in the docker container (I'm not sure why).
Try to set the prog parameter when instanciating your ArgParser, something like:
parse = argparse.ArgumentParser(prog="run_me.py")
I managed to solve this issue. The issue was mainly with the shebang that I have provided. So when I was executing the script in the docker container, it was checking to execute code from shebang location.
I would say, that is a mistake from my side, FileNotFoundError was not giving me a clue to shebang. Finally, figured it out.
@olinox14 - Thanks for your update, "prog" parameter in argparse helped me to output a better help message with the script name in docker container, rather than the full path inside docker container.
os.execv(program, args) by default doesn't search for program (first argument) based on PATH envrionment variable. os.execvp does.
From os.execv documentation :
os.execv(path, args)The variants which include a “p” near the end (execlp(), execlpe(), execvp(), and execvpe()) will use the PATH environment variable to locate the program file.
So, either user os.execvp or provide full path to program to be run to os.execv.