I also had this problem and I will use different directory paths to avoid ambigiouty in this answer.
E.g. -v /var/host/jenkins_home:/var/jenkins_home
First I would like to reproduce the error and create the directory on my host with sudo -u root mkdir -p /var/host/jenkins_home
. Since the directory is created by root
only root has permission to access it.
$ ls -al /var/host/jenkins_home/
total 8
drwxr-xr-x 2 root root 4096 Jul 27 03:54 .
drwxr-xr-x 3 root root 4096 Jul 27 03:54 ..
When I start jenkins now I will get the same error like you
$ docker run -p 8080:8080 -p 50000:50000 -v /var/host/jenkins_home:/var/jenkins_home jenkins/jenkins:lts
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
To fix this problem you must change the permissions on the host filesystem so that uid 1000 and gid 1000 has access to /var/host/jenkins_home
.
sudo chown -R 1000:1000 /var/host/jenkins_home/
If I start jenkins now it will work:
$ docker run -p 8080:8080 -p 50000:50000 -v /var/host/jenkins_home:/var/jenkins_home jenkins/jenkins:lts
Running from: /usr/share/jenkins/jenkins.war webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
2020-07-27 03:51:36.430+0000 [id=1] INFO org.eclipse.jetty.util.log.Log#initialized: Logging initialized @441ms to org.eclipse.jetty.util.log.JavaUtilLog
2020-07-27 03:51:36.577+0000 [id=1] INFO winstone.Logger#logInternal: Beginning extraction from war file
When working with docker you should think in uid and gid and not in usernames, because they can differ and lead to confusion.
E.g. on my host machine the uid 1000
is my user rene
$ id -un 1000
rene
But in the container it is jenkins
:
$ docker exec <CONTAINER_NAME> id -un 1000
jenkins
EDIT
I still get same error
Check the permissions in the container
docker run --rm -v /var/host/jenkins_home:/var/jenkins_home jenkins/jenkins:lts ls -al /var/jenkins_home
it should show you that jenkins
is the owner and group of /var/jenkins_home
total 12
drwxr-xr-x 2 jenkins jenkins 4096 Jul 27 04:56 .
drwxr-xr-x 1 root root 4096 Jul 15 14:56 ..
-rw-r--r-- 1 jenkins jenkins 100 Jul 27 04:56 copy_reference_file.log
EDIT
Yes. I am running docker inside a VM, right. And the VM host is also a linux. I am new to dockers, so did not understand much of what you said here. Can you please elaborate.
So you have the following setup:
+-------------------------------------------------------------------------------+
+ VM Host |
+-------------------------------------------------------------------------------+
| |
| +-------------------------------------------------+ |
| / | VM (Docker Host) | |
| +- var +-------------------------------------------------+ |
| +- ... | / +-----------------------+| |
| | +- var | container jenkins || |
| | +- host +-----------------------+| |
| | + -jenkins_home |/ || |
| | / |+- var || |
| | | +- jenkins_home || |
| | +-----------------------+| |
| +-------------------------------------------------+ |
+-------------------------------------------------------------------------------+
Please ensure that you run the commands on the docker host (the VM). Keep in mind that the docker host file system is different from your local (VM Host).