0

[Solved] I found the solution, unrelated to the linked question. I reposted it here with the answer.

I have a Python (3.9) script that exists in the following file structure:

L usr
  L src
    L project_root
      L folder_a
        L folder_b
          L foo.py
          L bar.sh

foo.py contains this line of code, which calls a subprocess bar.sh and passes some data to it:

subprocess.call(['/usr/src/folder_a/folder_b/bar.sh', f'{some_data}'])

This subprocess call finds the shell script just fine when run locally.

However, when I run this same code in a docker container, I get the following exception:

Exception: [Errno 2] No such file or directory: './folder_a/folder_b/bar.sh'

Troubleshooting steps I have tried:

  1. I confirmed that the path is correct, both spelling and location
  2. The docker container's top-level is the project_root folder
  3. I have tried both the relative and exact paths, neither work
  4. I have SSH'd into the container and checked the files, the shell script is present in the exact directory that I provided.
  5. I have tried using os.path.abspath() to generate the absolute path to the shell script, but it was still not found.
  6. I have checked os.cwd to confirm that the current working directory is usr/src
  7. I have used Path(__file__).parent('./bar.sh') to find the absolute path to the shell script, which just gave me the string /usr/src/folder_a/folder_b/bar.sh, the same as I've been using.

Any ideas?

  • 1
    Can you use `os.getcwd()` to see the actual working directory? If it's not `project_root`, then you know the bug. Also, maybe instead computing the path yourself, you can find the path to the current python file and use the relative path to the bash script? See https://stackoverflow.com/questions/3718657/how-do-you-properly-determine-the-current-script-directory – omajid Oct 01 '21 at 16:42
  • 1
    Might also be a good idea to run `find` from within the container to see the directory is what you expect it to be. – omajid Oct 01 '21 at 16:43
  • The problem is that you have a different current working directory on docker. I've linked a question whose accepted answer shows how to use pathlib and `__file__` to get your script's directory. Knowing where foo.py is, tells you where the shell script is. – tdelaney Oct 01 '21 at 16:45
  • @tdelaney I don't see the link you mention in your comment. – RufusVS Oct 01 '21 at 16:58
  • Its in that top blue area that says _This question already has answers here:_ I added the comment to give some context to the linked question (and so you could argue if its not right!). – tdelaney Oct 01 '21 at 17:04
  • @omajid I checked `os.cwd()` and the directory is different! The root directory is two levels higher in docker: `usr/src/...` but the issue is still here for whatever reason... I hard-coded *and* generated (with `os.path.abspath()` the correct absolute path to the shell script, but still got the same error saying the file is not found. I'll edit the original to reflect this. – Cameron Gould Oct 01 '21 at 17:41
  • @tdelaney so I used the linked question's solution and the path that I got *was* slightly different: `'/usr/src/folder_a/folder_b/bar.sh'` but this also raises the file not found exception... I'll keep searching. – Cameron Gould Oct 01 '21 at 17:48
  • @tdelaney @omajid I found the solution in the `subprocess` documentation, reposted the question and answered it [here](https://stackoverflow.com/questions/69410841/python-subprocess-call-cannot-find-shell-script-file-location-in-docker-image/69410842#69410842) – Cameron Gould Oct 01 '21 at 19:01
  • @CameronGould - Its not the difference between `call` and `run`. You edited the question to change from a relative path to an absolute path. But that's a risky proposition. Now your script can only run from one place. – tdelaney Oct 01 '21 at 19:08
  • @tdelaney the key difference was the inclusion of the kwarg. I ran the code with `shell=True` and it worked. This would work with `subprocess.call()` as well, but `subprocess.run()` is the more appropriate method to use with Python >3.5 according to docs. I tried with absolute path and it didn't work – Cameron Gould Oct 01 '21 at 20:08

0 Answers0