Issue
I am trying to build a docker image using Jenkins pipeline, with Jenkins running in a Docker container.
This is my Jenkinsfile:
stage 'Deploy'
dir('/apps/npt'){
sh 'chmod +x deploy_prod.sh'
sh 'cat deploy_prod.sh'
sh './deploy_prod.sh'
}
this is how I start my container:
docker run --detach --publish 8080:8080 -u 0 --volume jenkins_volume:/var/jenkins_home --volume apps:/apps --name jenkins jenkins/jenkins
This is the sh file:
#!/bin/bash
#build docker container
docker build -t npt .
This is the error I get:
./deploy_prod.sh: line 6: docker: command not found
I have tried to add the docker socket as a volume to the container -v /var/run/docker.sock:/var/run/docker.sock
, but stil same error.
I am using the same configuration on a different project with jenkins running as a service on the VM, with no issues...
Solution
You will need to add docker client inside the Jenkins docker container. Take a look at https://github.com/jenkinsci/docker/blob/master/README.md#installing-more-tools for how to do that. You can also install it within the container using docker exec.
When the script runs inside the container, it checks the PATH variable to resolve docker
command. The docker client then writes instructions to /var/run/docker.sock. The docker server reads these instructions and acts accordingly.
-v /var/run/docker.sock:/var/run/docker.sock
-> this means that the docker client within your docker container can talk to docker engine(server/VM) running on host.
Answered By - Palash Goel
Answer Checked By - Terry (JavaFixing Volunteer)