Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline Docker -- Container is Not Running

I have Jenkins running on an EC2 Instance. I have the EC2 Plugin Configured in a Peered VPC, and when a job is tagged 'support_ubuntu_docker' it will spin up an Jenkins Slave, with Docker pre-installed.

I am able to follow the examples, and get my job to connect to the local docker running on the Slave, and run commands inside the container.

Working: https://pastebin.com/UANvjhnA

pipeline {
    agent {
        docker { 
            image 'node:7-alpine' 
            label 'support_ubuntu_docker'
             }
    }
    stages {
        stage('Test') {
            steps {
                sh 'node --version'
            }
        }
    }
}

Not Working https://pastebin.com/MsSZaPha

pipeline {
    agent {
        docker { 
            image 'hashicorp/terraform:light' 
            label 'support_ubuntu_docker'
             }
    }
    stages {
        stage('Test') {
            steps {
                sh 'terraform --version'
            }
        }
    }
}

I have tried with the ansible/ansible:default image, as well as a image I created myself.

FROM alpine:3.7
RUN apk add --no-cache terraform
RUN apk add --no-cache ansible
ENTRYPOINT ["/bin/ash"]

This image behaves locally.

[jenkins_test] docker exec -it 3843653932c8 ash                                                                                                                                                                                                                                                   10:56:42  ☁  master ☂ ⚡ ✭
/ # terraform --version
Terraform v0.11.0

/ # ansible --version
ansible 2.4.6.0
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.15 (default, Aug 22 2018, 13:24:18) [GCC 6.4.0]
/ # 

I really just want to be able to clone my terraform git repo, and use the terraform in the container to run my init/plan/applies.

The error im getting for all of these is.

java.io.IOException: Failed to run top 'c9dfeda21b718b9df1035500adf2ef80c5c3807cf63e724317d620d4bcaa14b3'. Error: Error response from daemon: Container c9dfeda21b718b9df1035500adf2ef80c5c3807cf63e724317d620d4bcaa14b3 is not running
like image 819
Justin Seiser Avatar asked Sep 28 '18 15:09

Justin Seiser


3 Answers

I had to change the entrypoint to empty to get it working with the following script it worked like a charm:

pipeline {
  agent {
    docker {
        image 'hashicorp/terraform:light'
        args '-i --entrypoint='
    }
  }
  stages {
    stage('Test') {
        steps {
            sh 'terraform --version'
        }
    }
  }
}
like image 137
S.Spieker Avatar answered Oct 13 '22 06:10

S.Spieker


The question really should have been a Docker question; what's the difference between node:7-alpine and hashicorp/terraform:light?

hashicorp/terraform:light has an ENTRYPOINT entry, pointing to /bin/terraform.
Basically that means you run it this way:
docker run hashicorp/terraform:light --version
And it will exit right away, i.e., it's not interactive.
So if you want an interactive shell within that Docker container, you'll have to override the ENTRYPOINT to point at a shell, say, /bin/bash and also tell Docker to run interactively:

pipeline {
    agent {
        docker { 
            image 'hashicorp/terraform:light' 
            args '-it --entrypoint=/bin/bash'
            label 'support_ubuntu_docker'
        }
    }
    stages {
        stage('Test') {
            steps {
                sh 'terraform --version'
            }
        }
    }
}
like image 20
samhain1138 Avatar answered Oct 13 '22 07:10

samhain1138


In a scripted pipeline you can do this:

docker.image(dockerImage).inside("--entrypoint=''") {
    // code to run on container
}

If you are creating the image to use in Jenkins from a base image that already has an ENTRYPOINT instruction, you can override it by adding this line to the end of your own Dockerfile:

ENTRYPOINT []

Then the whole --entrypoint is no longer needed.

like image 6
Lee Meador Avatar answered Oct 13 '22 06:10

Lee Meador