Error while running zookeeper to use a docker host volume "cp: cannot create regular file ‘/zookeeper/conf/configuration.xsl’: Permission denied"

When I am try to mount host volume to docker zookeeper container using –volume options, it gives me permission denied error cp: cannot create regular file ‘/zookeeper/conf/configuration.xsl’: Permission denied.

When I use –volume it created directories automatically in case it does not exists with root:root user and group. As per different stackoverflow posts I tried to give permission and ownership to current user and group to mounted directories.

If I remove mount and let docker use internal volume then zoopker is started successfully.

Docker version

[debezium-sprintreview]$ docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:25:41 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:24:18 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

I have also tried creating directories that zookeeper needs with correct permissions.

[[email protected]@host u01]$
sudo mkdir -p /u01/zookeeper-data
sudo mkdir -p /u01/kafka-data/

[[email protected]@host u01]$
sudo chown -R [email protected] /u01/zookeeper-data
sudo chown -R [email protected] /u01/kafka-data
sudo chmod g+w /u01/zookeeper-data
sudo chmod g+w /u01/kafka-data

[[email protected]@host u01]$ ls -lrt
drwxrwxr-x 6 [email protected] root 54 Jan  7 00:08 zookeeper-data
drwxrwxr-x 6 [email protected] root 57 Jan  7 00:08 kafka-data

Error

[[email protected]@host u01]$ docker network create -d bridge my-network

[[email protected]@host u01]$ docker run -it --rm --name zookeeper 
> -p 2181:2181 -p 2888:2888 -p 3888:3888 
> --network=my-network 
> --volume /u01/zookeeper-data/conf:/zookeeper/conf 
> --volume /u01/zookeeper-data/data:/zookeeper/data 
> --volume /u01/zookeeper-data/txns:/zookeeper/txns 
> debezium/zookeeper:0.10
cp: cannot create regular file '/zookeeper/conf/configuration.xsl': Permission denied
cp: cannot create regular file '/zookeeper/conf/log4j.properties': Permission denied
cp: cannot create regular file '/zookeeper/conf/zoo.cfg': Permission denied
cp: cannot create regular file '/zookeeper/conf/zoo_sample.cfg': Permission denied

I also looked at possible locations these files might already exists to understand whether it is mount directory or docker container home permissions that could cause problems.

[[email protected]@host u01]$ sudo find / -name "configuration.xsl"
/var/lib/docker/overlay2/17525d8f7df11fbffeb129bf011965cf6256b4406b572f5833f12c9f4b381cba/diff/zookeeper/conf/configuration.xsl
/var/lib/docker/overlay2/0ea52937e833bc822261526d94354ebaa3cee3b89e5bcbfbc25e6391b60ef6df/diff/zookeeper/conf/configuration.xsl
/var/lib/docker/overlay2/bee93711df0d017bdd4ac0fe356d04a0e282cd821def323f4fd2858a77f2a717/diff/zookeeper/conf/configuration.xsl
/var/lib/docker/overlay2/bee93711df0d017bdd4ac0fe356d04a0e282cd821def323f4fd2858a77f2a717/diff/zookeeper/conf.orig/configuration.xsl

[[email protected]@host u01]$ sudo ls -lrt /var/lib/docker/overlay2/17525d8f7df11fbffeb129bf011965cf6256b4406b572f5833f12c9f4b381cba/diff/zookeeper/conf/
total 12
-rw-r--r-- 1 1001 1001  922 Mar  6  2019 zoo_sample.cfg
-rw-r--r-- 1 1001 1001 2161 Mar  6  2019 log4j.properties
-rw-r--r-- 1 1001 1001  535 Mar  6  2019 configuration.xsl

Source: StackOverflow