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

Solution 1:

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. E.g. 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:/$