简体   繁体   中英

How to get docker-compose to work in docker generated jenkins pipeline on windows?

I am running this code to start up a dockerized jenkins. This works.

docker run -p 81:8080 -p 50000:50000 --name myjenkins --privileged -v %cd%/jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins

I have a successful checkout scm using bitbucket, webhooks and connecting to my local jenkins over firewall everytime I push. Previous Jenkinsfile example works:

pipeline {
  agent any

  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
  }
}

I have then added into my Jenkinsfile

pipeline {
  agent any
  stages {
      stage('Test') {
        steps {
          echo 'Testing...'
        }
      }
      stage('docker-compose') {
          steps {
             sh "./dockcompose.sh"
          }
      }
  }
  post {
     always {
        sh "./dockcompose-down.sh"
     }
  }
}

I have a local docker-compose.yml file which I have thoroughly tested locally and works. In the pipeline error I get:

docker-compose: not found

Any suggestions why it can't find docker-compose and how to get it to recognise the method? thanks

You must install docker-compose inside the container since the jenkins/jenkins image does not come with docker-compose . I would suggest to extend the original image by creating your own Dockerfile. Eg inside any directory, create the following file named Dockerfile :

FROM jenkins/jenkins

USER root

# see https://docs.docker.com/compose/install/
RUN curl -L \
  "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" \
  -o /usr/local/bin/docker-compose \
  && chmod +x /usr/local/bin/docker-compose

USER jenkins

Now from within that directory build the new image, we will name it myjenkins :

docker build -t myjenkins .
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM jenkins/jenkins
 ---> 57f9f0b056cc
Step 2/4 : USER root
 ---> Using cache
 ---> f6dfbc759063
Step 3/4 : RUN curl -L   "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)"   -o /usr/local/bin/docker-compose   && chmod +x /usr/local/bin/docker-compose
 ---> Running in 36c8c22c01fb
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   617    0   617    0     0   1944      0 --:--:-- --:--:-- --:--:--  1946
100 16.4M  100 16.4M    0     0  2404k      0  0:00:06  0:00:06 --:--:-- 3423k
Removing intermediate container 36c8c22c01fb
 ---> ca119efd5ea6
Step 4/4 : USER jenkins
 ---> Running in 5d1149b328b5
Removing intermediate container 5d1149b328b5
 ---> 841b3adbfe94
Successfully built 841b3adbfe94
Successfully tagged myjenkins:latest

You can list it with docker image ls :

docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
myjenkins                  latest              841b3adbfe94        10 minutes ago      585MB
...

Now start your container. Using your command, you only have to replace the image name jenkins/jenkins at the end of your command with myjenkins (note for testing on my mac PC I removed the bind-mount of /var/jenkins_home but you should be able to keep it). Also, remove previously started container if necessary:

docker run --rm -p 81:8080 -p 50000:50000 --name myjenkins  -v /var/run/docker.sock:/var/run/docker.sock myjenkins

Enter your container and check if docker-compose is installed:

docker exec -it myjenkins bash
jenkins@43b74c8f602b:/$ docker-compose --version
docker-compose version 1.25.3, build d4d1b42b
jenkins@43b74c8f602b:/$ 

apparently docker-compose executable is not in the PATH environment variable of the user under which Jenkins runs..

Best solution however is not to use docker compose but simple docker build or kubernetes plugin

Jenkins Container in docker-compose file: enter image description here

You should add this rows in docker-compose file.

Don't forget /usr/local/bin/docker-compose file is mustn't empty in your server machine.

Jenkins execute result: enter image description here

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