jenkins on Windows10 192.168.1.8,
docker on Ubuntu, 192.168.1.11
從ubuntu create 一個image from dockerfile, 從Jenkins添加這個image做從節點。
1. 配置docker host to enable remote API
# Jenkins通過REST API連接docker,Docker Remote API port: 4243, Docker Hostport Range: 32768 - 60999
打開docker service file : /lib/systemd/system/docker.service, 找到ExecStart一行,將其替換成:
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
重啟docker
sudo systemctl daemon-reload sudo service docker restart
驗證:
docker host: curl http://localhost:4243/version Jenkins host: curl http://192.168.1.11:4243/version
2. 創建一個簡單鏡像, 必須包含ssh server, java, 登錄用戶名密碼
Dockerfile:
FROM ubuntu:18.04 RUN apt-get update && \ apt-get -qy full-upgrade && \ apt-get install -qy git && \ # Install a basic SSH server apt-get install -qy openssh-server && \ sed -i 's|session required pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd && \ mkdir -p /var/run/sshd && \ # Install JDK 8 apt-get install -qy openjdk-8-jdk && \ # Cleanup old packages apt-get -qy autoremove && \ # Add user jenkins to the image adduser --quiet jenkins && \ # Set password for the jenkins user echo "jenkins:jenkins" | chpasswd && \ mkdir /home/jenkins/.m2 # Copy authorized keys COPY ~/.ssh/authorized_keys /home/jenkins/.ssh/authorized_keys RUN chown -R jenkins:jenkins /home/jenkins/.m2/ && \ chown -R jenkins:jenkins /home/jenkins/.ssh/ # Standard SSH port EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]
在Ubuntu將 Dockerfile和.ssh放在同一目錄:
$ docker build -t jenkins_node .
3. 配置Jenkins
添加Dock插件
進入Manage Jenkins - System Configuration - Manage Nodes and Clouds - Configure Clouds
點開Docker Agent templates, 其中Registry Authentication和SSH Credentials都是Dockerfile中的jenkins/jenkins
4. 新建任務,Restrict where this project can be run,填上Label: dockerNode
5. Build Now...
https://devopscube.com/docker-containers-as-build-slaves-jenkins/