Can docker cp set user id and group?

When I use the docker cp command to copy a file to a running docker container the user id and group for the new file are set to root:root. Is there a way to copy and set the user id and group to some user, i.e. steve:steve using the cp command?

Currently I am attaching to the container and running chown on the new file but I would like to eliminate this step if possible. I tried:

docker cp .bashrc steve@hopeful_brahmagupta:/home/steve/

1 Like

" Ownership is set to the user and primary group at the destination. For example, files copied to a container are created with UID:GID of the root user. Files copied to the local machine are created with the UID:GID of the user which invoked the docker cp command."

@steveflenniken I notice that there was no solution posted to this thread on the docker cp ownership problem. Did you get resolution to this issue?

As @dvohra points out, the documentation clearly states that the files copied into the container, which in my case is a volume, should be owned by root but the directory and files instead take the UID/GID of the host user.

My case is I am running multiple containers controlled by docker-compose and one of those containers is based on the official nginx image where I will update the nginx conf files from time to time.

I copy a local directory conf.d on the host owned by a non-root user to the conf.d directory in the container.

Before the copy:
root@4f1a6eb0d0a9:/# ls -l /etc/nginx/
total 48
drwxr-xr-x 2 root root 4096 Mar 16 22:49 ca
drwxr-xr-x 2 root root 4096 Mar 16 22:49 certs-letsencrypt
drwxr-xr-x 2 root root 4096 Mar 18 14:36 conf.d
-rw-r–r-- 1 root root 1007 Mar 9 15:27 fastcgi_params
-rw-r–r-- 1 root root 2837 Mar 9 15:27 koi-utf
-rw-r–r-- 1 root root 2223 Mar 9 15:27 koi-win

Copy from host
$ docker cp nginx/conf.d devneutronicsca_nginx_1:/etc/nginx

After Copy conf.d UID/GID is 1000:1000, a normal user and the file mode also takes on the host file mode.
root@4f1a6eb0d0a9:/# ls -l /etc/nginx/
total 48
drwxr-xr-x 2 root root 4096 Mar 16 22:49 ca
drwxr-xr-x 2 root root 4096 Mar 16 22:49 certs-letsencrypt
drwxrwxr-x 2 1000 1000 4096 Mar 18 14:23 conf.d
-rw-r–r-- 1 root root 1007 Mar 9 15:27 fastcgi_params
-rw-r–r-- 1 root root 2837 Mar 9 15:27 koi-utf
-rw-r–r-- 1 root root 2223 Mar 9 15:27 koi-win

And the files copied over also have the UID/GID 1000/1000
root@4f1a6eb0d0a9:/# ls -l /etc/nginx/conf.d/
total 20
-rw-rw-r-- 1 1000 1000 913 Mar 18 13:53 app_4sites.conf
-rw-rw-r-- 1 1000 1000 919 Mar 18 13:51 app_discourse.conf
-rw-r–r-- 1 1000 1000 1234 Mar 18 13:45 app_nodebb.conf
-rw-rw-r-- 1 1000 1000 181 Mar 18 13:27 default.conf
-rw-r–r-- 1 1000 1000 159 Mar 18 13:30 domains80_redirect_ssl.conf