当我将docker或docker-compose与卷配合使用时,我经常会遇到权限问题,因为主机上不知道容器用户:

mkdir i-can-to-what-i-want
rmdir i-can-to-what-i-want
docker run -v$(pwd):/home -w/home ubuntu touch you-shall-not-delete-it
$ ls -al you-shall-not-delete-it
-rw-r--r--  2 root   root   0 2020-08-08 00:11 you-shall-not-delete-it
一种解决方案是始终执行此操作:
UID=$(id -u) GID=$(id -g) docker-compose up
要么
UID=$(id -u) GID=$(id -g) docker run ...
但是...很麻烦...
还有其他方法吗?

最佳答案

--user将完成此工作,除非这是您要避免的确切麻烦的解决方案:

who
neo      tty7         2020-08-08 04:46 (:0)

docker run --user $UID:$GID -v$(pwd):/home -w/home ubuntu touch you-shall-delete-it

ls -la
total 12
drwxr-xr-x  3 neo neo 4096 Aug  8 02:12 .
drwxr-xr-x 34 neo neo 4096 Aug  8 02:03 ..
drwxr-xr-x  2 neo neo 4096 Aug  8 02:03 i-can-to-what-i-want
-rw-r--r--  1 neo neo    0 Aug  8 02:12 you-shall-delete-it

08-28 09:45