目标:
1.通过Dockerfile创建新的image
2.通过image创建容器去运行jenkins
3.启动nginx 容器挂载hexo blog
4.通过jenkins同时部署blog到github和nginx
5.push 新创建的image到docker hub
步骤:
1.创建一个目录dockerhexo
2.创建一个新目录名为Docker_file,创建Dockerfile:
1 | jenkins稳定版本 |
2.运行 docker build生成image:
1 | docker build -t zxlman/jenkinshexo . |
3.查看新生成的image:
4.创建一个shellscript用来删除对应的容器,方便测试,容器名jenkinscontainerremove.sh
1 | echo "Stopping container myjenkins" |
执行 chmod +x enkinscontainerremove.sh
赋予执行权限,如果执行这个脚本就运行sh jenkinscontainerremove.sh
5.创建一个shell脚本启动jenkins容器,如下,`sh jenkinscontainerstartup.sh
1 | docker container run --name myjenkins \ |
ps:
1 | -v /home/kevin/.ssh:/var/jenkins_home/.ssh \ #用来访问github拉取repository |
6.创建一个shell脚本启动nginx容器,如下,`sh nginxstartup.sh
1 | docker container stop mynginx |
7.打开jenkins并且创建pipline job:job Blog_Deployment_Pipline
8.配置jenkins job:
1 | pipeline { |
7.Trigger job,但是发现报了如下的错误:
那是因为github上clone下来的代码缺少hexo相关的文件,执行如下脚本:
1 | cd /home/kevin/dockerhexo/jenkins_home/workspace/Blog_Deployment_Pipline/ |
重新trigger这个job
8.查看blog是否部署到github page和nginx web服务器,可以看到通过如下网址均可以访问blog,并且部署成功了
http://kevin.southeastasia.cloudapp.azure.com:81/
https://nightwish2016.github.io/
9.push这个image 到docker hub docker push zxlman/jenkinshexo
https://hub.docker.com/repository/docker/zxlman/jenkinshexo
Tips:
Start containers automatically:
使用root用户进入jenkins:
docker exec -it –user root 69d /bin/bash
window 10 启动jenkins容器:
1 | docker container run --name jenkinshexocontainer --restart unless-stopped -v /c/Users/xxx/jenkins_home2:/var/jenkins_home -v /c/Users/xxx/.ssh:/var/jenkins_home/.ssh -v /c/Users/xxx/.gitconfig:/var/jenkins_home/.gitconfig -d -p 8083:8080 jenkinshexo |
Issues:
1 | docker container run --name myjenkins --restart unless-stopped -v /home/kevin/jenkins_home:/var/jenkins_home -v /home/kevin/.ssh:/var/jenkins_home/.ssh -p 82:8080 zxlman/jenkinshexo |
Solution:
jenkins的默认用户没有对/home/kevin/dockerhexo/jenkins_home的写权限
sudo chown 1000 /home/kevin/dockerhexo/jenkins_home