在之前的文章中介绍了Gitlab环境的搭建和CI与Gitlab的整合,那么今天主要介绍Docker中搭建CI的环境,Docker诞生于云计算的时代,它主要是基于Go语言实现的开源容器项目,目前关于Docker容器的生态系统已经很完善,而且各大主流的操作系统公司都支持Docker。今天主要介绍在Docker中搭建Jenkins的环境,关于Docker环境的搭建在后期的文章中逐步的介绍。CI就不需要多余的介绍了, 它是自动化测试中必须要掌握的一个技能之一,同时也是实现CICD整合的核心工具之一。
安装好Docker的环境以后,执行docker run hello-world,就会显示如下的信息:
在docker中安装Jenkins,首先需要找到Jenkins的镜像,同时建议安装最新版本的Jenkins,如果安装的是旧版本的,可能导致的情况就是某些插件无法安装,或者是安装以后不兼容等情况。执行如下命令获取Jenkins的镜像:
docker pull jenkins/jenkins:lts 见执行命令后的截图信息:
使用docker images查看镜像,见执行命令后输出的信息:
下来执行命令docker inspect f32b4bb22e4d查看镜像的信息,见执行命令后输出的信息:
rlay2/712c41bbf566a5627b37e0a2f7c45614d8e38e251cca29c2cc82be7ff5c086a3/diff:/var/lib/docker/overlay2/52fed63ad6c6a130d2d25ae336125aa5c3fbc8cee0090fc802d4dd2b2d854a9c/diff:/var/lib/docker/overlay2/cb07495dd0929a97e7d8f29ab35b8a0c9550ae7b6872f0c642d00696f3f3991f/diff:/var/lib/docker/overlay2/119c563d38cadc9e5e1d1c0bcd96dc0d99951c4b811bf124c9114f2edc9ffe47/diff:/var/lib/docker/overlay2/2b496ab1f553fddaa4fea6ab915ef7e577df94dcc841634d69d6939700eb8201/diff:/var/lib/docker/overlay2/836acdc3c9d75e67d9e32f4eeda5f6ce43594c1691f28150ed677e266e8ba404/diff:/var/lib/docker/overlay2/096c6f4c13a62d8b8b6078cc889943272b40eb2b9973e86f7ea8b532f3620f6c/diff:/var/lib/docker/overlay2/a5be17cb6df7dee449c4b8e67cc59fc33e22bcacdfd9b00b73567b1d87f9a469/diff:/var/lib/docker/overlay2/c2360b1fe8d644057f44c6d3f97b00a64170f6a1e3966f63da80aa36f3983216/diff",
"MergedDir": "/var/lib/docker/overlay2/c156aa815ff7d7b09b5cba868f4c0a4102e9b621f86cd3ea065f4b6f966e41f5/merged",
"UpperDir": "/var/lib/docker/overlay2/c156aa815ff7d7b09b5cba868f4c0a4102e9b621f86cd3ea065f4b6f966e41f5/diff",
"WorkDir": "/var/lib/docker/overlay2/c156aa815ff7d7b09b5cba868f4c0a4102e9b621f86cd3ea065f4b6f966e41f5/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:f73e7e79899a33b4b9b78da62efb71520844f8dd518f3c390e27bc3063bce307",
"sha256:d09a13f0562727a458ffbb3a66f6e74e70f8963f6bb813baa03c4a5af77c9dac",
"sha256:057abc6c693757d66fb885c2e5c825cf27d3fb355f71eb69e72d6c9d3f27d25c",
"sha256:e7c6a88ad7ae5b7246c905a16d5941b4292342778ce7125256bedf9864a99eab",
"sha256:c109878b2e37f01fdcff763608401f33e33e60655bfbfa46c3ea2d0df4c6137c",
"sha256:32c83575bd304d57d6f05123b63c14cc2df2fa7b38e247752716372a5634f652",
"sha256:795c58d007a6a2956ce77c78ffa426dfe0d0972a6270b2215224b21edb039f2b",
"sha256:cdfab899a401be8bb3eb7693abe57744991cb47efb9926205346526bfbd720b2",
"sha256:72d2e37a9ce7a3ef770f7048e0c69e63a37de80b9d6d5bf1a9a8fdc5ea34c4a8",
"sha256:e1787d4d466313e9d3c531683a7d66c50df82ff9fe46c53f0c6295d88dd86526",
"sha256:c30202d911aada91d881a06e6a7d166a50416b6dd6c6065f5e58da399310d1bd",
"sha256:1d04cc5c4d3141a9be518ba7f7c897e3017520f05561da3ef3c4643bc0c3e91a",
"sha256:00cbc39c09c5db5056641762df60846eabf3b6b826d328f042eb74e78d0894c9",
"sha256:9d016001a5f8ddfa6a691bb90f6729aa86b3566ca9fa5ac47ba3341e027ec59f",
"sha256:849ee987421f2b41132075a44a9d9baa4d067573cc808b958eb04a970abfcf59",
"sha256:a58e65c734eb9a0d4b9030920280a086a89efe5629224b8dd02c242e09463f82",
"sha256:723ea0f5b90dd3d28678c8a0a1cf679139b32797fc0b00536b51413ad389ee7a",
"sha256:b05f43802b1edc289418747bd37f26f46cb5411917d7a24b27ebf82c64211869",
"sha256:dad9266a531c4f240b6aed46f1834a301b086b81420828828b30472ff6d3c85b"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
在如上的信息中,就能够看到Jenkins的版本为:JENKINS_VERSION=2.176.3。
下来需要配置Jenkins的目录,具体如下如下:
mkdir jenkins_home
chown -R 1000 /root/jenkins_home/
最后就是在容器中启动Docker了,启动的命令如下:
docker run --name ci -p 8080:8080 -p 5000:5000 -v /r
oot/jenkins_home/:/var/jenkins_home jenkins/jenkins:lts
#这里对参数做一点解释,主要为 --name:对镜像自定义的名称, -p:映射容器端口和服务器端口,8080端口是jenkins的端口,5000端口用于连接Agent,-v:参数把jenins的home目录挂载到本地,执行如上的命令后,输出的log为:
Running from: /usr/share/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
Sep 21, 2019 1:31:22 PM Main deleteWinstoneTempContents
WARNING: Failed to delete the temporary Winstone file /tmp/winstone/jenkins.war
Sep 21, 2019 1:31:22 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: Logging initialized @1180ms
Sep 21, 2019 1:31:22 PM winstone.Logger logInternal
INFO: Beginning extraction from war file
Sep 21, 2019 1:31:24 PM org.eclipse.jetty.util.log.JavaUtilLog warn
WARNING: Empty contextPath
Sep 21, 2019 1:31:24 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: jetty-9.2.z-SNAPSHOT
Sep 21, 2019 1:31:25 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
Jenkins home directory: /var/jenkins_home found at: EnvVars.masterEnvVars.get("JENKINS_HOME")
Sep 21, 2019 1:31:26 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: Started w.@47404bea{/,file:/var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war}
Sep 21, 2019 1:31:26 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: Started ServerConnector@1c80e49b{HTTP/1.1}{0.0.0.0:8080}
Sep 21, 2019 1:31:26 PM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: Started @5290ms
Sep 21, 2019 1:31:26 PM winstone.Logger logInternal
INFO: Winstone Servlet Engine v2.0 running: controlPort=disabled
Sep 21, 2019 1:31:28 PM jenkins.InitReactorRunner$1 onAttained
INFO: Started initialization
Sep 21, 2019 1:31:28 PM jenkins.InitReactorRunner$1 onAttained
INFO: Listed all plugins
Sep 21, 2019 1:31:30 PM jenkins.InitReactorRunner$1 onAttained
INFO: Prepared all plugins
Sep 21, 2019 1:31:30 PM jenkins.InitReactorRunner$1 onAttained
INFO: Started all plugins
Sep 21, 2019 1:31:30 PM jenkins.InitReactorRunner$1 onAttained
INFO: Augmented all extensions
Sep 21, 2019 1:31:32 PM jenkins.InitReactorRunner$1 onAttained
INFO: Loaded all jobs
Sep 21, 2019 1:31:32 PM hudson.model.AsyncPeriodicWork$1 run
INFO: Started Download metadata
Sep 21, 2019 1:31:33 PM jenkins.util.groovy.GroovyHookScript execute
INFO: Executing /var/jenkins_home/init.groovy.d/tcp-slave-agent-port.groovy
Sep 21, 2019 1:31:34 PM jenkins.InitReactorRunner$1 onAttained
INFO: Completed initialization
Sep 21, 2019 1:31:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@6c68e6e9: display name [Root WebApplicationContext]; startup date [Sat Sep 21 13:31:34 UTC 2019]; root of context hierarchy
Sep 21, 2019 1:31:34 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@6c68e6e9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@76146d99
Sep 21, 2019 1:31:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@76146d99: defining beans [authenticationManager]; root of factory hierarchy
Sep 21, 2019 1:31:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@3cd45809: display name [Root WebApplicationContext]; startup date [Sat Sep 21 13:31:34 UTC 2019]; root of context hierarchy
Sep 21, 2019 1:31:34 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@3cd45809]: org.springframework.beans.factory.support.DefaultListableBeanFactory@dde78b7
Sep 21, 2019 1:31:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@dde78b7: defining beans [filter,legacy]; root of factory hierarchy
Sep 21, 2019 1:31:35 PM jenkins.install.SetupWizard init
INFO:
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
f1bbfbb172cc4e92b2e0422b7b101d3f
This may also be found at: /var/jenkins_home/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
--> setting agent port for jnlp
--> setting agent port for jnlp... done
显示出如上的信息后,说明启动Jenkins已成功,需要在阿里云的服务器开放8080的端口。使用docker ps -a 可以查看所有的容器,见如下截图信息:
执行docker port ci查看容器的占用端口,见如下的输出结果信息:
[root@wuya ~]# docker port ci
8080/tcp -> 0.0.0.0:8080
5000/tcp -> 0.0.0.0:5000
[root@wuya ~]#
可以看到容器占用的端口情况,当然也可以执行如下的命令查看端口的占用情况,具体为:
[root@wuya ~]# lsof -i:8080;lsof -i:5000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 9811 root 4u IPv6 16411374 0t0 TCP *:webcache (LISTEN)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 9828 root 4u IPv6 16411481 0t0 TCP *:commplex-main (LISTEN)
Jenkins在容器里面启动成功后,同时8080的端口开放后,在浏览器输入http://x.x.x.x:8080/jenkins就会显示Jenkins的启动界面,如下图所示:
下来输入密码,然后就是安装插件以及点击Finish完成。当然下次在容器里面怎么启动Jenkins了,执行docker ps -a | grep ci ,获取到它的CONTAINER ID后,直接就可以启动或者停止,见命令:
[root@wuya ~]# docker stop bdf19546027b
bdf19546027b
[root@wuya ~]# docker port ci
[root@wuya ~]# docker start bdf19546027b
bdf19546027b
[root@wuya ~]# docker port ci
5000/tcp -> 0.0.0.0:5000
8080/tcp -> 0.0.0.0:8080
当然同时执行docker stats CONTAINER ID,就可以得到某一个容器的资源使用情况,如查看Jenkins的,见如下的输出信息: