Control environment variables inside a Docker container

Issue

Control environment variables inside a Docker container with the Docker Pipeline plugin.

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline plugin
  • Docker Pipeline plugin

Resolution

By default, when using the Docker Pipeline plugin the node’s environment variables will not be propagated to the docker container - since with Docker you want to get isolate environments. You can check this here

However, if at Node level (http/s:///computer//configure), under Node Properties, you add Environment variables, those will be added to the container.

If you would like to override again what is injected at Node level then you will need to use withEnvstep.

node ('linux-slave') {
    withEnv(['PATH=/usr/local/Cellar/coreutils/8.25/libexec/gnubin:/usr/local/Cellar/gnu-sed/4.2.2/libexec/gnubin:/Users/fbelzunc/cloudbees/support/support-shinobi-tools:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home//bin:/Users/fbelzunc/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/local/sbin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.6.5/bin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/beaker-1.16.0/bin:/usr/local/Cellar/ruby/2.1.0/bin/:/path/testing']) {
        docker.image('maven:3.3.3-jdk-8').inside {
          sh 'echo $PATH'
          sh 'mvn --version'
        }
    }
    sh 'echo $PATH'
}
Have more questions? Submit a request

1 Comments

  • 0
    Avatar
    Phil Clay

    This statement...

    • However, if at Node level (http/s:///computer//configure), under Node Properties, you add Environment variables, those will be added to the container.

    seems to be incorrect.

    The link points to a line of code that calls

    EnvVars envHost = computer.getEnvironment();

    However, computer.getEnvironment() does not include the environment variables added via Node Properties.

    This can be confirmed by creating a node named 'node-with-properties', and adding some environment variables via Node Properties, and running the following Jenkinsfile

    node('node-with-properties') {
        docker.image('alpine').inside() {
            sh 'env'
        }
    }

    You can then inspect the output of the env command, and notice that the environment variables from Node Properties are not available inside the docker container.

Please sign in to leave a comment.