前两篇中,介绍了如何搭建个人私有镜像仓库Harbor,并编写了一个goweb项目进行打包上传,本文则继续介绍如何利用jenkins 实现自动打包镜像并上传。
mkdir jenkins
cd jenkins
vim vim docker-compose.yml
version: "3.1"
services:
jenkins:
image: jenkins/jenkins
container_name: jenkins
ports:
- 8080:8080
- 50000:50000
volumes:
- ./data/:/var/jenkins_home/
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
- /etc/docker/daemon.json:/etc/docker/daemon.json
执行 docker-compose up -d
发现jenkins并没有启动并且执行docker logs输出如下报错:
docker logs fa9f80b44831
------
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?
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?
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?
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?
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?
看了下主要是因为data文件没有权限的原因,我直接777大法:
chmod 777 ./data
重新执行docker-compose up -d
访问ip:8080 , 发现可以启动了。不上技术手段是不行的。
最后,选择默认插件->创建账户。
话说回来,aliyun 2c2g,想玩云原生不太行。
我直接一个重启大法,最终在阿里云控制台进行了重启操作后,恢复正常。
docker exec -it jenkins /bin/bash
进入jenkins容器后执行:
ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/var/jenkins_home/.ssh/id_rsa):
/var/jenkins_home/.ssh/id_rsa already exists.
Overwrite (y/n)?
jenkins@fa9f80b44831:/$
一路回车,由于我的生成过,生成的rsa路径为:/var/jenkins_home/.ssh
cat /var/jenkins_home/.ssh/id_rsa.pub
将值复制出来。
右上角头像->设置->SSH公钥,将刚才复制出来的值添加上去。
echo "1. 制作服务镜像"
docker build -t goweb .
echo "2. 登录 harbor 镜像仓库"
docker login -u admin -p Harbor12345 47.xx.xx.171
echo "3. 推送镜像至 harbor 仓库"
docker tag goweb:latest 47.xx.xx.171/library/goweb
docker push 47.94.191.171/library/goweb
如果遇到以下报错:
原因:Docker是在root用户下部署的,所以jenkins 没有权限使用Docker
解决:将jenkins加入docker用户组 或者 :直接执行
sudo chmod 666 /var/run/docker.sock
点击立即构建
构建成功后,查看Harbor
成功上传!