简体   繁体   中英

build docker file on host with Jenkins running in container

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM