安装环境
-
操作系统 centos7.3
-
安装Docker,安装及配置见 《Docker之Docker介绍及安装配置
-
安装Jenkins
下载Jenkins
-
一、Jenkins安装
1.获取docker镜像 -
docker pull jenkins
2.查看jenkins版本
-
docker inspect jenkins
-
[root@localhost ~]# docker inspect jenkins [ { "Id": "sha256:cd14cecfdb3a657ba7d05bea026e7ac8b9abafc6e5c66253ab327c7211fa6281", "RepoTags": [ "jenkins:latest" ], "RepoDigests": [ "jenkins@sha256:eeb4850eb65f2d92500e421b430ed1ec58a7ac909e91f518926e02473904f668" ], "Parent": "", "Comment": "", "Created": "2018-07-17T16:20:34.183816595Z", "Container": "a3e3890f6333066d464113032a93622a8a12305fa1cf7a29e57ad29cbde66a19", "ContainerConfig": { "Hostname": "a3e3890f6333", "Domainname": "", "User": "jenkins", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "50000/tcp": {}, "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "JAVA_HOME=/docker-java-home", "JAVA_VERSION=8u171", "JAVA_DEBIAN_VERSION=8u171-b11-1~deb9u1", "CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1", "JENKINS_HOME=/var/jenkins_home", "JENKINS_SLAVE_AGENT_PORT=50000", "TINI_VERSION=0.14.0", "TINI_SHA=6c41ec7d33e857d4779f14d9c74924cab0c7973485d2972419a3b7c7620ff5fd", "JENKINS_VERSION=2.60.3", "JENKINS_UC=https://updates.jenkins.io", "JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental", "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log" ], "Cmd": [ "/bin/sh", "-c", "#(nop) COPY file:a4f750618f51f00d2f8268ac43fdd8d8ef5ce16e1d412afa5a9bc7492a5d975f in /usr/local/bin/install-plugins.sh " ], "ArgsEscaped": true, "Image": "sha256:9e1d90142b1a1b659fae5d4b6427c3196495b24006ac29943aac019390bdd5d1", "Volumes": { "/var/jenkins_home": {} }, "WorkingDir": "", "Entrypoint": [ "/bin/tini", "--", "/usr/local/bin/jenkins.sh" ], "OnBuild": [], "Labels": {} }, "DockerVersion": "17.06.2-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "jenkins", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "50000/tcp": {}, "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "LANG=C.UTF-8", "JAVA_HOME=/docker-java-home", "JAVA_VERSION=8u171", "JAVA_DEBIAN_VERSION=8u171-b11-1~deb9u1", "CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1", "JENKINS_HOME=/var/jenkins_home", "JENKINS_SLAVE_AGENT_PORT=50000", "TINI_VERSION=0.14.0", "TINI_SHA=6c41ec7d33e857d4779f14d9c74924cab0c7973485d2972419a3b7c7620ff5fd", "JENKINS_VERSION=2.60.3", "JENKINS_UC=https://updates.jenkins.io", "JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental", "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log" ], "Cmd": null, "ArgsEscaped": true, "Image": "sha256:9e1d90142b1a1b659fae5d4b6427c3196495b24006ac29943aac019390bdd5d1", "Volumes": { "/var/jenkins_home": {} }, "WorkingDir": "", "Entrypoint": [ "/bin/tini", "--", "/usr/local/bin/jenkins.sh" ], "OnBuild": [], "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 695718055, "VirtualSize": 695718055, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/0d29e24fe75cbdb208ecb411bb2ebed2bf14de7b74da80899bf3cb65034ef90b/diff:/var/lib/docker/overlay2/3880d516c2506c6179730899cc1b3946b81bbc8f978ff488bae14c7b80ec933a/diff:/var/lib/docker/overlay2/812e49fd758182e4fb3d71159c15ab0815b9d7229329ba74931e4ae92a7763b8/diff:/var/lib/docker/overlay2/e2560b6a9c566cc1720c3313337259b68caa9e3f1ad331173a03b93f12524ceb/diff:/var/lib/docker/overlay2/e7ec25bbb7d232e60629969973507a622670ef64f2690e94f462d52aecd142bd/diff:/var/lib/docker/overlay2/36bffec22ff3af366a47805feca49dd862a4eb0b2a5814a7b857ecfad64e446f/diff:/var/lib/docker/overlay2/0a98ad8da0ded10dad6c26a7970665c8d948dd76bf18df68bb6d607cb517563f/diff:/var/lib/docker/overlay2/49dae95c4b4a42584b014f91e2f3fc9841e9ce6da566b21181c0a9b47e62ae6a/diff:/var/lib/docker/overlay2/d261d98f9f7564e8f04be46aed32974bebe1d0dfd13e0917f65494307a31d464/diff:/var/lib/docker/overlay2/e240752f2b7e868e7227d898efa7902c36903ca8c7f929cf293c8ca647bc6c08/diff:/var/lib/docker/overlay2/994d6c02ddfc3124ba8e4620475d0f04c5cbf2786b2fcb40bc1ef2325f50802c/diff:/var/lib/docker/overlay2/e182e8cd263a6239b68300f6ebf60baab86aabdbde157e92af849df871e7d4e0/diff:/var/lib/docker/overlay2/4e8a773b7ac16afb7815ba79af339c2c7ddfc3f27d0c6b465071179bc7e09e3c/diff:/var/lib/docker/overlay2/711ce086dd89ef5d2c781e73a601177f6b35e90900a1b22b0ed8f775fedb91fa/diff:/var/lib/docker/overlay2/7abf702f4da530ed159af0f84ad87f2416077dd7458cc11ea05e47c56c5f7a6f/diff:/var/lib/docker/overlay2/c88c187eaaef98695d21ec49987793cce27137fdb75ae2a2b650d2a440ae828c/diff:/var/lib/docker/overlay2/ec7becae8e35806e2e17f6d93e250ad38bf944293800c472c31a663cdb1eec11/diff:/var/lib/docker/overlay2/53312955925ee209944e70d24a080dcb89ea944a548c9edd4624afbd1f581924/diff:/var/lib/docker/overlay2/abf752d7c713285775f8bf3c80a9719ab7b60476305d664095466e8028e317c3/diff", "MergedDir": "/var/lib/docker/overlay2/ee2f7b45daf8d367044507df8bcac2c4c72bc7d95e67486ff7b0c4e2f5d1713a/merged", "UpperDir": "/var/lib/docker/overlay2/ee2f7b45daf8d367044507df8bcac2c4c72bc7d95e67486ff7b0c4e2f5d1713a/diff", "WorkDir": "/var/lib/docker/overlay2/ee2f7b45daf8d367044507df8bcac2c4c72bc7d95e67486ff7b0c4e2f5d1713a/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:3b10514a95bec77489a57d6e2fbfddb7ddfdb643907470ce5de0f1b05c603706", "sha256:719d45669b35360e3ca0d53d159c42ca9985eb925a6b28ac38d0ded39a1314e8", "sha256:ce6466f43b110e66d7d3a72600262a1f1b015d9a64aad5f133f081868d4825fc", "sha256:fa0c3f992cbd10a0569ed212414b50f1c35d97521f7e4a9e55a9abcf47ca77e2", "sha256:c477b6c8ca459c76222eb535faedb8979f4094552ac2a6cd56cb0f29262fdcfb", "sha256:9c2e8b91bfa88a95f732adec2bfc5c03fb40c535e6646ecf386235cdfb4e0e96", "sha256:c3ebb2aa7787555b542be6400d8064ae209a29d90c6e1b99ac5d02d5040392b4", "sha256:518c9e7eb3264c27c8d77b4d194c80d14373c5942c2da2e4409f062573139f1b", "sha256:571ae0d6961ab39851cdf93f5a753902cdd563d82e78cea54cbb6b0d420eb8f4", "sha256:9fe468dbb76f324cd4287ea7e32edf6fe0f8aa993776fc152a17ae4a9a68f948", "sha256:6ff38243bfb80a82749f0a954d1bd2faaa5673de4be5d5d9665100e2785fed40", "sha256:c19390bb619a105dd32e573b3a26765216ee27736304266909104607045b4779", "sha256:a0775f499ef138c2badd29831b31c28f4515c7802d0bde35111c8f8c7b60ed7d", "sha256:1db7316340117880a83816808041a1f9ea1848632ab72f51ce79fb60a6135e3e", "sha256:047f9c957a2e718be97e9928f1170d25e7dadd71737ca2a1e8e3db139c905915", "sha256:ceed640cbb9350a13ce3051520b59833fb2fc4bc05c69873792293a2a27a485c", "sha256:d51e4482f53a5764af588bea1c8dda817e968af11e7e2df3ae3051757e1dc533", "sha256:37c1d818eb0bf741290f4122d6ae95b1dffb1f544896b0089ad7789613fa8804", "sha256:b1891bf16fa79f6376941e4b2409d26f2c85e993de9ba3dcaee57dc80ac4d889", "sha256:0577e068c587d352efe5dd72477ae6927626840d167fbbf59e17241b8f50a127" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]
-
启动Jenkins前准备
创建
jenkins
文件夹创建jenkins文件夹,用于和容器内文件夹做磁盘挂载
命令:
mkdir /usr/local/jenkins
为jenkins 文件夹添加读写权限以便jenkins应用运行时读写文件
-
chmod 777 /usr/local/jenkins
-
启动Jenkins
-
docker run -itd -p 8880:8080 -p 50000:50000 --name jenkins --privileged=true -v /usr/local/jenkins:/usr/local/jenkins jenkins
-
-p 8880:8880 -p 50000:50000 进行端口映射
-
--privileged=true
在CentOS7中的安全模块selinux把权限禁掉了,参数给容器加特权。 -
-v
/usr/local/jenkins:/usr/local/jenkins 磁盘挂载 -
--name 以启动容器的名字
第一个jenkins 容器的名字 (--name jenkins --privileged=true)第二个jenkins 镜像的名字(/usr/local/jenkins:/usr/local/jenkins jenkins)
当容器停止时/jenkins不会被清除,便于数据备份,体现了数据与程序分离的原则,通过--name 来指定Jenkins的名称,便于查找
-
查看运行状态
docker ps
访问jenkins服务需要输入密码
先进入到容器内部
docker exec -it jenkins /bin/bash
jenkins 为容器的名字
退出当前jenkins
exit
查看防火墙状态
systemctl status firewalld
查看防火墙的开放端口
firewall-cmd --list-all
结果报错:
解决:是因为 之前安装过Python3 将Python2.7 替换导致的
以上提示中提到了“/usr/bin/firewall-cmd”,我们输入以下命令:
vi /usr/bin/firewall-cmd
修改第一行,在python后面添加数字2.7:
修改后保存即可
在此输入
firewall-cmd --list-all
查看对应的规则信息
将jenkins 端口 8880加入到开放的列表中
firewall-cmd --permanent --add-port=8880/tcp
# 移除端口 附加的防火墙其他知识 firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
再次查看防火墙开放的端口,8880 被加入进去
接下来就可以用http://ip:8880 来访问jenkins
根据提示进入到 容器中查看对应的密码
将密码粘贴到此处
输入后安装建议的插件,推荐的插件里就包含版本控制软件git。
完毕后,根据提示设置登陆账户
然后新建一个项目,在源代码控制那一栏,输入你的项目的线上git仓库地址,注意默认应该是master分支,因为生产环境部署的代码必须是主分支