kool
kool

Reputation: 3613

docker not found inside jenkins pipeline

I have a jenkins installed on my linux machine and connected it to my kubernetes cluster.

I've installed docker and docker-pipeline plugin inside jenkins and they can be found under /var/lib/jenkins/plugins:

docker-build-step
docker-build-step.jpi
docker-commons
docker-commons.jpi
docker-java-api
docker-java-api.jpi
docker-plugin
docker-plugin.jpi
docker-workflow
docker-workflow.jpi

and configured in Global Tool Configuration:

enter image description here

However when I run docker.build or even sh 'docker ps' inside my pipeline it throws:

/home/jenkins/agent/workspace/nodejs@tmp/durable-492ddc4c/script.sh: 1: docker: not found

EDIT:

I've built my own image with docker and when I exec into it I can run docker command but pipeline still throws docker not found command and found out it is because jnlp doesn't have docker installed. Any ideas how to fix it inside jenkins-inbound-agent?

Upvotes: 2

Views: 7480

Answers (2)

kool
kool

Reputation: 3613

I've fixed it by adding:

tools {
  <...>
  'org.jenkinsci.plugins.docker.commons.tools.DockerTool' 'docker'
}

in environment part of my pipeline. And it had appeared in my $PATH while checking inside the pod.

Then I've had issues while invoking docker.withRegistry() and solved it by using docker.withTool() as following:

docker.withTool('docker'){
    docker.withRegistry('repo','credentials') { 
        <rest of the pipeline here>
    }
}

It might not be the prettiest solution but it worked for my homelab use-case.

Upvotes: 2

atline
atline

Reputation: 31674

You should customize the jenkins/inbound-agent to add docker command, a minimal example as next:

Dockerfile:

FROM jenkins/inbound-agent
USER root
RUN apt-get update; \
apt-get install -y wget; \
wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz; \
tar zxvf docker-20.10.9.tgz; \
cp -f docker/docker /usr/local/bin; \
rm -fr docker-20.10.0.tgz docker; \
apt-get purge -y wget

Build:

docker build -t my-inbound-agent .

Above will install a docker client binary in customized image to let you have docker command in inbound-agent. But still, you need mount docker unix socket to inbound-agent when start it, also you need to use your customized agent image, like next:

Start agent:

docker run --init -v /var/run/docker.sock:/var/run/docker.sock my-inbound-agent -url ......

Then, we could verify it's ok in agent with next:

$ docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS         PORTS     NAMES
6ad70ee079f5   my-inbound-agent   "/usr/local/bin/jenk…"   7 minutes ago   Up 7 minutes             jolly_fermat
$ docker exec -it 6ad70ee079f5 docker version
Client:
 Version:           20.10.9
 API version:       1.41
 Go version:        go1.16.8
 Git commit:        c2ea9bc
 Built:             Mon Oct  4 16:03:22 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.1
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       f001486
  Built:            Tue Dec 15 04:32:45 2020
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Upvotes: 3

Related Questions