3

i'm new to jenkins. currently i have a jenkins server start by docker with this docker-compose file:

version: '3.7'
services:
  jenkins:
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
      - 50000:50000
    container_name: jenkins
    volumes:
      - ~/jenkins:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/local/bin/docker:/usr/local/bin/docker

then i created a simple pipeline to test the docker inside jenkins. This is the pipeline script:

node {
    stage "Create build output"
   
    sh "docker info"

}

and the error is the message below:

Started by user myuser
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pip
[Pipeline] {
[Pipeline] stage (Create build output)
Using the ‘stage’ step without a block argument is deprecated
Entering stage Create build output
Proceeding
[Pipeline] sh
+ docker info
/var/jenkins_home/workspace/test-pip@tmp/durable-eb4fd6e4/script.sh: 1: docker: Permission denied
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

after checked some other topic, i already done all of these command:

chmod 777 /var/run/docker.sock
sudo usermod -a -G docker $USER

but nothing works. i also install docker plugin for jenkins. After get inside the jenkins container by this command:

docker exec -it 9729efd670b7 /bin/bash

i do the docker command:

docker info

but the console print out command not found:

bash: docker: command not found

does anyone know what should i config to make jenkins run docker? thank you.

rok
  • 9,403
  • 17
  • 70
  • 126
guy normal
  • 33
  • 1
  • 3
  • can you run this command like this ```sudo docker info``` – Dashrath Mundkar Sep 04 '21 at 08:22
  • 1
    @DashrathMundkar i tried but the error change to : ``bash: sudo: command not found`` – guy normal Sep 04 '21 at 08:34
  • and my os is ``CentOS Linux release 8.4.2105`` btw – guy normal Sep 04 '21 at 08:38
  • [jenkins 'execute script' build step Error: /bin/docker: Permission denied](https://stackoverflow.com/questions/42150870/jenkins-execute-script-build-step-error-bin-docker-permission-denied) is similar describes an SELinux-related issue. Does that question help you? – David Maze Sep 04 '21 at 11:29
  • (It's often better to install the `docker` binary using a package manager in a custom image's Dockerfile than to try to bind-mount the host's binary. That could have missing shared-library dependencies or be for the wrong OS entirely. Though, these problems wouldn't usually result in a "permission denied" error.) – David Maze Sep 04 '21 at 11:30
  • Thank you for your comment. But after correct the mount path to ``/usr/bin/docker``. now it works. – guy normal Sep 05 '21 at 12:41

2 Answers2

4

There is some diferent posibilitites to run jenkins into docker, see details in this link.
Basically, there is three solutions :

  1. Extend the jenkins image to install docker
  2. Mount docker host to docker container
  3. Run another image which contains docker-in-docker

In your case, you can use he second solutions, but since each solutions got advantages and inconvenients, you can try another.
Try to change your mount path to match the container path which should be /usr/bin/docker.

fmdaboville
  • 1,394
  • 3
  • 15
  • 28
0

See this demo project where jenkins runs in docker container with docker client inside of it.

To have working docker client inside Jenkins container follow the below flow:

  • build custom Jenkins Dockefile
  • copy docker client inside custom Jenkins Dockerfile (for example, from dind image)
  • expose host docker socket to Jenkins container using bind mount.
rok
  • 9,403
  • 17
  • 70
  • 126