Operations | Install Jenkins using Docker | Jenkins

Operations | Install Jenkins using Docker | Jenkins

Preface

The content of this issue is mainly for learning how to install Jenkins through Docker. It is only for record and reference. I hope it will be helpful to everyone.

Preparation

  • System: CentOS 7.9
  • Configuration: 4c8g

Quick installation

The following is an example of installing Jenkins using Docker.

Environment setup

  • Docker installation documentation: https://docs.docker.com/engine/install/
  • Docker Compose installation documentation: https://docs.docker.com/compose/install/
Install Docker
  • Make sure the yum package is updated to the latest
yum update
  • Uninstall old version
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
  • Set warehouse source
yum install -y yum-utils
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
  • start installation
#由于repo中默认只开启stable仓库,故这里安装的是最新稳定版
yum install docker-ce docker-ce-cli containerd.io
  • Starting and booting
systemctl start docker
systemctl enable docker
  • Verify installation was successful
docker version
Install Docker Compose

The following is the independent installation method of Docker Compose. For other methods, please refer to the official documentation.

  • Installing Compose standalone
curl -SL https://github.com/docker/compose/releases/download/v2.12.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
  • Command execution failed
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • Set file or folder permissions
chmod +x /usr/local/bin/docker-compose
  • Verify whether the installation is successful
docker-compose version

Install Jenkins

Jenkins Chinese official website: https://www.jenkins.io/zh/

Deploy using Docker Compose
  1. Create a folder anywhere in the system. This document uses ~/opt/docker/jenkins as an example
mkdir -p ~/opt/docker/jenkins && cd ~/opt/docker/jenkins

Create some necessary files or folders, keeping the directory structure as shown in the example:

mkdir -p ./{config,data,logs}
.
├── docker
│   └── jenkins
│       ├── docker-compose.yml
│       ├── data
│       └── config
│       └── logs

This is for reference only, you can follow your own preferences.

Note: In subsequent operations, all data generated by Jenkins will be saved in this directory, please save it properly.

  1. createdocker-compose.yaml
version: '3'
services: # 集合                                
  docker_jenkins:
    user: root # 为了避免一些权限问题 在这我使用了root
    privileged: true
    restart: always # 重启方式
    image: jenkins/jenkins:lts # 指定服务所使用的镜像 在这里我选择了 LTS (长期支持)
    container_name: jenkins # 容器名称
    ports: # 对外暴露的端口定义
      - 8082:8080 # 访问Jenkins服务端口
      - 50000:50000
    environment:
      TZ: Asia/Shanghai
    volumes: # 卷挂载路径
      - ./data:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker # 这是为了我们可以在容器内使用docker命令
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose

/usr/share/jenkinsContains some Jenkins configurations, such as plug-ins, which can be mapped to the host if necessary.

  1. Start service
docker-compose up -d

View logs in real time:

docker-compose logs -f
  1. address

Access address after installation is complete:http://{部署Jenkins所在服务IP}:8080

There will be a few minutes of waiting time here

Deploy using Docker
  1. Create a folder anywhere in the system. This document uses ~/opt/docker/jenkins as an example
mkdir -p ~/opt/docker/jenkins && cd ~/opt/docker/jenkins

Create some necessary files or folders, keeping the directory structure as shown in the example:

.
├── docker
│   └── jenkins
│       ├── data
│       └── config
│       └── logs
  1. Start service
docker run \
  -u root \
  --rm \
  -d \
  -p 8080:8080 \
  -p 50000:50000 \
  -v /opt/docker/jenkins/data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  --name jenkins \
  jenkins/jenkins:lts

Check the running log:

docker ps // 查看containerId
docker logs [ID | NAME]

View real-time logs:

docker logs -f [ID | NAME]

Pay attention to check whether 8080 is already occupied! If the modified port is occupied

  1. address

Access address after installation is complete:http://{部署Jenkins所在服务IP}:8080

There will be a few minutes of waiting time here

user's guidance

initialization

For details, see the official website tutorial:https://www.jenkins.io

Unlock Jenkins
  • method one

Enter the Jenkins container:docker exec -it {Jenkins容器名} bash

For example:docker exec -it jenkins bash

View password:cat /var/jenkins_home/secrets/initialAdminPassword

Copy the password into the input box

  • Method 2

Check the log to get the password

docker logs -f [ID|NAME]
或
docker-compose logs -f

After entering the password correctly, follow the recommended instructions to boot the operation, and wait until the boot installation is completed to enter the Jenkins home page.

Default plugin

If there are no special requirements, just choose the recommended plug-in to install.

Create admin user

Important configuration

Configure address and email

Plug-in management

Install git parameters
  • Search plugin
  1. Entry 首页系统管理插件管理可选插件 Interface

  2. Search git paremeters plug-in and click to install

  • Configure plugin
  1. Check This project is parameterized in the project general items and select Git Parameter

Install nodejs
  • Search plugin
  1. Entry 首页系统管理插件管理可选插件 Interface

  2. Search nodejs plug-in and click to install

  • Configure plugin
  1. Entry 首页系统管理全局工具配置 Interface
  2. Found NodeJS Configuration Bar

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

Install Publish Over SSH
  • Search plugin
  1. Entry 首页系统管理插件管理可选插件 Interface

  2. Search ssh plug-in and click to install

  • Configure plugin
  1. Entry 首页系统管理全局工具配置 Interface
  2. Found SSH Servers Configuration Bar

Install Maven
  • Search plugin
  1. Entry 首页系统管理插件管理可选插件 Interface

  2. Search Maven Integration plug-in and click to install

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

  • Configure plugin
  1. Entry 首页系统管理全局工具配置 Interface
  2. Found maven–maven Configuration Bar

Install Chinese language
  • Search plugin
  1. Entry 首页系统管理插件管理可选插件 Interface

  2. Search Locale plug-in and click to install

After the installation is complete, enter /restart after the path in the address bar to restart Jenkins

  • Configure plugin
  1. Entry 首页系统管理系统配置 Interface
  2. Find the Default language configuration bar and enter zh_CN or zh_TW

If the above method does not work, you can try to search for Localization: Chinese (Simplified) plug-in

The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly.

The latest version of Jenkins may have incomplete or ineffective translations

FAQ

The installation is complete but the Jenkins instance is offline.

The reason is that Jenkins will check the network connection before downloading the plug-in, and it will read the URL in the default.json file, which is located under /var/jenkins_home/updates. connectionCheckUrl The default is to access the Google address. If you do not circumvent the wall, an error may occur, so change the value of connectionCheckUrl to www.baidu.com. , restart the service after making changes.

When the installation is completed, the access address appears HTTP ERROR 503

If this screen appears, it is most likely a firewall problem, such as: the currently accessed port is not open, etc.

Jenkins initialization plug-in installation exception, unable to proceed to the next step

  • method one

You can try changing the network or checking whether a proxy is enabled or something like that.

  • Method 2

This method is to install Jenkins on the host machine. As for Docker, please refer to the processing flow of this method.

  1. Stop the Jenkins service
sudo systemctl stop jenkins
  1. Revision config.xmlText item
sudo vim /var/lib/jenkins/config.xml
Modify line 10 <useSecurity>true</useSecurity> to: <useSecurity>false</useSecurity> 

Delete lines <authorizationStrategy …/> and <securityRealm …> to</securityRealm>

end

That’s it for this issue. Friends passing by, please remember to support us!

Guess you like

Origin blog.csdn.net/weixin_39122254/article/details/134024368