1.拉去镜像

docker pull hub.c.163.com/library/jenkins:latest

 2.创建jenkins本地存储空间

mkdir -p  /opt/kenkins-data

3.修改目录用户uid

需要修改下目录权限, 因为当映射本地数据卷时,/opt/jenkins-data目录的拥有者为root用户,而容器中jenkins user的uid为1000

docker run -ti --rm --entrypoint="/bin/bash" hub.c.163.com/library/jenkins  -c "whoami && id" -c "whoami && id"
jenkins
uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins)
so:
sudo chown -R 1000:1000 /opt/jenkins-data

不修改会显示 :

touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

4.实例镜像

docker run --name jkens -d  -p 8080:8080 -p 50000:50000 -v /opt/jenkins-data:/var/jenkins_home hub.c.163.com/library/jenkins

 5.docker重启默认自动拉起jkens

docker update --restart=always <CONTAINER ID>

docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS                       PORTS               NAMES
7544eaa08c25        hub.c.163.com/library/jenkins   "/bin/tini -- /usr/l…"   11 minutes ago      Exited (143) 4 minutes ago                       jkens
c7c86b92d9d2        hub.c.163.com/library/jenkins   "/bin/tini -- /usr/l…"   12 minutes ago      Exited (1) 12 minutes ago                        cocky_goldstine
[root@localhost docker]# docker update --restart=always 7544eaa08c25
7544eaa08c25

或者起容器时直接
docker run --name jkens -d  -p 8080:8080 -p 50000:50000 -v /opt/jenkins-data:/var/jenkins_home hub.c.163.com/library/jenkins  --restart=always

 6.首次使用时,访问 ip:8080 jenkins web-ui 端需要输入密码即:

 docker 启动时如下输出,键入即可。

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

2f2a452d37ba4b04824bc6af09a1dac3lalal

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

  后续操作 安装插件 创建用户