Search code examples
dockerjenkinsjenkins-pipelinejenkins-plugins

How to run Jenkins pipeline in a container based on a dockerfile?


I'm trying to run a pipeline inside a container based on a dockerfile I've stored in my SCM.

Here below the beginning of my pipeline:

pipeline {
    agent { 
        dockerfile {
            dir '002_CICD'
        }

    }
....

When I try to run the pipeline I have the following error:

   + docker build -t 66dae53280bc29614c69b505e48424c25e689b91 -f 002_CICD/Dockerfile 002_CICD
/var/jenkins_home/workspace/AMP_app_docker_pipe@tmp/durable-cbf3977b/script.sh: 1:
 /var/jenkins_home/workspace/AMP_app_docker_pipe@tmp/durable-cbf3977b/script.sh: docker: not found

To run the docker container I've installed the following Jenkins plugins:

  1. Docker plugin
  2. docker-buil-step
  3. Docker Pipeline
  4. Docker commons plugins
  5. Docker API plugin

I've also enabled the docker API on a remote server and the connections looks fine.

What am I missing? Many thanks


Solution

  • What you are trying to implement is a docker in docker setup(quite used with regard to the CI process)

    As your jenkins is running inside a container you need to install a docker (cli/client) in your Dockerfile hence the error docker not found.

    This docker client is will use the docker engine of the host for performing all the operations

    With regards to the error

    Cannot connect to the Docker daemon at unix:///var/run/docker.sock

    This can occur if the jenkins user doesn't have access to the host /var/run/docker.sock. Check the permissions of this file on host and the associated permissions of the user with which jenkins is running