您现在的位置是:首页 >技术杂谈 >关于Docker 和 Jenkins的相关聚焦网站首页技术杂谈

关于Docker 和 Jenkins的相关聚焦

DB架构 2023-06-14 04:00:02
简介关于Docker 和 Jenkins的相关聚焦

1.Install Docker refer 

Documentation:System:DDG:InstallationofDockerEngineCommunityEditiononCentOS7:Current - Genesys Documentation

 2.Installing Docker Compose 

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-centos-7

[jenkins@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[sudo] password for jenkins: 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100 11.2M  100 11.2M    0     0  3216k      0  0:00:03  0:00:03 --:--:-- 4894k
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ sudo chmod +x /usr/local/bin/docker-compose
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ docker-compose
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert deploy
                              keys in v3 files to their non-Swarm equivalent

Commands:
  build              Build or rebuild services
  bundle             Generate a Docker bundle from the Compose file
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information
[jenkins@localhost ~]$ 

3. Download the jenkins Docker image

[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ docker pull jenkins/jenkins
Using default tag: latest
latest: Pulling from jenkins/jenkins
b0248cf3e63c: Pull complete 
33b6b181dd75: Pull complete 
a0ae619c7e04: Pull complete 
628a3df031c5: Pull complete 
0c9cd1e5e117: Pull complete 
4a90cdec99a2: Pull complete 
a8bb08b626f0: Pull complete 
0564e42b8d2d: Pull complete 
fd62b7502c2d: Pull complete 
f1461e578f49: Pull complete 
9cde15aa1b91: Pull complete 
f055553ffae1: Pull complete 
bcf1ce5799b0: Pull complete 
Digest: sha256:7560cc798140cdcdef5b75ca069c28b2a44f179827e3e55076ea18bdfb17b5aa
Status: Downloaded newer image for jenkins/jenkins:latest
docker.io/jenkins/jenkins:latest
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ docker images
REPOSITORY        TAG       IMAGE ID       CREATED      SIZE
jenkins/jenkins   latest    6c30514bdde6   6 days ago   471MB
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ docker info | grep -i root
 Docker Root Dir: /var/lib/docker
[jenkins@localhost ~]$ 
[jenkins@localhost ~]$ sudo du -sh /var/lib/docker
[sudo] password for jenkins: 
474M    /var/lib/docker
[jenkins@localhost ~]$ 

4. Create a Docker Compose file for Jekins

jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ echo $PWD
/home/jenkins/jenkins-data
[jenkins@localhost jenkins-data]$ echo $PWD/jenkins_home
/home/jenkins/jenkins-data/jenkins_home
[jenkins@localhost jenkins-data]$ vi docker-compose.yml
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ cat docker-compose.yml 
version: '3'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports: 
      - "8080:8080"
    volumes:
      - "$PWD/jenkins_home:/var/jenkins_home"
    networks:
      - net
networks:
  net:
    
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ ls -l
total 4
-rw-rw-r--. 1 jenkins jenkins 228 Apr 25 09:53 docker-compose.yml
drwxrwxr-x. 2 jenkins jenkins   6 Apr 25 09:50 jenkins_home
[jenkins@localhost jenkins-data]$ 

5. Create a Docker Container for Jenkins

[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ id
uid=1000(jenkins) gid=1000(jenkins) groups=1000(jenkins),10(wheel),994(docker) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ sudo chown 1000:1000 jenkins_home -R
[sudo] password for jenkins: 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker-compose up -d
Creating network "jenkins-data_net" with the default driver
Creating jenkins ... done
[jenkins@localhost jenkins-data]$ cat docker-compose.yml 
version: '3'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports: 
      - "8080:8080"
    volumes:
      - "$PWD/jenkins_home:/var/jenkins_home"
    networks:
      - net
networks:
  net:
    
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   12 seconds ago   Up 11 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   39 seconds ago   Up 38 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   46 seconds ago   Up 45 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   47 seconds ago   Up 46 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   48 seconds ago   Up 47 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   49 seconds ago   Up 48 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   55 seconds ago   Up 55 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker logs -f jenkins
Running from: /usr/share/jenkins/jenkins.war
webroot: /var/jenkins_home/war
2023-04-25 14:08:17.240+0000 [id=1]     INFO    winstone.Logger#logInternal: Beginning extraction from war file
2023-04-25 14:08:18.274+0000 [id=1]     WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2023-04-25 14:08:18.333+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: jetty-10.0.13; built: 2022-12-07T20:13:20.134Z; git: 1c2636ea05c0ca8de1ffd6ca7f3a98ac084c766d; jvm 11.0.18+10
2023-04-25 14:08:18.613+0000 [id=1]     INFO    o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2023-04-25 14:08:18.670+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: Session workerName=node0
2023-04-25 14:08:19.147+0000 [id=1]     INFO    hudson.WebAppMain#contextInitialized: Jenkins home directory: /var/jenkins_home found at: EnvVars.masterEnvVars.get("JENKINS_HOME")
2023-04-25 14:08:19.317+0000 [id=1]     INFO    o.e.j.s.handler.ContextHandler#doStart: Started w.@6cd164a6{Jenkins v2.401,/,file:///var/jenkins_home/war/,AVAILABLE}{/var/jenkins_home/war}
2023-04-25 14:08:19.334+0000 [id=1]     INFO    o.e.j.server.AbstractConnector#doStart: Started ServerConnector@2e9fda69{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2023-04-25 14:08:19.347+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: Started Server@40f70521{STARTING}[10.0.13,sto=0] @2629ms
2023-04-25 14:08:19.351+0000 [id=24]    INFO    winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2023-04-25 14:08:19.600+0000 [id=31]    INFO    jenkins.InitReactorRunner$1#onAttained: Started initialization
2023-04-25 14:08:19.626+0000 [id=30]    INFO    jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2023-04-25 14:08:20.462+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2023-04-25 14:08:20.466+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: Started all plugins
2023-04-25 14:08:20.471+0000 [id=32]    INFO    jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2023-04-25 14:08:20.660+0000 [id=33]    INFO    jenkins.InitReactorRunner$1#onAttained: System config loaded
2023-04-25 14:08:20.660+0000 [id=33]    INFO    jenkins.InitReactorRunner$1#onAttained: System config adapted
2023-04-25 14:08:20.661+0000 [id=33]    INFO    jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2023-04-25 14:08:20.671+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2023-04-25 14:08:20.739+0000 [id=49]    INFO    hudson.util.Retrier#start: Attempt #1 to do the action check updates server
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/var/jenkins_home/war/WEB-INF/lib/groovy-all-2.4.21.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2023-04-25 14:08:21.079+0000 [id=35]    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:

ee285e455df144e1b09baebe8ebd3304

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

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

2023-04-25 14:08:38.358+0000 [id=35]    INFO    jenkins.InitReactorRunner$1#onAttained: Completed initialization
2023-04-25 14:08:38.371+0000 [id=23]    INFO    hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
2023-04-25 14:08:39.687+0000 [id=49]    INFO    h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2023-04-25 14:08:39.687+0000 [id=49]    INFO    hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1

 

 

 

 

 

 6. Troubleshooting: Jenkins not coming up?

Issue:

docker ps doesn't show the running container.

Debug:

docker ps -a shows the container with exit status.

docker logs jenkins shows a volume permission error.

Reason:

* Inside of the Jenkins container, there's a user named "jenkins" which has a Linux uid of 1000.

* You are mounting a docker volume to /var/jenkins_home which is the home directory of that user. If the directory doesn't have 1000 permissions, then the user won't be able to write/delete files, which causes the container to exit.

Resolution:

Apply 1000 permissions to your jenkins-data folder, and then restart the container.

 
  1. sudo chown 1000:1000 -R ~/jenkins-data
  2. docker-compose up -d

7. Stop jenkins

[jenkins@localhost ~]$ ls -ltr
total 0
drwxrwxr-x. 3 jenkins jenkins 52 Apr 25 09:53 jenkins-data
[jenkins@localhost ~]$ cd jenkins-data/
[jenkins@localhost jenkins-data]$ ls -ltr
total 8
-rw-rw-r--.  1 jenkins jenkins  228 Apr 25 09:53 docker-compose.yml
drwxrwxr-x. 13 jenkins jenkins 4096 Apr 25 10:21 jenkins_home
[jenkins@localhost jenkins-data]$ cat docker-compose.yml 
version: '3'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports: 
      - "8080:8080"
    volumes:
      - "$PWD/jenkins_home:/var/jenkins_home"
    networks:
      - net
networks:
  net:
    
[jenkins@localhost jenkins-data]$ docker-compose stop
Stopping jenkins ... done
[jenkins@localhost jenkins-data]$ 


 

[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker-compose start
Starting jenkins ... done
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   30 minutes ago   Up 7 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 


[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
d6ebbd5fec50   jenkins/jenkins   "/usr/bin/tini -- /u…"   32 minutes ago   Up 2 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker-compose down
Stopping jenkins ... done
Removing jenkins ... done
Removing network jenkins-data_net
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ cat docker-compose.yml 
version: '3'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    ports: 
      - "8080:8080"
    volumes:
      - "$PWD/jenkins_home:/var/jenkins_home"
    networks:
      - net
networks:
  net:
    
[jenkins@localhost jenkins-data]$ docker-compose up -d
Creating network "jenkins-data_net" with the default driver
Creating jenkins ... done
[jenkins@localhost jenkins-data]$ 
[jenkins@localhost jenkins-data]$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
009ab632d3f4   jenkins/jenkins   "/usr/bin/tini -- /u…"   14 seconds ago   Up 12 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 50000/tcp   jenkins
[jenkins@localhost jenkins-data]$ 

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。