Change the owner of a file in a running Docker con

2019-09-16 01:05发布

问题:

I am running Docker in Windows (not the native version as I don't have Windows Pro).

I am running a image with an attached volume like so using the Docker Quickstart terminal (MinGW64) (I also used Windows Command Prompt):

docker run --rm -it -v /c/Users/yahya/example:/host-shared debian bash

When I run ls -l on the /host-shared directory I get this:

total 0
drwxrwxrwx 1 1000 staff 0 Aug  6 20:38 hi

However when I try to change the owner of this file like so:

chown www-data:www-data hi 

and the run ls -l again; the owner has not changed!

I believe this may be a Windows only problem.

回答1:

I just tested it (on Linux though) and it actually works. It may not work as you expect or need it, but it does work.

The files in the container belongs to the user www-data:www-data, as for the host, it belongs to the user 1:33.

If the host and container users do not match then it is because the UID and GID differs between the container and the host. The solution is to create a user using useradd --uid $HOST_USER_UID --gid $HOST_USER_GID $USER_NAME, then you're set!


Update

Here is the issued commands:

root@5665d3b794a2:/# ls -lh host-shared/aklm/
total 0
-rw-rw-r--. 1 www-data www-data 0 Aug  6 07:03 aze
-rw-rw-r--. 1 www-data www-data 0 Aug  6 07:03 azer
root@5665d3b794a2:/# chown -R root:root /host-shared/
root@5665d3b794a2:/# ls -lh host-shared/aklm/
total 0
-rw-rw-r--. 1 root root 0 Aug  6 07:03 aze
-rw-rw-r--. 1 root root 0 Aug  6 07:03 azer

Docker Server Version: 1.11.2



标签: docker