'why docker cp command change /etc/hosts /etc/hostname /etc/resolv.conf owner

My hosts file owner is admin in docker container, after docker cp command to copy a file into container from physics machine, it's owner has been changed to root.

I want to know why docker do this.



Solution 1:[1]

As suggested by Ganesh,

files copied to a container are created with UID:GID of the root user

Source: Official docker cp documentation

This is because the Docker daemon (actually performing the commands you run) runs as root by default.

If you are simply running commands from your terminal, you can run

docker exec [container] chown [user] [file]

to change the permissions of the file after you copied it.

If you are copying the file from within a Dockerfile, you can add the --chown option:

COPY -chown=admin source-in-host destination-in-container

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Isra