Cài đặt Jenkins trên Docker cho Hệ thống Debian/Ubuntu

Cập nhật hệ thống

sudo apt update
sudo apt upgrade -y

Cài đặt các thành phần phụ thuộc

sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

Thêm kho lưu trữ Docker

curl -fsSL https://download.docker.com/linux/debian/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
https://download.docker.com/linux/debian $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list

Cài đặt Docker Engine

sudo apt update
sudo apt install -y \
    docker-ce \
    docker-ce-cli \
    containerd.io

Kiểm tra cài đặt Docker

sudo docker run --rm hello-world

Thay thế nguồn APT (nếu cần)

Sao lưu file cấu hình:

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

Cập nhật nội dung với mirror của Aliyun:

echo "deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs) main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs) main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ $(lsb_release -cs)-backports main restricted universe multiverse" | \
sudo tee /etc/apt/sources.list

Triển khai Jenkins Container

sudo docker run -d \
  --privileged \
  --name jenkins_instance \
  -p 9080:8080 \
  -v /data/jenkins:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  jenkins/jenkins:lts

Cấu hình Update Center

sudo sed -i 's|https://updates.jenkins.io/update-center.json|https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json|' \
/data/jenkins/hudson.model.UpdateCenter.xml

sudo docker restart jenkins_instance

Cài đặt Docker trong Container Jenkins

sudo docker exec -it jenkins_instance bash -c \
"apt update && apt install -y docker.io && \
echo '{\"registry-mirrors\":[\"https://registry.docker-cn.com\"]}' > /etc/docker/daemon.json && \
service docker restart"

Thiết lập ban đầu

Truy cập Jenkins qua trình duyệt tại cổng 9080. Mật khẩu khởi tạo được hiển thị trong logs container:

sudo docker logs jenkins_instance

Thẻ: docker Jenkins CI/CD containerization

Đăng vào ngày 4 tháng 7 lúc 10:44