赞
踩
目前有个docker启动的jenkins机器在运行,该jenkins服务上已经安装了很多需要的插件。现在想写个shell脚本在一台干净的机器上一键安装docker,拉取jenkins镜像并启动容器,但是这个jenkins容器中需要有之前安装过的插件。
直接使用docker commit命令从原先的jenkins容器创建一个新的镜像,在另外的机器上启动这个镜像,发现jenkins服务没有任何插件,还是需要自己安装插件。commit没有生效的原因是因为之前的jenkins容器启动时挂载了宿主机的目录(/var/jenkins_home),安装的插件都在宿主机。
将之前的jenkins容器挂载的目录整个打包,直接启动时解压这个包放到指定的目录,启动jenkins容器时挂载这个目录即可。(后续定期对这个目录进行打包留作备份,防止数据丢失)
cd /var
tar -cvf jenkins_home.tar jenkins_home
这里的操作环境是内网,所以docker是离线安装,内网搭建的harbor私服(配置了HTTPS访问),配置了DNS。
脚本名:install.sh
同级目录下的文件:docker-18.06.1-ce.tgz、ca.crt、jenkins_home.tar
#!/bin/bash #安装docker tar -xvf docker-18.06.1-ce.tgz #把解压的docker文件移到/usr/bin目录下 cp docker/* /usr/bin #删除解压的目录 rm -rf docker #docker配置文件 if [ ! -f /etc/systemd/system/docker.service ]; then touch /etc/systemd/system.docker.service echo "create file:/etc/systemd/system/docker.service succeed!" fi #写入配置 cat>/etc/systemd/system/docker.service<<EOF [Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network-online.target firewalld.service Wants=network-online.target [Service] Type=notify # the default is not to use systemd for cgroups because the delegate issues still # exists and systemd currently does not support the cgroup feature set required # for containers run by docker ExecStart=/usr/bin/dockerd --registry-mirror=https://yourdomain.com -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock ExecReload=/bin/kill -s HUP $MAINPID # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Uncomment TasksMax if your systemd version supports it. # Only systemd 226 and above support this version. #TasksMax=infinity TimeoutStartSec=0 # set delegate yes so that systemd does not reset the cgroups of docker containers Delegate=yes # kill only the docker process, not all processes in the cgroup KillMode=process # restart the docker process if it exits prematurely Restart=on-failure StartLimitBurst=3 StartLimitInterval=60s [Install] WantedBy=multi-user.target EOF #创建目录 if [ ! -d /etc/docker ]; then mkdir /etc/docker echo "create directory:/etc/docker succeed!" fi if [ ! -d /etc/docker/certs.d ]; then mkdir /etc/docker/certs.d echo "create directory:/etc/docker/certs.d succeed!" fi if[ ! -d /etc/docker/certs.d/yourdomain.com ]; then mkdir /etc/docker/certs.d/yourdomain.com echo "create directory:/etc/docker/certs.d/yourdomain.com succeed!" fi #创建daemon.json if [ ! -f /etc/docker/daemon.json ]; then touch /etc/docker/daemon.json echo "create file:/etc/docker/daemon.json succeed!" fi #配置docker工作目录 cat>/etc/docker/daemon.json<<EOF { "data-root":"/root/data/docker" } EOF #拷贝ca.crt证书(登录harbor私服) cp ca.crt /etc/docker/certs.d/yourdomain.com #配置DNS sed -i '$a nameserver IP' /etc/resolv.conf #启动docker systemctl daemon-reload systemctl restart docker #登录私服 docker login -u admin -p admin yourdomain.com #拉取镜像 docker pull yourdomain.com/xxx/jenkins:2.234 #挂载目录 tar -xvf jenkins_home.tar -C /var #启动jenkins docker run -t -p 8080:8080 -p 50000:50000 -v /var/jenkins_home:/var/jenkins_home yourdomain.com/xxx/jenkins:2.234
#赋权
chmod +x install.sh
#执行脚本
./install.sh
执行脚本看到jenkins启动后就可以进入jenkins的页面了,http://IP:8080进入jenkins页面,由于挂载的是之前的整个目录,所以用户信息也能直接使用,登录进入后可以看到插件都是已安装过的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。