網頁

2020/10/2

Docker 安裝Jenkins

在macOS的docker上安裝jenkins。

範例環境:

  • macOS Catalina
  • Docker 19.03.12


下載Jenkins image

在Google搜尋"jenkins docker image"可能會有兩個結果,一是jenkins,另一是jenkins/jenkins。應該安裝jenkins/jenkins,因為jenkins已經deprecated了。

在terminal終端機輸入docker pull jenkins/jenkins:lts下載長期支援版本(Long-term support)的image。

$ docker pull jenkins/jenkins:lts
lts: Pulling from jenkins/jenkins
3192219afd04: Pull complete
17c160265e75: Pull complete
cc4fe40d0e61: Pull complete
9d647f502a07: Pull complete
d108b8c498aa: Pull complete
1bfe918b8aa5: Pull complete
dafa1a7c0751: Pull complete
56d10de66d35: Pull complete
551c64ca8830: Pull complete
baf872ebca58: Pull complete
a18e71cf1279: Pull complete
9094538107fd: Pull complete
37e72a5fe7b3: Pull complete
1f63632e1642: Pull complete
76e9a99c1663: Pull complete
ac41ea128690: Pull complete
7c514880d0ae: Pull complete
1a9fbaf06b30: Pull complete
a640391b16ec: Pull complete
16ba366203c0: Pull complete
Digest: sha256:fb4e6cf6a7038aa30c6420a4a041fc2eb4eb470e7b09cdf4b18f0882f50da8bc
Status: Downloaded newer image for jenkins/jenkins:lts
docker.io/jenkins/jenkins:lts

jenkins image下載完輸入docker images檢視。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jenkins/jenkins     lts                 190554e5446b        3 weeks ago         708MB


在Docker容器運行Jenkins

輸入docker run --name jenkins -p 8080:8080 -p 50000:50000 -v ~/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts在docker container上執行jenkins。

  • --name jenkins:將container的名稱命名為jenkins
  • -p 8080:8080:將container的8080 port對映到主機(host)的8080 port,為jenkins的管理頁面登入port
  • -p 50000:50000:將container的50000 port對映到主機(host)的50000 port,為jenkins連結slave節點使用的JNLP(Java Web Start)port。
  • -v ~/jenkins_home:/var/jenkins_home:將container的/var/jenkins_home對映到host的~/jenkins_home目錄做為jenkins的docker volume。若沒有~/jenkins_home目錄執行時會自動建立。
  • -v /var/run/docker.sock:/var/run/docker.sock:讓jenkins containers中能使用docker daemon。
$ docker run --name jenkins -p 8080:8080 -p 50000:50000 -v ~/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:lts
Running from: /usr/share/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
2020-10-02 11:11:01.758+0000 [id=1]	INFO	org.eclipse.jetty.util.log.Log#initialized: Logging initialized @676ms to org.eclipse.jetty.util.log.JavaUtilLog
2020-10-02 11:11:01.993+0000 [id=1]	INFO	winstone.Logger#logInternal: Beginning extraction from war file
2020-10-02 11:11:17.873+0000 [id=1]	WARNING	o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2020-10-02 11:11:18.005+0000 [id=1]	INFO	org.eclipse.jetty.server.Server#doStart: jetty-9.4.30.v20200611; built: 2020-06-11T12:34:51.929Z; git: 271836e4c1f4612f12b7bb13ef5a92a927634b0d; jvm 1.8.0_242-b08
2020-10-02 11:11:22.608+0000 [id=1]	INFO	o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2020-10-02 11:11:22.738+0000 [id=1]	INFO	o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0
2020-10-02 11:11:22.738+0000 [id=1]	INFO	o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults
2020-10-02 11:11:22.750+0000 [id=1]	INFO	o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 660000ms
2020-10-02 11:11:23.702+0000 [id=1]	INFO	hudson.WebAppMain#contextInitialized: Jenkins home directory: /var/jenkins_home found at: EnvVars.masterEnvVars.get("JENKINS_HOME")
2020-10-02 11:11:24.004+0000 [id=1]	INFO	o.e.j.s.handler.ContextHandler#doStart: Started w.@733c423e{Jenkins v2.249.1,/,file:///var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war}
2020-10-02 11:11:24.059+0000 [id=1]	INFO	o.e.j.server.AbstractConnector#doStart: Started ServerConnector@387a8303{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2020-10-02 11:11:24.060+0000 [id=1]	INFO	org.eclipse.jetty.server.Server#doStart: Started @23014ms
2020-10-02 11:11:24.066+0000 [id=21]	INFO	winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2020-10-02 11:11:25.697+0000 [id=28]	INFO	jenkins.InitReactorRunner$1#onAttained: Started initialization
2020-10-02 11:11:25.797+0000 [id=31]	INFO	jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2020-10-02 11:11:28.101+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2020-10-02 11:11:28.116+0000 [id=29]	INFO	jenkins.InitReactorRunner$1#onAttained: Started all plugins
2020-10-02 11:11:28.141+0000 [id=27]	INFO	jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2020-10-02 11:11:29.047+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: System config loaded
2020-10-02 11:11:29.047+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: System config adapted
2020-10-02 11:11:29.050+0000 [id=27]	INFO	jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2020-10-02 11:11:29.052+0000 [id=26]	INFO	jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2020-10-02 11:11:29.084+0000 [id=46]	INFO	hudson.model.AsyncPeriodicWork#lambda$doRun$0: Started Download metadata
2020-10-02 11:11:29.128+0000 [id=46]	INFO	hudson.util.Retrier#start: Attempt #1 to do the action check updates server
2020-10-02 11:11:30.933+0000 [id=30]	INFO	o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@7e24ad91: display name [Root WebApplicationContext]; startup date [Fri Oct 02 11:11:30 UTC 2020]; root of context hierarchy
2020-10-02 11:11:30.933+0000 [id=30]	INFO	o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@7e24ad91]: org.springframework.beans.factory.support.DefaultListableBeanFactory@63464205
2020-10-02 11:11:30.964+0000 [id=30]	INFO	o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@63464205: defining beans [authenticationManager]; root of factory hierarchy
2020-10-02 11:11:31.368+0000 [id=30]	INFO	o.s.c.s.AbstractApplicationContext#prepareRefresh: Refreshing org.springframework.web.context.support.StaticWebApplicationContext@51e42bf0: display name [Root WebApplicationContext]; startup date [Fri Oct 02 11:11:31 UTC 2020]; root of context hierarchy
2020-10-02 11:11:31.369+0000 [id=30]	INFO	o.s.c.s.AbstractApplicationContext#obtainFreshBeanFactory: Bean factory for application context [org.springframework.web.context.support.StaticWebApplicationContext@51e42bf0]: org.springframework.beans.factory.support.DefaultListableBeanFactory@5e472430
2020-10-02 11:11:31.372+0000 [id=30]	INFO	o.s.b.f.s.DefaultListableBeanFactory#preInstantiateSingletons: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5e472430: defining beans [filter,legacy]; root of factory hierarchy
2020-10-02 11:11:32.022+0000 [id=30]	INFO	jenkins.install.SetupWizard#init:

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

eaef0d9d2454497997475e8e8e3f2ddb

This may also be found at: /var/jenkins_home/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2020-10-02 11:11:41.466+0000 [id=31]	INFO	jenkins.InitReactorRunner$1#onAttained: Completed initialization
2020-10-02 11:11:41.499+0000 [id=20]	INFO	hudson.WebAppMain$3#run: Jenkins is fully up and running
2020-10-02 11:11:42.272+0000 [id=46]	INFO	h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2020-10-02 11:11:42.273+0000 [id=46]	INFO	hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2020-10-02 11:11:42.283+0000 [id=46]	INFO	hudson.model.AsyncPeriodicWork#lambda$doRun$0: Finished Download metadata. 13,190 ms
2020-10-02 10:54:50.302+0000 [id=30]	INFO	jenkins.InitReactorRunner$1#onAttained: Completed initialization
2020-10-02 10:54:50.319+0000 [id=20]	INFO	hudson.WebAppMain$3#run: Jenkins is fully up and running

啟動時會產生一組密碼顯示在啟動時的訊息中,例如上面的eaef0d9d2454497997475e8e8e3f2ddb。這組密碼也能在/var/jenkins_home/secrets/initialAdminPassword找到,也就是啟動時設定的jenkins docker volume目錄~/jenkins_home/secrets/initialAdminPassword。由於設定volumes '~/jenkins_home:/var/jenkins_home',所以可以直接在host的~/jenkins_home/secrets/initialAdminPassword查看初始密碼。


Docker Compose

除了以上下載image後執行命令來運行Jenkins container的方式外,也可以改用建立docker-compose.yml的方式來運行Jenkins container。

docker-compose.yml

version: '3.8'
services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    ports:
      - '8080:8080'
      - '50000:50000'
    volumes:
      - '~/jenkins_home:/var/jenkins_home'
      - '/var/run/docker.sock:/var/run/docker.sock'
    stdin_open: true
    tty: true

>docker-compose.yml同目錄輸入docker-compose up -d啟動container。

$ docker-compose up -d
[+] Running 14/14
 ⠿ redis Pulled                                                                                                                                                                                       73.9s
   ⠿ bbeef03cda1f Pull complete                                                                                                                                                                       40.9s
   ⠿ 398a127e9b8f Pull complete                                                                                                                                                                       42.7s
   ⠿ 740ae8bf5abb Pull complete                                                                                                                                                                       42.9s
   ⠿ 5b55a2898e5d Pull complete                                                                                                                                                                       42.9s
   ⠿ 11d78b95ea0e Pull complete                                                                                                                                                                       43.0s
   ⠿ 725cf6a496a4 Pull complete                                                                                                                                                                       67.2s
   ⠿ acc71bc946ba Pull complete                                                                                                                                                                       67.2s
   ⠿ aeb3c582524d Pull complete                                                                                                                                                                       67.4s
   ⠿ 6ba52cfaad0b Pull complete                                                                                                                                                                       69.5s
   ⠿ 4d90a31f0368 Pull complete                                                                                                                                                                       69.6s
   ⠿ 38d0248b6a15 Pull complete                                                                                                                                                                       69.6s
   ⠿ e148037a0095 Pull complete                                                                                                                                                                       69.7s
   ⠿ 3a72a5e34c1b Pull complete                                                                                                                                                                       69.7s
[+] Running 1/1
 ⠿ Container jenkins  Started


檢視Jenkins container

輸入docker ps檢視運行中的jenkins。

CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
c90ff3c69a58        jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   27 minutes ago      Up 27 minutes       0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   jenkins

運行的jenkins docker container id為c90ff3c69a58,名稱為jenkins

使用指令docker exec -it jenkins bash進入jenkins container的bash shell介面。

$ docker exec -it jenkins bash
jenkins@c90ff3c69a58:/$


初始配置

第一次啟動要做初始配置如下。

在瀏覽器網址列輸入http://localhost:8080進入jenkins管理頁面(若出現404則要等一下,Jenkins很慢起來)。第一次進入會要求輸入密碼,把啟動時產生的密碼貼上並點Confinue



點選[Install suggested plugins]。



接著就開始啟動並安裝plugins。



第一次執行會請你建立管理員,這邊直接按[Skip and continue as admin]跳過。



設定jenkins URL,使用預設的http://localhost:8080/即可,點選Save and Finish



點選Start using Jenkins



接著就進入Jenkins管理頁面首頁。



到此就完成docker jenkins的安裝。接著建立第一個Jenkins任務


修改密碼

People -> admin -> Configure頁面的Password欄位可修改密碼。




沒有留言:

張貼留言