permission denied creating directories as non-root in docker

Viewed 856

I have a script which must be run as a non-root. This script also creates directories and files. If I run the script as a non-root user in docker, I get a permission denied error creating dirs. I tried to chmod the parent directory the script is in but it doesn't work. What should be the best practice here?

RUN mkdir test
WORKDIR /test
USER testuser
RUN mkdir .cache     <--- permission denied
1 Answers

That happens because your /test directory was created by root, and by default won't allow any other users to create anything in it. To change ownership to the user you want, you can use chown before your USER testuser step:

RUN chown testuser /test

If there are already files inside the directory, you will need to pass the -R flag to change the permission recursively:

RUN chown -R testuser /test

Another option would be giving the directory red+write+execute permissions for all users. However, this is probably NOT what you want, the above should serve you well for almost all cases.

RUN chmod 777 /test
Related