--- tags: Jenkins --- # Jenkins + Docker 從頭開始 # https://www.jenkins.io/doc/book/installing/docker/ ## DockerFile - 使用最新image安裝docker command - docker build -t jenkins_docker . ``` FROM jenkins/jenkins:latest USER root RUN apt-get update && apt-get install -y apt-transport-https \ ca-certificates curl gnupg2 \ software-properties-common RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - RUN apt-key fingerprint 0EBFCD88 RUN add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" RUN apt-get update && apt-get install -y docker-ce-cli USER jenkins #RUN jenkins-plugin-cli --plugins "blueocean:1.24.7 docker-workflow:1.26" ``` ## 在jenkins管理->新增節點配置 docker agent安裝 ------- 名稱 docker_custom 描述 docker-2 執行程式數量 1 遠端檔案系統根目錄 /home/jenkins/agent 標籤 docker 使用方式 盡可能使用這個節點 啟動模式 Launch agent via execution of command on the master 啟動指令 ``` docker run -i --rm --name agent --init jenkins/slave java -jar /usr/share/jenkins/agent.jar ``` ``` docker run --rm -i --group-add 995 -v /var/run/docker.sock:/var/run/docker.sock --init jenkins/slave java -jar /usr/share/jenkins/agent.jar ``` ------------- ## 製作含有docker的jenkins/slave images 進入此jenkins docker容器 ``` 以root進入(-u 0:0),預設為jenkins,會無法安裝套件 docker exec -u 0:0 -ti 25fc9cf4c41 bash apt-get update apt-get install docker.ce (本人使用docker.io,但只要命令行好像只需要docker.ce,commit image會更小更輕便) ``` 出容器把新image commit ``` docker commit -m "add docker i" f38ada76b548 jenkins/custom_agent:0.01 f38ada76b548 = local agent containerID ``` ## 以新image來新增節點 同上面docker agent安裝 ``` 節點名字改下 啟動指令 docker run -i --rm --name agent3 -v /var/run/docker.sock:/var/run/docker.sock --init jenkins/custom_agent:0.01 java -jar /usr/share/jenkins/agent.jar ``` PS: 讓container使用主機的docker daemo ``` -v /var/run/docker.sock:/var/run/docker.sock ``` 你會發現容器裡頭的jenkins沒有權限連接docker daemo 解決方式: 以root進入容器,把jenkins新增到docker group下 ``` usermod -a -G docker jenkins ``` 即便如此還是denied 為什麼? 1.我們是使用主機的/var/run/docker.sock 2.容器內外的docker droup ID不同 我們必須把容器內的docker group id 改得跟主機docker droup id 相同 ``` 主機: cat /etc/group|grep docker docker:x:118:jenkins 容器內: groupmod -g 118 docker ``` --------------- ## 用test file測試 ``` pipeline { agent none stages { stage('Back-end') { agent { docker { image 'maven:3-alpine' } } steps { sh 'mvn --version' } } stage('Front-end') { agent { docker { image 'node:7-alpine' } } steps { sh 'node --version' } } } } ``` ``` [Pipeline] withEnv [Pipeline] { [Pipeline] isUnix [Pipeline] sh + docker inspect -f . maven:3-alpine Error: No such object: maven:3-alpine [Pipeline] isUnix [Pipeline] sh + docker pull maven:3-alpine 3-alpine: Pulling from library/maven 88286f41530e: Pulling fs layer 009f6e766a1b: Pulling fs layer (略) [Pipeline] End of Pipeline Finished: SUCCESS ``` 用完容器就會被jenkins砍了 ## more easy ``` version: '3.8' networks: jenkins-network: name: jenkins volumes: data: name: jenkins-data services: jenkins: container_name: jenkins-blueocean image: jenkinsci/blueocean restart: unless-stopped user: root networks: - jenkins-network ports: - 8080:8080 - 50000:50000 volumes: - data:/var/jenkins_home - /var/run/docker.sock:/var/run/docker.sock ``` 結論 ----- 我們agent是使用docker建造的,但也想讓agent能使用docker插件,那就必須在容器中在安裝一次docker 我們選擇讓容器中的docker連上主機的docker daemon (當然也可以不要,但這比較輕鬆) 意思就是假設agent創建了一個新的container 你在主機上也是可以用docker ps看到的 假設你今天有一台強大的主機裝jenkins 就可以用這方法創建多個agent,讓每個agent彼此獨立下 卻又同時可以使用docker插件,自由的創建你要的編譯環境 並且多個agent也可以讓job不用排隊等待建置
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up