在jenkins容器内部使用docker

在jenkins容器内部使用docker

1.使用本地的docker

进入/var/run,找到docker.sock

[root@npy run]# ls
auditd.pid  containerd   cryptsetup          dmeventd-client  docker.pid   initramfs  lvm          netreport       sepermit  sudo         tmpfiles.d  user
chrony      crond.pid    dbus                dmeventd-server  docker.sock  lock       lvmetad.pid  NetworkManager  setrans   syslogd.pid  tuned       utmp
console     cron.reboot  dhclient-ens33.pid  docker           faillock     log        mount        plymouth        sshd.pid  systemd      udev        xtables.lock
[root@npy run]# pwd
/var/run
[root@npy run]# 

2.修改docker.sock文件所属组

chown root:root docker.sock

3.修改权限

chmod o+rw docker.sock

此时的docker.sock

srw-rw-rw-  1 root   root      0 211 20:39 docker.sock

4. 修改jenkins的docker-compose.yml

追加挂载数据

/var/run/docker.sock://var/run/docker.sock

/usr/bin/docker:/usr/bin/docker

/etc/docker/daemon.json:/etc/docker/daemon.json

version: '3.1'
services:
  jenkins:
    image: jenkins/jenkins
    privileged: true
    user: root
    ports:
      - 10240:8080
      - 10241:50000
    container_name: npy_jenkins
    volumes:
      - /home/jenkins/jenkins_mount:/var/jenkins_home
      - /etc/localtime:/etc/localtime
      - /var/run/docker.sock://var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /etc/docker/daemon.json:/etc/docker/daemon.json

5.docker测试

进入容器执行docker,发现版本可以显示

[root@npy jenkins_docker]# docker exec -it 65f2c8960631 bash
root@65f2c8960631:/# docker version
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:51:00 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:48:42 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.16
  GitCommit:        31aa4358a36870b21a992d3ad2bef29e1d693bec
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
root@65f2c8960631:/# 

你可能感兴趣的:(运维,docker,jenkins,容器)