0

I have a Python script which needs to be run inside a Docker container (because that's how a piece of software needed is installed). This script creates some files that are needed for later processing.

When I run in the Command Prompt

docker exec -it CONTAINER bash
python3 script.py

the script does generate the files. However, I tried to use a batch file to automate the process:

docker exec -it CONTAINER python3 script.py

The script does run, since it prints information in the Command Prompt, but it does not create the files I need. What am I doing wrong?

  • 1
    perhaps you can share some/all of `script.py`? i'd be particularly interested in anything pertaining to file path formation. e.g. maybe your files are getting created, but not where you think? – ghatzhat Oct 05 '22 at 17:05
  • You shouldn't usually use `docker exec` to install software, since that will be lost as soon as the container exits. Can you `RUN ./script.py` in a Dockerfile instead? How are you verifying that the output is or isn't present? – David Maze Oct 05 '22 at 17:10
  • @ghatzhat You actually made me think twice about where it was saving files. I had assumed that the script would save where the file was (which was actually another folder). Turns out it was saving in the root instead. – AspiringMathematician Oct 05 '22 at 17:15
  • 1
    If a script file like `script.py` uses just file names without path on opening files for read/write operations, it references the files in the current directory. Which directory is the current directory is defined by the process starting an executable like `python3.exe`. So it would be a good idea to improve `script.py` by [getting the file path of the script file](https://stackoverflow.com/questions/3430372/) and referencing all files opened for read/write with that path to work independent on which directory is the current directory on processing the Python script. – Mofi Oct 05 '22 at 17:43
  • @Mofi Thanks to your comment I figured a good enough workaround. Since I'd prefer the files to be in the same folder as the script, I just changed the working directory when using ```docker exec``` – AspiringMathematician Oct 05 '22 at 18:02

1 Answers1

0

So thanks to the comments from ghatzhat and Mofi I figured the issue: the script was saving files in the root folder in the container. This was because I hadn't specified the file path in script.py.

Since I'd like the files to be in the same folder as the script no matter where it is, and changing the path inside the script would take some work, I instead changed the working directory in the batch file by using

docker exec -w FOLDER_SCRIPT CONTAINER python3 script.py

where FOLDER_SCRIPT is where the script is located.

Honestly, it seems obvious in hindsight, but guess that's part of learning curve. Thanks again to ghatzhat and Mofi for their helpful comments!