流水线可以让项目发布流程更加清晰,docker可以大大减少Jenkins配置。

使用docker安装jenkins

DockerHub搜索jenkins

数据卷挂载到 /var 磁盘目录下,因为该磁盘空间较大

  1. # DEPRECATED; use "jenkins/jenkins:lts" instead
  2. docker pull jenkins/jenkins
  3. # 启动
  4. docker run -u root -itd --name jenkins -p 8082:8080 -p 50000:50000 -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro -v /var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home jenkins/jenkins
  5. # -p 8082:8080 Jenkins默认网页访问端口为8080,将端口映射到外部主机,
  6. # 阿里云服务器要配置防火墙和安全组
  7. -p 8082:8080 \ # 映射主机端口(第一个数字8082)和容器端口(后一个数字8080),用于HTTP访问
  8. -p 50000:50000 \ # 映射主机端口(第一个数字50000)和容器端口(后一个数字50000),用于连接Agent
  9. # -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock
  10. # 使Jenkins内部可以使用docker命令
  11. # -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro
  12. # 配置Jenkins容器的时区
  13. # -v /var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home
  14. # 具名挂载 卷名:容器内路径 将Jenkins的配置映射到外部主机卷,容器删除仍可保留配置
  15. # 挂载目录 /var/volumes4docker/jenkins/jenkins_home 下

image.png
Running from: /usr/share/jenkins/jenkins.war 表示jenkins启动成功

查看容器信息

  1. # docker inspect containerID
  2. [
  3. {
  4. "Id": "dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f",
  5. "Created": "2021-10-07T08:19:50.973194204Z",
  6. "Path": "/sbin/tini",
  7. "Args": [
  8. "--",
  9. "/usr/local/bin/jenkins.sh"
  10. ],
  11. "State": {
  12. "Status": "running",
  13. "Running": true,
  14. "Paused": false,
  15. "Restarting": false,
  16. "OOMKilled": false,
  17. "Dead": false,
  18. "Pid": 15916,
  19. "ExitCode": 0,
  20. "Error": "",
  21. "StartedAt": "2021-10-07T08:31:26.25836015Z",
  22. "FinishedAt": "2021-10-07T08:30:49.508319144Z"
  23. },
  24. "Image": "sha256:d1be8383ce8b4976456a8408d7c5263b1ff36d887216131eeb94e3e6c8c8eeb3",
  25. "ResolvConfPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/resolv.conf",
  26. "HostnamePath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/hostname",
  27. "HostsPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/hosts",
  28. "LogPath": "/var/lib/docker/containers/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f/dd61611e0a08c42e8c74d6caf6f08a43ee576acae723b4242d4cbf7ada0cec0f-json.log",
  29. "Name": "/jenkins",
  30. "RestartCount": 0,
  31. "Driver": "overlay2",
  32. "Platform": "linux",
  33. "MountLabel": "",
  34. "ProcessLabel": "",
  35. "AppArmorProfile": "",
  36. "ExecIDs": null,
  37. "HostConfig": {
  38. "Binds": [
  39. "/usr/bin/docker:/usr/bin/docker",
  40. "/var/run/docker.sock:/var/run/docker.sock",
  41. "/etc/localtime:/etc/localtime:ro",
  42. "/var/volumes4docker/jenkins/jenkins_home:/var/jenkins_home"
  43. ],
  44. "ContainerIDFile": "",
  45. "LogConfig": {
  46. "Type": "json-file",
  47. "Config": {}
  48. },
  49. "NetworkMode": "default",
  50. "PortBindings": {
  51. "50000/tcp": [
  52. {
  53. "HostIp": "",
  54. "HostPort": "50000"
  55. }
  56. ],
  57. "8080/tcp": [
  58. {
  59. "HostIp": "",
  60. "HostPort": "8082"
  61. }
  62. ]
  63. },
  64. "RestartPolicy": {
  65. "Name": "no",
  66. "MaximumRetryCount": 0
  67. },
  68. "AutoRemove": false,
  69. "VolumeDriver": "",
  70. "VolumesFrom": null,
  71. "CapAdd": null,
  72. "CapDrop": null,
  73. "CgroupnsMode": "host",
  74. "Dns": [],
  75. "DnsOptions": [],
  76. "DnsSearch": [],
  77. "ExtraHosts": null,
  78. "GroupAdd": null,
  79. "IpcMode": "private",
  80. "Cgroup": "",
  81. "Links": null,
  82. "OomScoreAdj": 0,
  83. "PidMode": "",
  84. "Privileged": false,
  85. "PublishAllPorts": false,
  86. "ReadonlyRootfs": false,
  87. "SecurityOpt": null,
  88. "UTSMode": "",
  89. "UsernsMode": "",
  90. "ShmSize": 67108864,
  91. "Runtime": "runc",
  92. "ConsoleSize": [
  93. 0,
  94. 0
  95. ],
  96. "Isolation": "",
  97. "CpuShares": 0,
  98. "Memory": 0,
  99. "NanoCpus": 0,
  100. "CgroupParent": "",
  101. "BlkioWeight": 0,
  102. "BlkioWeightDevice": [],
  103. "BlkioDeviceReadBps": null,
  104. "BlkioDeviceWriteBps": null,
  105. "BlkioDeviceReadIOps": null,
  106. "BlkioDeviceWriteIOps": null,
  107. "CpuPeriod": 0,
  108. "CpuQuota": 0,
  109. "CpuRealtimePeriod": 0,
  110. "CpuRealtimeRuntime": 0,
  111. "CpusetCpus": "",
  112. "CpusetMems": "",
  113. "Devices": [],
  114. "DeviceCgroupRules": null,
  115. "DeviceRequests": null,
  116. "KernelMemory": 0,
  117. "KernelMemoryTCP": 0,
  118. "MemoryReservation": 0,
  119. "MemorySwap": 0,
  120. "MemorySwappiness": null,
  121. "OomKillDisable": false,
  122. "PidsLimit": null,
  123. "Ulimits": null,
  124. "CpuCount": 0,
  125. "CpuPercent": 0,
  126. "IOMaximumIOps": 0,
  127. "IOMaximumBandwidth": 0,
  128. "MaskedPaths": [
  129. "/proc/asound",
  130. "/proc/acpi",
  131. "/proc/kcore",
  132. "/proc/keys",
  133. "/proc/latency_stats",
  134. "/proc/timer_list",
  135. "/proc/timer_stats",
  136. "/proc/sched_debug",
  137. "/proc/scsi",
  138. "/sys/firmware"
  139. ],
  140. "ReadonlyPaths": [
  141. "/proc/bus",
  142. "/proc/fs",
  143. "/proc/irq",
  144. "/proc/sys",
  145. "/proc/sysrq-trigger"
  146. ]
  147. },
  148. "GraphDriver": {
  149. "Data": {
  150. "LowerDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63-init/diff:/var/lib/docker/overlay2/594c79d6b867335e0d3cba6beff9068a2881e70aedebfbdbe765cf681846d402/diff:/var/lib/docker/overlay2/ababb979acfee1b0fb4ebc078e41849b3998866e89334a9c1981b8b14c26ec6d/diff:/var/lib/docker/overlay2/d5c9b846126480c1bc5bb329c15cc81796e7c4ded5d0d07b61d2d56da5ede4ee/diff:/var/lib/docker/overlay2/7ecf232ddc190042dbaebb0eec97b8b7677679e5109296ba4258fcbfaf602c40/diff:/var/lib/docker/overlay2/d880a90f682016a5c1e65ad409327da5554dff317632d2d01a61c15f6abe86db/diff:/var/lib/docker/overlay2/6635d79e7c57657d5200cc4eec9c84e40488f3013415b14093853ba87d998231/diff:/var/lib/docker/overlay2/dbad0d02b0641f5ceb126d41ed67f5cea5c4e668d47bddeeecf77962905cb290/diff:/var/lib/docker/overlay2/70f1d11b7fca6f970339fa0fc1a08dd15a0f92b23cda2a9e4b1e241a75987577/diff:/var/lib/docker/overlay2/c76ffd9dac15f7f77bf1fa9ede92ead04888647bba180b361a60bec7c04f4bf1/diff:/var/lib/docker/overlay2/ee8b58c94ef4699bb14fcd5c1d9dd6766d0b850fb7dc8df1eed6acfa2fc7a096/diff:/var/lib/docker/overlay2/da638011364fa24f3b17b994ba85530fc96d850906ffbd4287d2b021d57f5f51/diff:/var/lib/docker/overlay2/162028340c25fb6568da119a4e08e0da1b4f5bd6b47e0ed775c2e5c2db85ffda/diff:/var/lib/docker/overlay2/7193bfcdaadc04c8dd578459185e5a517484c77973ee99ab0571e71d84323ea5/diff:/var/lib/docker/overlay2/6310d645f34aa9dcf56271518b23e448ba53737226503ba104b6df763bd07ff7/diff:/var/lib/docker/overlay2/ce15fc78d857ac74372f6b15b1b65d8c5a70bae58555c16ff43a6928a110330f/diff:/var/lib/docker/overlay2/5122a05d3e3d5141403d70b3d8360962f08518c91a3d9013e413b1b3ca98ff86/diff:/var/lib/docker/overlay2/649d241945635331ec166baa9afb4dcbed387f7a3c01ae909de2f4d2b28e0698/diff",
  151. "MergedDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/merged",
  152. "UpperDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/diff",
  153. "WorkDir": "/var/lib/docker/overlay2/0293c2371b66db4028a138952309d3e9c8d67477a9fdbf34d2e8523d14a90b63/work"
  154. },
  155. "Name": "overlay2"
  156. },
  157. "Mounts": [
  158. {
  159. "Type": "bind",
  160. "Source": "/usr/bin/docker",
  161. "Destination": "/usr/bin/docker",
  162. "Mode": "",
  163. "RW": true,
  164. "Propagation": "rprivate"
  165. },
  166. {
  167. "Type": "bind",
  168. "Source": "/var/run/docker.sock",
  169. "Destination": "/var/run/docker.sock",
  170. "Mode": "",
  171. "RW": true,
  172. "Propagation": "rprivate"
  173. },
  174. {
  175. "Type": "bind",
  176. "Source": "/etc/localtime",
  177. "Destination": "/etc/localtime",
  178. "Mode": "ro",
  179. "RW": false,
  180. "Propagation": "rprivate"
  181. },
  182. {
  183. "Type": "bind",
  184. "Source": "/var/volumes4docker/jenkins/jenkins_home",
  185. "Destination": "/var/jenkins_home",
  186. "Mode": "",
  187. "RW": true,
  188. "Propagation": "rprivate"
  189. }
  190. ],
  191. "Config": {
  192. "Hostname": "dd61611e0a08",
  193. "Domainname": "",
  194. "User": "root",
  195. "AttachStdin": false,
  196. "AttachStdout": true,
  197. "AttachStderr": true,
  198. "ExposedPorts": {
  199. "50000/tcp": {},
  200. "8080/tcp": {}
  201. },
  202. "Tty": false,
  203. "OpenStdin": false,
  204. "StdinOnce": false,
  205. "Env": [
  206. "TZ=Asia/Shanghai",
  207. "PATH=/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  208. "LANG=C.UTF-8",
  209. "JENKINS_HOME=/var/jenkins_home",
  210. "JENKINS_SLAVE_AGENT_PORT=50000",
  211. "REF=/usr/share/jenkins/ref",
  212. "JENKINS_VERSION=2.315",
  213. "JENKINS_UC=https://updates.jenkins.io",
  214. "JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental",
  215. "JENKINS_INCREMENTALS_REPO_MIRROR=https://repo.jenkins-ci.org/incrementals",
  216. "COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log",
  217. "JAVA_HOME=/opt/java/openjdk"
  218. ],
  219. "Cmd": null,
  220. "Image": "jenkins/jenkins",
  221. "Volumes": {
  222. "/var/jenkins_home": {}
  223. },
  224. "WorkingDir": "",
  225. "Entrypoint": [
  226. "/sbin/tini",
  227. "--",
  228. "/usr/local/bin/jenkins.sh"
  229. ],
  230. "OnBuild": null,
  231. "Labels": {
  232. "org.opencontainers.image.description": "The Jenkins Continuous Integration and Delivery server",
  233. "org.opencontainers.image.licenses": "MIT",
  234. "org.opencontainers.image.source": "https://github.com/jenkinsci/docker",
  235. "org.opencontainers.image.title": "Official Jenkins Docker image",
  236. "org.opencontainers.image.url": "https://www.jenkins.io/",
  237. "org.opencontainers.image.vendor": "Jenkins project",
  238. "org.opencontainers.image.version": "2.315"
  239. }
  240. },
  241. "NetworkSettings": {
  242. "Bridge": "",
  243. "SandboxID": "1cd79de9c2420e53d5881242b400b174628a31e3693ca613f91ac436e69ef69b",
  244. "HairpinMode": false,
  245. "LinkLocalIPv6Address": "",
  246. "LinkLocalIPv6PrefixLen": 0,
  247. "Ports": {
  248. "50000/tcp": [
  249. {
  250. "HostIp": "0.0.0.0",
  251. "HostPort": "50000"
  252. }
  253. ],
  254. "8080/tcp": [
  255. {
  256. "HostIp": "0.0.0.0",
  257. "HostPort": "8082"
  258. }
  259. ]
  260. },
  261. "SandboxKey": "/var/run/docker/netns/1cd79de9c242",
  262. "SecondaryIPAddresses": null,
  263. "SecondaryIPv6Addresses": null,
  264. "EndpointID": "2677c1159f6b61d7f7b9a7b1bd31f53fe059792409e5ad9cb9203c9d49ddcf08",
  265. "Gateway": "172.17.0.1",
  266. "GlobalIPv6Address": "",
  267. "GlobalIPv6PrefixLen": 0,
  268. "IPAddress": "172.17.0.3",
  269. "IPPrefixLen": 16,
  270. "IPv6Gateway": "",
  271. "MacAddress": "02:42:ac:11:00:03",
  272. "Networks": {
  273. "bridge": {
  274. "IPAMConfig": null,
  275. "Links": null,
  276. "Aliases": null,
  277. "NetworkID": "8d10d0e8db5eaee9a179ad0c38340b01c68ac5135f648e9149aac24ac4113ccb",
  278. "EndpointID": "2677c1159f6b61d7f7b9a7b1bd31f53fe059792409e5ad9cb9203c9d49ddcf08",
  279. "Gateway": "172.17.0.1",
  280. "IPAddress": "172.17.0.3",
  281. "IPPrefixLen": 16,
  282. "IPv6Gateway": "",
  283. "GlobalIPv6Address": "",
  284. "GlobalIPv6PrefixLen": 0,
  285. "MacAddress": "02:42:ac:11:00:03",
  286. "DriverOpts": null
  287. }
  288. }
  289. }
  290. }
  291. ]

宿主机访问jenkins控制台日志

  1. docker logs -f jenkins
  2. # -f 查看实时日志

宿主机访问jenkins主目录

  1. # 容器内/var/jenkins_home挂载目录
  2. /var/volumes4docker/jenkins/jenkins_home

启动问题及处理

  1. curl localhost:8082
  2. Authentication required

防火墙要开放50000 和8082 端口,同时也要配置阿里云安全组规则。