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/
原文:https://www.cnblogs.com/chenk1032/p/13580742.html