"Docker: command not found" from Jenkins on MacOS - docker

When running jobs from Jenkinsfile with Pipeline syntax and a Docker agent, the pipeline fails with "Docker: command not found." I understand this to mean that either (1) Docker is not installed; or (2) Jenkins is not pointing to the correct Docker installation path. My situation is very similar to this issue: Docker command not found in local Jenkins multi branch pipeline . Jenkins is installed on MacOS and running off of localhost:8080. Docker is also installed (v18.06.0-ce-mac70)./
That user's solution included a switch from pipeline declarative syntax to node scripted syntax. However I want to resolve the issue while retaining the declarative syntax.
Jenkinsfile
#!groovy
pipeline {
agent {
docker {
image 'node:7-alpine'
}
}
stages {
stage('Unit') {
steps {
sh 'node -v'
sh 'npm -v'
}
}
}
}
Error message
docker inspect -f . node:7-alpine
docker: command not found
docker pull node:7-alpine
docker: command not found
In Jenkins Global Tool Configuration, for Docker installations I tried both (1) install automatically (from docker.com); and (2) local installation with installation root /usr/local/.
All of the relevant plugins appears to be installed as well.

I solved this problem here: https://stackoverflow.com/a/58688536/8160903
(Add Docker's path to Homebrew Jenkins plist /usr/local/Cellar/jenkins-lts/2.176.3/homebrew.mxcl.jenkins-lts.plist)

I would check the user who is running the jenkins process and make sure they are part of the docker group.

You can try adding the full path of docker executable on your machine to Jenkins at Manage Jenkins > Global Tool Configuration.
I've seen it happen sometimes that the user which has started Jenkins doesn't have the executable's location on $PATH.

Related

Jenkins avoid tool installation if it is installed already

Not a jenkins expert here. I have a scripted pipeline where I have tool installed (Node). Unfortunately it was configured to pull in other dependencies which takes 250sec in overall now. I'd like to add a condition to avoid this installation if it(Node with packages) was already installed previously, but don't know where to start. Perhaps jenkins stores meta info from prev runs that can be checked?
node {
env.NODEJS_HOME = "${tool 'Node v8.11.3'}"
env.PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
env.PATH = "/opt/xs/bin:${env.PATH}"
// ...
}
Are you using dynamic jenkins agents (docker containers)? In this case tools will be installed everytime you run build.
Mount volumes to containers, use persistant agents or build your own docker image with installed nodejs.
As I see you use workaround to install nodejs tool.
Jenkins supports it native way (declarative style):
pipeline {
agent any
tools {
nodejs 'NodeJS_14.5'
}
stages {
stage ('nodejs test') {
steps {
sh 'npm -v'
}
}
}
}
On first run tools will be installed. On next ones - will not since it is installed.

Invalid agent type "call" specified. Must be one of [any, docker, dockerfile, kubernetes, label, none]

We are using the code below to spin up a kubernetes cloud docker agent in Jenkins:
agent {
docker { image 'golang:1.14' }
}
We get the error below:
14:55
I get;
WorkflowScript: 158: Invalid agent type "call" specified. Must be one of [any, docker, dockerfile, kubernetes, label, none] # line 158, column 17.
docker { image 'golang:1.14' }
^
Any ideas how to fix this, please?
We have a jenkins.gdsl file which could be in the mix. Any ideas whether this is the right direction to follow? (We are not even sure what a gdsl file is!)
Probably you have to install both plugins:
Docker Pipeline
Docker plugin + deps(Auto)
Not very well explained on the doc and it seems the main plugin Docker plugin does not install Docker Pipeline by default.
See here Using Docker with Pipeline
You should install docker plugins to be able to use Docker in Jenkins.
Go to manage Jenkins.
Manage Plugins.
Install:
Docker pipeline
Docker
You should see the following plugins installed:
Optionally restart Jenkins.

'Cannot run program "docker"' when using withDockerRegistry in Declarative Jenkins Pipeline

I'm in the middle of refactoring our old scripted jenkins pipeline and am trying to take advantage of the nice jenkins declarative syntax.
However I'm having issue trying to authenticate with our private docker registry.
The resources online tell me this should work:
steps {
sh 'docker -v'
withDockerRegistry([url: DOCKER_REGISTRY_URL, credentialsId: DOCKER_REGISTRY_CREDENTIALS]) {
pushDockerImage()
}
}
The first line, "docker -v", executes correctly and the current docker version is printed out.
However when it tries to execute "withDockerRegistry" it fails with:
Cannot run program "docker": error=2, No such file or directory
Do I have the syntax wrong or am I missing some sort of global config?
Cheers,
Add a toolName to the withDockerRegistry:
withDockerRegistry(registry: [url: DOCKER_REGISTRY_URL, credentialsId: DOCKER_REGISTRY_CREDENTIALS], toolName: 'docker')
Also setup docker in Global Tool Configuration.

Jenkins pipeline/docker :Jenkins does not seem to be running inside a container

I'm trying to execute the sample of code found in Jenkins Pipeline here : https://jenkins.io/doc/book/pipeline/docker/
node {
/* Requires the Docker Pipeline plugin to be installed */
docker.image('maven:3-alpine').inside('-v $HOME/.m2:/root/.m2') {
stage('Build') {
sh 'mvn -B'
}
}
}
And give me this error:
[Pipeline] withDockerContainer
Jenkins does not seem to be running inside a container
[Pipeline] // withDockerContainer
I don't know why is it stopping like that without doing anything.
I have already install docker, docker plugin/docker pipeline on the latest version.
In configuration tool, i add the installation root path.
Did I miss something ?
Thanks in advance
This message is a normal debug message, maybe a little confusing, but not an error. As the Jenkins Pipeline code is written, during initialization it checks whether the step is already running in a container. I think the message could be written better.
If you have more problems than this message, please provide the entire log. Sounds like maybe a node cannot be assigned, or the docker client is not installed, or the docker image cannot be pulled.
The issue is a bit old but I faced a similar situation and I want to share.
I noticed that Jenkins mentions the the cause of the issue at the end of the pipeline logs.
For example in my case, the issue states:
java.io.IOException: Failed to run top '0458e2cc8b4e09c53bb89f680026fc8d035d7e608ed0b60912d9a61ebb4fea4d'. Error: Error response from daemon: Container 0458e2cc8b4e09c53bb89f680026fc8d035d7e608ed0b60912d9a61ebb4fea4d is not running
When checking the stage where this happened it's similar to the above you mentioned when using dockerImage.inside(), the reason in my case is that my Dockefile already defines an entrypoint and when using the inside feature jenkins gets confused, so to avoid this try overriding the entrypoint by passing it as a parameter to the inside function as follows:
dockerImage.inside("--entrypoint=''") {
echo "Tests passed"
}
Another good way to find the issue is to access your jenkins server ans list the docker containers with docker ps -a you may find the build container failed, check the logs then you will get a hint, in my case the logs says cat: 1: ./entrypoint.sh: not found.

Jenkins error "No such property: docker for class: groovy.lang.Binding"

I'm trying to follow this tutorial to create a simple docker environment in as part of my Jenkins pipeline build.
I'm trying to build a couple of Docker images just as a test before I do my maven build. At the moment I have the following Groovy for my Jenkinsfile:
#!groovy
node {
stage 'Building docker env'
def dbImage = docker.build('oracle', 'docker/oracle')
def wlpImage = docker.build('liberty', 'docker/liberty')
stage 'Running maven build'
git url: 'https://mysite/myproject.git', branch: 'docker'
def mvnHome = tool 'maven 3.3.9'
sh "${mvnHome}/bin/mvn -B clean install"
}
I'm trying to have the Docker build look in the directory "docker/oracle" and call the Dockerfile in that directory, and build the Docker images named oracle, and liberty. At the moment though it's giving me this error:
Running on master in /root/.jenkins/workspace/pipeline_test
[Pipeline] {
[Pipeline] stage (Building docker env)
Using the ‘stage’ step without a block argument is deprecated
Entering stage Building docker env
Proceeding
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: docker for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:241)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
...
Any ideas what could be the problem with the docker build command I'm using? (or could it be something I forgot to install in Jenkins?)
The issue was I needed to install the Docker Pipeline plugin in Jenkins.
To install the plugin using the GUI:
Dashboard > Manage Jenkins > Manage Plugins > Available (tab) > docker-workflow.
As Pete says you will have to install the Docker Pipeline plugin. You can do that though the Jenkins UI.
Maybe I'm missing some part of your code, but where do you define the docker? If that's the complete Groovy script, you're trying to bind a variable which isn't declared anything, so it's not to weird that it fails, right?
Just define a docker it that's what you want, like:
def docker = "my docker" // something similar like this
And it will at-least resolve your missing property exception.
Whenever we see the error like below :
groovy.lang.MissingPropertyException: No such property:
It means , groovey script did not able to find the property mentioned post the colon sign : , so we need to either define the user defined variable/property or use the correct one from API.
If you have this issue:
groovy.lang.MissingPropertyException: No such property: docker for class: groovy.lang.Binding.
We most likely encountered the same issue, in order to fix it I only had to install the Docker Pipeline plugin in Jenkins, so all you have to do is go to:
Jenkins Homepage > Manage Jenkins > Manage Plugins > Available
Search for Docker Pipeline install it restart jenkins and you are ready to go.
For more info about Docker Pipeline Plugin Scripts click here.
I had the same issue but after I installed the Docker Pipeline plugin as #Affes Salem suggested it is working now.

Resources