why do I need permission to write to a txt file? How do I solve this problem
main.py
text_file = open("sample.txt", "w")
n = text_file.write('some words dzfsadfadfs')
text_file.close()
Dockerfile
FROM rayproject/ray
ADD main.py .
CMD ["python", "main.py"]
error
Traceback (most recent call last):
File "main.py", line 3, in <module>
text_file = open("sample.txt", "w")
PermissionError: [Errno 13] Permission denied: 'sample.txt'
The problem is that the base image you're using ( rayproject/ray
) is configured to run as a non-root user ( ray
), but sets the working directory to /
. There is a writeable directory /home/ray
available; the easiest solution is probably adding an appropriateWORKDIR
directive to your Dockerfile:
FROM rayproject/ray
WORKDIR /home/ray
ADD main.py .
CMD ["python", "main.py"]
Update
I figured this out by first spawning a shell and checking what uid the process was running as:
$ docker run -it --rm testimage bash
(base) ray@bca6b7788820:~$ id
uid=1000(ray) gid=100(users) groups=100(users),27(sudo)
I checked the passwd
file for information about that user:
(base) ray@bca6b7788820:~$ grep ray /etc/passwd
ray:x:1000:100::/home/ray:/bin/bash
And verified that directory was writeable:
(base) ray@bca6b7788820:~$ cd /home/ray
(base) ray@bca6b7788820:~$ touch foo
In general you can also figure things like this out by inspecting the corresponding Dockerfile, but for this particular base image that ended up being a bit of a pain because you need to track down a chain of dependencies:
rayproject/ray
, and the Dockerfile is here .rayproject/ray-deps
rayproject/base-deps
And if you look at that last Dockerfile, you find:
USER $RAY_UID
ENV HOME=/home/ray
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.