简体   繁体   中英

Docker "permission denied" in container

I am trying to run a docker image by

docker run  -it -v $PWD/examples:/home/user/examples image 

which should make $PWD/examples in the host accessible in the container. However when I ls in the container, it keeps giving me

ls: cannot access 'examples': Permission denied

I have tried the answers for similar questions, the z/Z option and chcon -Rt svirt_sandbox_file_t /host/path/ and run --privileged , but neither of them have any effect in my case. In fact, the z option appears to work for the first time ls , but when I issue ls the second time it is denied again.

In the comments it turned out that there is probably a USER instruction in the Dockerfile of the image. This user is not allowed to access examples due to file access permissions of examples .


It is possible to supersede USER with docker run option --user .

A quick and dirty solution is to run with --user=root to allow arbitrary access. Be aware that files written as root in container to folder examples will be owned by root .

A better solution is to look for owner of examples , call him foo . Specify its user id and group id to have exactly the same user in container:

docker run --user $(id -u foo):$(id -g foo)  imagename

Another possible solution is to allow arbitray access with chmod 666 examples or chmod 644 examples , but most probably you don't want that.


The best way would be to look at the Dockerfile and check the purpose of USER instruction.

  • If it only serves the purpose of avoiding root in container, the best way is to use --user=foo or more precisely --user=$(id -u foo):$(id -g foo) .
  • If something in Dockerfile/image relies on specific USER , it may be the best to change access permissions of examples .
  • If you have access to the Dockerfile, you may adjust it to fit your host user/the owner of examples .

Try running the container as privileged:

sudo docker run --privileged=true -itd -v /***/***:/***  ubuntu bash

for example: sudo docker run --privileged=true -itd -v /home/willie:/wille ubuntu bash

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM