docker使用jenkins修改挂载目录
第一次用docker创建jenkins的时候,不熟,对命令不太了解。摸索了下,创建了一个jenkins容器,用的以下参数。
docker run -d -p 8080:8080 -p 50000:50000 \ -v jenkins:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /etc/localtime:/etc/localtime \ -v /usr/local/maven/apache-maven-3.6.3:/usr/local/maven \ -v /usr/local/gradle/gradle-6.5.1:/usr/local/gradle \ --name jenkins docker.io/jenkins/jenkins
后来没找到/var/jenkins_home目录在宿主机的挂载目录,几经摸索,发现在路径下面/var/lib/docker/volumes/jenkins/_data目录下面。
想把它修改到自定义目录/usr/local/jenkins目录下面,首先找到配置文件目录:
/var/lib/docker/containers/[hash_of_the_container]/config.v2.json
/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json
是支持的json格式,可以格式化一下方便查看,两个文件依次如下修改:
"/var/jenkins_home": { "Source": "/usr/local/jenkins", "Destination": "/var/jenkins_home", "RW": true, "Name": "", "Driver": "", "Type": "bind", "Propagation": "rprivate", "Spec": { "Type": "bind", "Source": "/usr/local/jenkins", "Target": "/var/jenkins_home" },
"Binds": [ "/usr/local/jenkins:/var/jenkins_home", "/var/run/docker.sock:/var/run/docker.sock", "/etc/localtime:/etc/localtime", "/usr/local/maven/apache-maven-3.6.3:/usr/local/maven", "/usr/local/gradle/gradle-6.5.1:/usr/local/gradle" ],
容器没停掉以前是替换重启不成功的,首先docker stop jenkins,然后docker start jenkins,发现报错。
Error response from daemon: error evaluating symlinks from mount source "/var/lib/docker/volumes/jenkins/_data": lstat /var/lib/docker/volumes/jenkins: no such file or directory
于是停掉docker服务,systemctl stop docker,再替换文件,再systemctl start docker,docker start jenkins,这时候不报错了。
这时候发现不报错了,但docker ps仍然没有启动的容器,想到原目录下面是jenkins/_data目录是1000用户属性,于是修改下当前路径的用户属性。
chown -R 1000 /usr/local/jenkins/
可以正常启动了,以前的配置还在。
注意:先将jenkins/_data目录内容,拷贝下来放到/usr/lcoal/jenkins目录下面的。
以后直接这样创建了,记录一下。
docker run -d -p 8080:8080 -p 50000:50000 \ -v /usr/local/jenkins:/var/jenkins_home \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /etc/localtime:/etc/localtime \ -v /usr/local/maven/apache-maven-3.6.3:/usr/local/maven \ -v /usr/local/gradle/gradle-6.5.1:/usr/local/gradle \ --name jenkins docker.io/jenkins/jenkins