[Declarative pipeline] Using Docker environment variables

936 views
Skip to first unread message

Peter Marcoen

unread,
Mar 2, 2017, 11:09:41 AM3/2/17
to Jenkins Users
In my Dockerfile I install Ant and declare environment variables pointing to this Ant installation:

FROM openjdk:7

# Install wget
RUN  apt-get update \
  && apt-get install -y wget \
  && rm -rf /var/lib/apt/lists/*

# Install ant
ENV ANT_VERSION 1.9.6
RUN cd && \
    wget -q http://archive.apache.org/dist/ant/binaries/apache-ant-${ANT_VERSION}-bin.tar.gz && \
    tar -xzf apache-ant-${ANT_VERSION}-bin.tar.gz && \
    mv apache-ant-${ANT_VERSION} /opt/ant && \
    rm apache-ant-${ANT_VERSION}-bin.tar.gz

ENV ANT_HOME /opt/ant
ENV PATH ${PATH}:/opt/ant/bin

# Create user jenkins
RUN groupadd -g 999 docker
RUN useradd jenkins -u 999 -g 999 --shell /bin/bash --create-home
RUN echo 'export ANT_HOME=/opt/ant'  >> /home/jenkins/.bashrc
RUN echo 'export PATH=$PATH:/opt/ant/bin' >> /home/jenkins/.bashrc
USER jenkins

As you can see I do 2 things: I use the docker ENV statement which should set the environment variables, then additionally I add export statements to the jenkins' user bashrc.
However, inside my Jenkins pipeline these environment variables don't appear to be set, no matter what I do. 
When I echo my path I see the following:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/jenkins/bin:/usr/lib/oracle/12.1/client64/bin:/usr/lib/oracle/12.1/client64/lib

When I echo my .bashrc there are indeed the following lines:

export ANT_HOME=/opt/ant
export PATH=$PATH:/opt/ant/bin

So why is my path set incorrectly?
I suspect the reason is that Jenkins overwrites the path when running docker, and I can kinda see this in the console output of Jenkins:

docker run -t -d -u 999:999 -w /opt/jenkins/workspace/testws -v /opt/jenkins/workspace/testws :/opt/jenkins/workspace/testws :rw -v /opt/jenkins/workspace/testws @tmp:/opt/jenkins/workspace/testws@tmp:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** --entrypoint cat 4b4e7722d454ad10aec20f95d2bb1c1ce527c880

And if I look in the documentation it even says how to set environment variables: https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Environment-variables
But this doesn't make sense to me. Isn't the whole point of using Docker that your environment will consistently be the same? So why would I want to use environment settings or tools from jenkins if I'm using docker? Then the environment is also not defined in a file anymore.

Is there any way around this? Or am I looking at it all wrong?

This is the Jenkinsfile that I use:

pipeline {
        agent {
                dockerfile {
                        label "docker"
                }
        }

        stages {
                stage("build") {
                        steps {
                                sh 'echo $PATH'
                                sh 'cat ~/.bashrc'
                        }
                }
        }
}

Peter Marcoen

unread,
Mar 2, 2017, 11:33:47 AM3/2/17
to Jenkins Users
Actually my issue looks a lot like this issue from 2015: https://groups.google.com/forum/#!searchin/jenkinsci-users/docker$20environment$20variables|sort:relevance/jenkinsci-users/TIwE0F_e8qs/6f4GP-a0CAAJ
However, this was an issue with the Cloudbees custom docker plugin.

Peter Marcoen

unread,
Mar 2, 2017, 11:46:18 AM3/2/17
to Jenkins Users
Could also be linked to this open issue: https://issues.jenkins-ci.org/browse/JENKINS-41204
Reply all
Reply to author
Forward
0 new messages