Problem
You need to create a temporary directory and found the tempfile module that python provides.
The tutorial you followed claimed that something like this works:
import tempfile
with tempfile.TemporaryDirectory() as tmpdirname:
print('Created temporary directory:', tmpdirname)
You try it, try to create a file in that folder and get an error telling you that the folder does not exist.
Solution
Replace it with this:
import tempfile
tmp_dir_name = tempfile.mkdtemp()
print('Created temporary directory:', tmp_dir_name)
It is less elegant, but it works (as long as you have the file permissions right)
Explanation
To be honest I have no explanation so far why it happens in a Docker environment. My theory is that there is somewhat of an issue with Python and Docker, where Docker doesn´t fill the requirement to Pythons expectation.
Nonetheless I was search quite some time for a working Solution and wanted to have it here for your ease.
Note
I use this solution for a production system on an ecommerce style website with the Saleor Framework.
It´s used to clone a repository which contains the sitemap.xml, generates the new one, commits it and pushes it to the GitHub repo. From there a GitHub Actions is deploying it into the running container with the website (here).
Let me know when it helped you.
Best,
Frank
Sources:
https://www.tutorialspoint.com/generate-temporary-files-and-directories-using-python