Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run docker commands inside jenkins pipeline jobs

In my Manage Jenkins > Global Tool Configuration, i have already configured a tool called "docker" as follows:

name:                   docker
install automatically:  CHECKED
docker version:         latest

Then all I have in my jenkinsfile is the following and nothing else:

node {
    DOCKER_HOME = tool "docker"
    sh """
        echo $DOCKER_HOME
        ls $DOCKER_HOME/bin/
        $DOCKER_HOME/bin/docker images
        $DOCKER_HOME/bin/docker ps -a
    """
}

I get an error like this "Cannot connect to the Docker daemon. Is the docker daemon running on this host?".

Following is the full console log:

Started by user Syed Rakib Al Hasan
[Pipeline] node
Running on master in /var/jenkins_home/workspace/helloDocker
[Pipeline] {
[Pipeline] tool
[Pipeline] sh
[helloDocker] Running shell script
+ echo /var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker
/var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker
+ ls /var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/
docker
+ /var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker images
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

How do i ensure that the docker daemon/service is running/started before my pipeline reaches the line to run docker commands.

Is there any other native docker-build-step plugin way to achieve what I am doing here? Like docker ps -a or docker images or docker build -t?

Some assumptions:

Let's say my chosen node do not already have docker/docker-engine installed/running in my host machine. That's the purpose of the tool command to automatically install docker in the node if it is not already there.

like image 551
Rakib Avatar asked Oct 29 '22 13:10

Rakib


1 Answers

This Jenkins plugin is for the docker client; I'd solve (work around) by:

  • setting up jenkins slaves where docker daemon is reachable, add a label
  • setting up a housekeeping job which will fail if docker daemon was not reachable (so we can notify the infra team without having the QA to figure out and escalate the problem)
  • assign jobs which assumes the docker daemon to be reachable to this label

I hope it helps, and I'm curious if any of you have a better solution!

like image 162
GyulaWeber Avatar answered Nov 15 '22 06:11

GyulaWeber