简体   繁体   中英

Permission denied when get contents generated by a docker container on the local fileystem

I use the following command to run a container:

  docker run -it -v /home/:/usr/ ubuntu64 /bin/bash

Then I run a program in the container, the program generates some files in the folder:/usr/ which also appear in /home/ but I can't access the generated files with an error: Permission denied outside the container.

I think this may because the files generated by root in the container but outside the container, the user have no root authority, but how to solve it?

What I want to do is accessing the files generated by the program(installed in the container) outside the container.

You need to use the -u flag

docker run -it -v $PWD:/data -w /data alpine touch nouser.txt

docker run -u `id -u` -it -v $PWD:/data -w /data alpine touch onlyuser.txt

docker run -u `id -u`:`id -g` -it -v $PWD:/data -w /data alpine touch usergroup.txt

Now if you do ls -alh on the host system

$ ls -alh
total 8.0K
drwxrwxr-x  2 vagrant vagrant 4.0K Sep  9 05:22 .
drwxrwxr-x 30 vagrant vagrant 4.0K Sep  9 05:19 ..
-rw-r--r--  1 root    root       0 Sep  9 05:21 nouser.txt
-rw-r--r--  1 vagrant root       0 Sep  9 05:21 onlyuser.txt
-rw-r--r--  1 vagrant vagrant    0 Sep  9 05:22 usergroup.txt

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